for /f tokens and delims help

Dell Inspiron 11z notebook - customizabl...
October 2, 2009 at 10:46:23
Specs: Windows XP
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 \


See More: for /f tokens and delims help

Report •


#1
October 2, 2009 at 14:02:41
for /F "tokens=1,2 delims=\" %%a in ('findstr /b /c:\\ printers.txt') do echo.%%a %%b


Report •

#2
October 3, 2009 at 13:23:24
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?



Report •

#3
October 4, 2009 at 12:44:05
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!
)


Report •

Related Solutions

#4
October 5, 2009 at 07:38:28
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).



Report •

#5
October 5, 2009 at 08:51:02
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.


Report •

#6
October 5, 2009 at 10:19:27
..> 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.



Report •

#7
October 6, 2009 at 06:23:30
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.

Report •

#8
October 6, 2009 at 07:54:21
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.


Report •

#9
October 6, 2009 at 08:30:48
Well, now the fog is vanishing. Be patient and I'll try to give you the code.

What about the If ErrorLevel?


Report •

#10
October 6, 2009 at 10:27:23
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.


Report •

#11
October 6, 2009 at 12:18:01
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
)



Report •

#12
October 6, 2009 at 14:56:11
@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.


Report •

#13
October 6, 2009 at 20:06:43
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.


Report •

#14
October 7, 2009 at 04:19:29
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


Report •

#15
October 7, 2009 at 08:23:07
IVO, I really appreciate all your help. It works exactly like I wanted it.

Very much appreciated.


Report •

#16
October 7, 2009 at 13:25:20
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.


Report •


Ask Question