Computing.Net > Forums > Programming > for /f tokens and delims help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

for /f tokens and delims help

Reply to Message Icon

Name: xirsteon
Date: October 2, 2009 at 10:46:23 Pacific
OS: Windows XP
Product: Dell Inspiron 11z notebook - customizable
Subcategory: Batch
Tags: batch, for /f, tokens, delims
Comment:

Hi, I'm new here but I need help with extracting info from a text file. So I've got a txt file that has network printer name with each line i.e. \\server\printer name
so, i've got a

for /f "tokens=1,2,* delims=" %%a in ('findstr /b /c:\\ printers.txt') do ( echo %%a )

This for statement only prints 'server' but I want it to print separately, both the 'server' and 'printer name' Thanks for the help in advance

The output should be like server printer name without the \\ and \



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: October 2, 2009 at 14:02:41 Pacific
Reply:

for /F "tokens=1,2 delims=\" %%a in ('findstr /b /c:\\ printers.txt') do echo.%%a %%b


1

Response Number 2
Name: xirsteon
Date: October 3, 2009 at 13:23:24 Pacific
Reply:

Thanks IVO. Very much appreciated.

I've another strange for /f statement.

I have a text file (sample.txt) whose output always have a tab before the actual texts. i.e.

\\server\printer name

I've got a for statement below that shows that same line but with the tab. How can I remove the tab?

here's the for statement.

for /f "tokens=2 delims=Z" %%a ('reg query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v device ') do echo.&echo.default printer:%%a

That returns what I want with the tab before the printer name. I've tried to re-assign the %%a to a variable and then try to remove the tab but no luck. Any help pls?



0

Response Number 3
Name: IVO
Date: October 4, 2009 at 12:44:05 Pacific
Reply:

setlocal EnableDelayedExpansion
for /F "tokens=2 delims=Z" %%a ('reg query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v device') do (
  set row=%%a
  echo.& echo.default printer: !row:~1!
)


1

Response Number 4
Name: xirsteon
Date: October 5, 2009 at 07:38:28 Pacific
Reply:

Thanks again IVO.

My last question:

So say I have a printers.txt file with the following output:
\\server\printer
\\server\printer
HP LaserJet xxx
Adobe PDF
default_printer: \\server\printer

and I have 2 for loops (1). to get the default printer , and the other (2). to map only the network printers,


(1).
REM Get the default printer
for /f "tokens=2 delims=Z," %%a in ('reg query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v "device"') do (
set row=%%a
echo.&echo.default printer:!row:~1!
echo. %%a >> printers.txt
)

(2).
REM maps all network printers

for /f "tokens=1,2,* delims=" %%B in ('findstr /b /c:\\ printers.txt') do (
echo. Now connecting %%B, please wait ....
echo.
rundll32 printui.dll,PrintUIEntry /in /n "%%B"
call:delay (wait 3 seconds)

..> check to make sure registry entry is created in
('reg query "HKCU\Printers\Connections\,,servername,printername"')

..> map the default printer only once from 1st for loop?

..> if the default printer is not a network printer, don't map it (for obvious reasons).



0

Response Number 5
Name: IVO
Date: October 5, 2009 at 08:51:02 Pacific
Reply:

Well, but I need more info

..> check to make sure registry entry is created in
('reg query "HKCU\Printers\Connections\,,servername,printername"')

Do you want just to check the presence of the key (already created) or to create the key and check if the result is correct?
In this last case let me know the command to create the key and in both cases which action must be activated if a failure is detected?

..> map the default printer only once from 1st for loop? 

What does the above mean? Please explain better.

..> if the default printer is not a network printer, don't map it (for obvious reasons).

Do you want the two For loops to be merged? Again the process is not fully clear to me.


0

Related Posts

See More



Response Number 6
Name: xirsteon
Date: October 5, 2009 at 10:19:27 Pacific
Reply:

..> check to make sure registry entry is created in
('reg query "HKCU\Printers\Connections\,,servername,printername"')
</>

When you run
rundll32 printui.dll,PrintUIEntry /in /n "%%B"

It creates a registry key in "HKCU\Printers\Connections", so I want to check if the reg key is created immediately after mapping each network printer which is why i have a call:delay --3 secs to make sure the reg entry gets created (in other words, this is for making sure the printer is on the network and that the connection was successfully created. )

If the reg key is present, I just want to print out "echo.Printer %%b successfully mapped."

if the key isn't present (printer isn't on the network or turned off), simple print a message saying printername is not online and proceed to mapping the rest of the network printers.

..> map the default printer only once from 1st for loop?

Among the printers in printers.txt is the default printer. so if there's a way to merge the two for loops in such a way that it gets the default printer first and then map the rest of the network printers (i wouldn't think there's any harm in mapping the default printer twice but if I can avoid mapping the default printer twice, it'll be great.)

..> if the default printer is not a network printer, don't map it (for obvious reasons).

Well, I only want to map only the network printer and infact if the default printer is a network printer, I want a logic to just map it and if the default printer is a local printer, i want a logic that'll simply say "Please contact your IT Dept to assist you in installing the driver for your local printer"

Since I don't know what kind of local printer the user will have, I'll have to manually hunt for the driver wherever it is.. so this is why I simply want only the network printer map.



0

Response Number 7
Name: IVO
Date: October 6, 2009 at 06:23:30 Pacific
Reply:

Sorry but I can understand the first For loop though I don't understand why you code

echo.%%b >> printers.txt

instead of

echo.default printer: !row:~1! >> printers.txt

I don't understand if 'map' is the same as 'connect' and if the default printer is defined in printers.txt by the string "default printer:". More I don't understand if you want to map (?) the default printer only if it is a network printer (identified by \\servername?).

What you want to code is quite easy, but your process and the related key are unfamiliar to me, so I fear to give you a messy script.

For example, to check for the creation of the key you could code

rundll32 printui.dll,PrintUIEntry /in /n "%%B"
if not Errorlevel 1 (
  echo. Printer %%B connected
) else 8
  echo. Printer %%B not found or turned off
)

The above works however if the rundll32 statement sets the correct Return Code.

0

Response Number 8
Name: xirsteon
Date: October 6, 2009 at 07:54:21 Pacific
Reply:

Thanks again IVO. I really appreciate it.

echo.default printer: !row:~1! >> printers.txt

Yes I want to map (connect) if the printer is identified by \\servername and don't want to connect if it's a local printer. For now I've this piece that checks for the default printer in the registry.

for /f "tokens=2 delims=Z," %%a in ('reg query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v "device"') do (
set row=%%a
set dp=!row:~1!
echo.!dp! >> printers.txt

)

I just need a logic in the above for loop to check if the printer is network (identified by \\servername) and if so, connect it and set it as default

and if it's a local printer, don't connect it, simply print a statement saying "your default printer is blah blah .

Do you how I can do that?

Thanks.


0

Response Number 9
Name: IVO
Date: October 6, 2009 at 08:30:48 Pacific
Reply:

Well, now the fog is vanishing. Be patient and I'll try to give you the code.

What about the If ErrorLevel?


0

Response Number 10
Name: xirsteon
Date: October 6, 2009 at 10:27:23 Pacific
Reply:

What about the If ErrorLevel?
what do you mean by "what about if ErrorLevel?"


I'm trying to integrate the "if not errorlevel 1" from your code above and see how it pans out..

but I still need to figure out the other piece to check whether to map (connect) a network printer if it's the default or just simply print a statement saying the default printer is local printer.


0

Response Number 11
Name: xirsteon
Date: October 6, 2009 at 12:18:01 Pacific
Reply:

Hey IVO,

I have this piece of code that supposingly should do what I wanted above: check to see if the default printer is a network printer, and if it is, connect and set it as the default. And if it is not, simply don't connect to it and just say your default printer is blah blah blah.

Problem is when I run this piece of code with cmdextensions enabled, and I know definitely that the printer is a network printer, the last echo line also gets printed. It is as if the ELSE clause is not in effect.

setlocal enableextensions
set dp=
for /f "tokens=2 delims=Z," %%a in ('reg query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v "device"') do (
set row=%%a
set dp=!row:~1!
echo.!dp! > defaultp.txt
REM type defaultp.txt
for /f "tokens=* delims=" %%a in ('findstr /b /c:\\ defaultp.txt') do (
echo.a:%%a
echo.default printer is a network printer &echo a_again:%%a
if /I "%%a"=="!dp!" (
echo.connecting %%a
rundll32 printui.dll,PrintUIEntry /in /y /n "%%a"
echo.default printer was set to: %%a
)ELSE (
echo.Your default printer is a local printer: !dp!
)
)
)

do you know what I might be doing wrong?

Also if the default printer was a local printer, it should skip right to the the line

echo.Your default printer is a local printer: !dp! in the code above right??

If I can get this piece above to work, then I can add your recommended check above to check for successful printer (network) connection right after the

rundll32 printui.dll,PrintUIEntry /in /y /n "%%a"

I can do it like this (like u suggested)
if not Errorlevel 1 (
echo.Printer %%a connected successfully.
)else (
echo.Printer %%B not found or turned off
)



0

Response Number 12
Name: IVO
Date: October 6, 2009 at 14:56:11 Pacific
Reply:

@echo off & setlocal EnableDelayedExpansion

echo.
for /F "tokens=2 delims=Z," %%a in ('reg query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v "device"') do (
  for %%z in (%%a) do set dp=!dp! %%z
  set dp=!dp:~1!
  if not "!dp!"=="!dp:\\=!" (
    echo.  Default printer "!dp!" is a network printer
    echo.  Connecting "!dp!"
    rundll32 printui.dll,PrintUIEntry /in /y /n "!dp!"
    if mot ErrorLevel 1 (
      echo.  Primter "!dp!" connected successfully
    ) else (
      echo.  Primter "!dp!" not found or turned off
    )
    echo.  Default printer was set to "!dp!"
  ) else (
    echo.  Your default printer "!dp!" is a local printer
  )
)
echo.


1

Response Number 13
Name: xirsteon
Date: October 6, 2009 at 20:06:43 Pacific
Reply:

Your code works. However, it is kind of broken for the purpose for what I want to use it for. Simply put, I want to read a simple text file i.e. defaultprinter.txt that has one line in it and then proceed to the rest of the code.

so say I backed up a user's desktop and then re-install the OS on the machine. And I later want to run the script to restore the user's default printer from the backed up defaultprinter.txt file which has the default printer in it.

Is it possible you can change the code a bit such that after getting the default printer in the registry, it's stored in a text file and then the text file is read(processed) by the second for loop ??

i.e. echo.%%a>default[rinter.txt
for %%z in (defaultprinter.txt) do set dp=!dp! %%z

Thanks.


0

Response Number 14
Name: IVO
Date: October 7, 2009 at 04:19:29 Pacific
Reply:

Here the script for your purpose (almost according to your latest post, as the requirements widely changed from the beginning of this thread).

If the file you setup in the variable "printer" does not exist the default printer is backed up, while if the file is found its content is restored setting the default printer connected if a network one.

Under my systems (Windows 2000/XP/Vista) that worked fine, but you have the lasst shot...

:: DEFPRT Usage: defprt
@echo off & setlocal EnableDelayedExpansion
echo.
:: Backup file PathName\FileName

set printer=defaultprinter.txt

:: Backup the default printer into %printer%

if not exist "%printer%" (
  for /F "tokens=2 delims=Z," %%a in ('reg query "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" /v "device"') do (
    for %%z in (%%a) do set dp=!dp! %%z
  )
  echo.!dp:~1!> "%printer%"
  echo.  Default printer "!dp:~1!" was backed-up into "%printer%"
  goto :EOF
)

:: Restore the default printer from %printer%

set /P dp=< "%printer%"

if not "!dp!"=="!dp:\\=!" (
  echo.  Default printer "!dp!" is a network printer
  echo.  Connecting "!dp!"
  rundll32 printui.dll,PrintUIEntry /in /y /n "!dp!"
  if not ErrorLevel 1 (
    echo.  Primter "!dp!" connected successfully
  ) else (
    echo.  Primter "!dp!" not found or turned off
  )
  echo.  Default printer was set to "!dp!"
) else (
  echo.  Your default printer "!dp!" is a local printer
)
echo.
:: End_Of_Batch


1

Response Number 15
Name: xirsteon
Date: October 7, 2009 at 08:23:07 Pacific
Reply:

IVO, I really appreciate all your help. It works exactly like I wanted it.

Very much appreciated.


0

Response Number 16
Name: IVO
Date: October 7, 2009 at 13:25:20 Pacific
Reply:

Glad you appreciated my script (that has a minor bug - edho instead of echo near the bottom).

Now the thread is closed for me; if you need more support or any unexpected bugs blow up, please contact me by a private message, clicking IVO.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: for /f tokens and delims help

Batch For /F delims=, bug? www.computing.net/answers/programming/batch-for-f-delims-bug-/16108.html

for /f tokens and wmic command www.computing.net/answers/programming/for-f-tokens-and-wmic-command/20176.html

Batch for loop tokens www.computing.net/answers/programming/batch-for-loop-tokens/16727.html