Solved

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

Posted on 2003-10-21
6
1,078 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:akourafas
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:_nn_
ID: 9593826
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

0
 
LVL 1

Author Comment

by:akourafas
ID: 9594088
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
0
 
LVL 1

Author Comment

by:akourafas
ID: 9594103
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
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 16

Expert Comment

by:_nn_
ID: 9594112
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

0
 
LVL 16

Accepted Solution

by:
_nn_ earned 200 total points
ID: 9594174
grmbl, the syntax looks correct, but it doesn't want to work here, sorry.

Alternative :

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

set TSPrinterName=%prt:~7,-1% %TSSID%
0
 
LVL 8

Assisted Solution

by:K_2K
K_2K earned 50 total points
ID: 9600025
_nn_,
Nice job, That should work fine, likely stripping the 3 off the end.  
%prt:~7,-1%   says "skip 7 letters and use all but the last one"  
%prt:~7%       says "skip 7 letters and use the rest"  
Unfortunately, my preferred "skip 7 letters and use all but the last zero" (:~7,-0) returns a null or empty string so I can't explicitly state the -0 at the end.


akourafas,
I only dropped this "why" post to help with future for loops, not trying to make any of it look "acceptable" for the question _nn_ has already answered.

I think the key to solving these in future for loops is to study the ["options"] and examples in "for /?" as well as studying output lines for all possible values of a command you wish to split.
I knew UNIX FOREACH and AWK before, and when I first looked at MS-DOS's FOR all I could say was "What were they (MS) thinking?"  Reading the 3 or 4 paragraphs after each example in the help while looking back at the example to see what option accomplishes each explaination is a good way to begin understanding what they were thinking.  Luckily we don't have to agree or defend what they setup, we only wish to learn to use it.

Delims=  is one commonly misunderstood option.  MS's help line: "This replaces the default delimiter set of space and tab." is so guardedly correct it becomes unclear.  If the option is left out any number or combination of spaces or tabs will separate fields.
If included, nothing but what you put after = separates fields.  However, it's not always easy to know if FOR will think you meant to include a space unless you always place delims= as the last option.  (Notice it's not what I think that matters, it's what FOR thinks.)  That way everything between the = and the " including a space or another = or an escaped special character becomes a field separator.   Field separators do not get included in fields. ( exception: If an * is included in tokens= then the last variable set by FOR may include separators, but let's not go there at this time.  Leaving * out of all options is best if possible)
So:
  "tokens=2 delims=, " 
Will break the line at all spaces and commas.  Hence the value you have with spaces in it is broken to several fields.  Since the printer name could be changed by any one person for their own taste it's not possible to know how many fields, in reality.
  "tokens=2 delims=,="
Will break the line at all commas and equal signs and spaces no longer matter.  (sidenote: "tokens=2 delims==," means the same thing, but other places in DOS give == special meaning, so I try to write it as above to avoid confusion.  The DOS example that uses ONLY = as a delimeter was needed but not well explained.  It was NOT a special meaning of == but a normal delimeter.)

Once the delimeters and fields can easily be counted, tokens become easier to choose and variables become predictable.  Tokens= is mostly well understood except that the default is first field ONLY and blank fields don't count.  Some say that can't be since I have many code clippets that take the whole line with the tokens option omitted.  Such codes include "delims=" which replace default delimiters with no delimiters.  That forces the line to be all one field.  The default token of first field only now works.  Some code does not include "delims=" and seems to be the entire line because all lines tested have no tabs or spaces.  In reality it is only using the first field and someday that code may lose parts of some lines if the data changes.


From _nn_'s fix, I'm guessing the line from his REGfree.EXE is much more friendly than MS's REG.EXE gives, looking something like this:
     Device=HP Laserjet 4000 Series/BOB/Session 3,winspool,TS007
If we know by REGfree there will always be an = between valuename and data (i don't know REGfree) then = would seem to be a good separator.
I do know the windows printer setup routines refuse to save a printer name that includes a comma so that also makes a good separator. (You already knew that)  I just changed my generic printer (Windows 2000) to "Generic / Text Only = to file" to see if I could force "tokens=2 delims=,=" to break and it worked (didn't work?).  
We could separate with = only and abuse the * special case to get all but the first = into the second field using "tokens=1* delims==" %%i  but then %%j becomes the part we need to keep, AND we must still break the fields by commas separately with another added for:  for /f "delims=," %%K in ("%%j") do set prt=%%K

Anyway, _nn_'s simple solution of using comma ONLY as a separator forcing first token to always become the same 7 letter skippable string (we know the valuename is always the same in this case) plus the correct value is faster and more elegant.  As I said, I only dropped this "why" post to ...

Enjoy,
2K
(\o/)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Command-line Script 15 75
Batch File Bulk File Rename 3 59
MailSend Attachments 41 176
how to get the largest file size? 33 59
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question