1.

Solve : Batch 'For' Problem?

Answer»

Hi everyone,

I'm trying to do the following as I'm about to roll out a couple of different machines in my office. Each new machine will copy its appropriate drivers to a folder called c:\windows\support\drivers based on what's in it. That part already works.
Now there's a couple of folders in that Drivers folder. I'd like to expand the registry key at HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion called DevicePath to include all the folder contained in c:\windows\support\drivers. This should install all hardware by plug and play when the Sysprep machine image comes up.

I've got this so far:

dir /ad /b c:\windows\support\drivers > c:\windows\support\finishing\drivers.txt
set drvpath=%systemroot%\inf
setlocal ENABLEDELAYEDEXPANSION
   for /f "tokens=1" %%X in ( c:\windows\support\finishing\drivers.txt
            ) do (
               set drvpath=!drvpath!;c:\windows\support\%%x
               )
endlocal
reg add HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion /v DevicePath /t REG_EXPAND_SZ /d %drvpath% /f


But this doesn't do me any good as it sets the key to \inf. I'm stumped and can't work out what I'm doing wrong.
Any help greatly aprreciated.for the 'for' loop to read text from a file it needs to either use back quotes or translated into a COMMAND, comme sa:

Code: [Select]  for /f "tokens=1" %%x in ('type c:\windows\support\finishing\drivers.txt') do (
set drvpath=!drvpath!;c:\windows\support\%%x)
or

Code: [Select]  for /f "tokens=1 usebackq" %%x in (`c:\windows\support\finishing\drivers.txt`) do (
set drvpath=!drvpath!;c:\windows\support\%%x)
FBTried both, result is both times that DevicePath is set to \inf.

When I do the dir /ad /b I get a list of folders, each line one folder. Is it easier to replace the line breaks with semicolons?

Thanks for the quick reply btw.why are you using "tokens=1"?
I was under the assumption I had to state what I want taken / read from the .txt file. Wrong? Quote from: BamBam71 on January 24, 2009, 08:43:14 AM

I was under the assumption I had to state what I want taken / read from the .txt file. Wrong?

Depends. If you want the whole line, use "tokens="

Tried it, doesn't make a difference. Have you tried leaving echo on and watching to see how the variables expand?
here's the output:

dir /ad /b d:\*.*  1>d:\temp\test.txt

set drvpath=C:\Windows\inf

setlocal ENABLEDELAYEDEXPANSION

for /F "tokens=" %x in (d:\temp\test.txt) do (set drvpath=!drvpath!;c:\windows\s
upport\drivers\%x )

endlocal

del d:\temp\test.txt

set drvpath
drvpath=C:\Windows\inf

As you can see they don't expand at all 

I'm not bound to that way either. If one of you can point me in the right direction I'm just trying to accomplish this:
list of folders > registry key in the form of folder1;folder2;folder3 and so on.1. Move Setlocal Enabledelayedexpansion to before you set drvpath.
2. Remove Endlocal.
3. The "tokens=1" ensures that if a line in the text file has spaces, you will only capture up to the FIRST space.




THANKS A LOT FOR YOUR HELP!!!

This is how it works in the end:

setlocal ENABLEDELAYEDEXPANSION

dir /ad /b d:\vms > d:\temp\test.txt
set drvpath=%systemroot%\inf
for /f "tokens=*" %%x in ( d:\temp\test.txt
            ) do (
               set drvpath=!drvpath!;c:\windows\support\drivers\%%x
               )
set drvpath


Quote
set drvpath

this is more usual

Code: [Select]echo %drvpath%
But that's just a stylistic thing.

Glad it works.

By the way, you do know that you can avoid using a temp file?

Use for /f and enclose a command in single quotes, and the command output will be PARSED, line by line, as if it was in a file.

And put the loop on one line?

Code: [Select]setlocal enabledelayedexpansion
set drvpath=%systemroot%\inf
for /f "tokens=*" %%x in ('dir /ad /b d:\vms') do set drvpath=!drvpath!;c:\windows\support\drivers\%%x
echo drvpath=%drvpath%


Discussion

No Comment Found