Link to home
Start Free TrialLog in
Avatar of akourafas
akourafas

asked on

How do I use the FOR /f statement to remove unwanted characters and set an enviroment variable

Step1
for /f "eol=; tokens=6 delims=\ " %%i in ("%TEMP%") do set TSSID=%%i
(This works properly & TSSID= the users terminal ID fine)

Test1
'REGfree.EXE -listvalue "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device
(this generates the value "HP Laserjet 4000 Series/BOB/Session 3,winspool,TS007)

Step2
for /f "Tokens=2 Delims=, " %%i in ('REGfree.EXE -listvalue "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"') do set prt=%%i
(this generatesthe value "HP 3")  Thats not what I was trying to do :-)

I would like to retrive the "HP Laserjet 4000 Series/BOB/Session 3" portion of the query
Avatar of _nn_
_nn_

I'm not sure where the "HP 3" comes from, but could you test removing the space you have in the delims clause ?

for /f "tokens=2 delims=," %%i in ('REGfree.EXE -listvalue "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"') do set prt=%%i

Avatar of akourafas

ASKER

When i run this:
'REGfree.EXE -listvalue "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device

I get:
REG_SZ HP Laserjet 4000 Series/RKM/Session 3,winspool,TS007
when trying to get:
HP Laserjet 4000 Series/RKM/Session 3

this part: "For /f "Tokens=2........."
extracted th HP

The number 3 was the script value extracted using:
for /f "eol=; tokens=6 delims=\ " %%i in ("%TEMP%") do set TSSID=%%i

Thanks for the quick response
Heres the script:

net use lpt1: /delete /y

for /f "eol=; tokens=6 delims=\ " %%i in ("%TEMP%") do set TSSID=%%i

for /f "Tokens=2 Delims=, " %%i in ('REGfree.EXE -listvalue "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"') do set prt=%%i

set TSPrinterName=%prt% %TSSID%
rundll32 printui.dll,PrintUIEntry /Xs /n "%TSPrinterName%" sharename "TSPrinter%TSSID%" attributes +Shared

net use lpt1: \\127.0.0.1\TSPrinter%TSSID% /y

:end
aah, ok. In which case, I'd do it like this :

for /f "tokens=2* eol=," %%i in ('REGfree.EXE -listvalue "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"') do set prt=%%i

ASKER CERTIFIED SOLUTION
Avatar of _nn_
_nn_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial