Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I translate the batch file from

rem working batchA.bat
PATH=%PATH%;C:\Program Files\gs\gs9.07\bin
PATH=%PATH%;C:\Program Files (x86)\Adobe\Reader 11.0\Reader
PATH=%PATH%;C:\Program Files (x86)\Google\Chrome\Application
PATH=%PATH%;C:\Program Files (x86)\cwRsync\bin
PATH=%PATH%;C:\Program Files (x86)\PDF Labs\PDFtk Server\bin

setx PATH "%PATH%" /m

to

rem NOT working batchB.bat
for /f "delims=" %%x in (List.txt) do (PATH=%PATH%;%%x)
setx PATH "%PATH%" /m

where List.txt contains the following.

C:\Program Files\gs\gs9.07\bin
C:\Program Files (x86)\Adobe\Reader 11.0\Reader
C:\Program Files (x86)\Google\Chrome\Application
C:\Program Files (x86)\cwRsync\bin
C:\Program Files (x86)\PDF Labs\PDFtk Server\bin

?

share|improve this question
    
Is doing it with PowerShell an option for you? –  Windos Mar 10 '13 at 20:49
    
@Windos: Unfortunately No. :-) –  Please don't touch Mar 10 '13 at 21:04
add comment

3 Answers

I have tried every way I can think of to implement this how you want to get it going, but keep running into road blocks like you have already seen.

There are a couple of options which aren't quite as nice to use, but will still get the job done:

1. Setting up the variables on one machine, exporting them and reusing them on another.

Adapted from how to set environment variable from file contents?

On a machine that has the paths setup already, run:

echo %PATH%> dat

This will create a file called dat which contains the full path from the reference machine.

You can then reapply them on a different machine by passing it into the command:

set /P PATH=< dat

It should be possible to edit dat such that it only contains the paths you want to append to a machine and modify the command like so:

set /P PATH=%path%;< dat

Note that I have not tested this modification.

Going forward you could add any changes to dat.

2. Using a third party app, pathed.exe

Using info from Edit the PATH environment variable in Windows without pain

Using Pathed, part of the gtools, you can append the path.

Assuming your list is List.txt and you're in the same directory as the list, the command would look like this:

for /f "delims=" %%x in (List.txt) do (pathed /append %%x /machine)

You'd also need pathed in your path or in the same directory you're working in.

2. Using PowerShell

You could also look into PowerShell, but you've already indicated this isn't an option (plus option 1 or 2 should be able to see you right.)

share|improve this answer
add comment

Try this instead:

setlocal enabledelayedexpansion
for /f "delims=" %%x in (List.txt) do (PATH=!PATH!;%%x)
setx PATH "!PATH!" /m
share|improve this answer
add comment

The problem that you are having is that the variables are expanded on the FOR line only once, so when you loop though it %PATH% is no longer a variable.

for /f "delims=" %%x in (List.txt) do (PATH=%PATH%;%%x)

Becomes this BEFORE the loop is even run:

for /f "delims=" %%x in (List.txt) do (PATH=C:\Windows;Blah;Blah;%%x)

You can solve this using delayed expansion as Karan said, but I've never had much luck with that. I always recommend a function when using for loops.

for /f "tokens=*" %%x in (List.txt) do call:loadpath "%%x"
setx PATH "%PATH%" /m

Exit /b 0

REM -------------------------

:loadpath
SET PATH=%PATH%;%~1
Exit /b 0

Notes: use "tokens=*" to get the whole line from the file. Double quote "%%x" when calling the function to place every word in %1. Use %~1 inside the function to strip the double quotes from the value.

share|improve this answer
    
Have you tried it? –  Please don't touch Mar 19 '13 at 0:56
    
sorry about that, I missed that your directories had spaces in them. –  batchprompt Mar 19 '13 at 13:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.