4

I'm successfully splitting %PATH% into multiple lines like this

set t=%PATH:;=^&echo.%

then this displays each path in new line nicely, just as I want:

echo %t%

However when I want to write variable into a file

echo %t% >paths.txt

only last line is written to the file.

What am I doing wrong?

Update

Turns out the set t=%PATH:;=^&echo.% command does not replace ; characters with line breaks (as I was told) but instead replaces it with the &echo.‌​ command, which is later executed.

2 Answers 2

4

What am I doing wrong?

You need to surround the last echo with ( and )

(echo %t%) > paths.txt

Corrected batch file (test.cmd):

@echo off
setlocal
set t=%PATH:;=^&echo.%
echo %t%
(echo %t%) > paths.txt
:endendlocal

Example usage:

> test
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\apps\WSCC\Sysinternals Suite
C:\apps\WSCC\NirSoft Utilities
C:\apps\Calibre\
C:\apps\Git\cmd
C:\apps\Git\mingw64\bin
C:\apps\Git\usr\bin
C:\apps\nodejs\
C:\Users\DavidPostill\AppData\Roaming\npm
> type paths.txt
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\apps\WSCC\Sysinternals Suite
C:\apps\WSCC\NirSoft Utilities
C:\apps\Calibre\
C:\apps\Git\cmd
C:\apps\Git\mingw64\bin
C:\apps\Git\usr\bin
C:\apps\nodejs\
C:\Users\DavidPostill\AppData\Roaming\npm

A simpler solution

This solution does not require any brackets in the path to be escaped.

test.cmd:

@echo off
setlocal
for %%i in ("%path:;=";"%") do (
  echo %%~i >> paths.txt
)
:endendlocal

And from the command line:

for %i in ("%path:;=";"%") do echo %~i >> paths.txt

Further Reading

8
  • I've already tied this. I got At this moment \Java\jdk1.6.0_39\bin\ was unexpected (thought it's said in Polish and I'm not sure how original message looks like). I have `C:\Program Files (x86)\Java\jdk1.6.0_39\bin` on 3 line in the variable if that helps. Mar 24, 2017 at 18:03
  • Let me know If i understand it right. Dose my variable %t% stores actual echo. commends in itself? that are then tied to be executed? Because I thought my %t% variable simply stores a single string that is written to the file... Mar 24, 2017 at 18:17
  • It stores echo commands in the string. The string looks like C:\Windows\system32&echo.C:\Windows&echo.C:\Windows\System32\Wbem&echo.C:\Windows\System32\WindowsPowerShell\v1.0\&echo.C:\apps\WSCC\Sysinternals Suite&echo.C:\apps\WSCC\NirSoft Utilities&echo.C:\apps\Calibre\&echo.C:\apps\Git\cmd&echo.C:\apps\Git\mingw64\bin&echo.C:\apps\Git\usr\bin&echo.C:\apps\nodejs\&echo.C:\Users\DavidPostill\AppData\Roaming\npm in my case.
    – DavidPostill
    Mar 24, 2017 at 18:26
  • 1
    @PawełAudionysos I've added a much simpler solution to the answer, which does not require any brackets in the path to be escaped
    – DavidPostill
    Mar 24, 2017 at 18:35
  • 1
    @PawełAudionysos I've added a command line solution.
    – DavidPostill
    Mar 24, 2017 at 19:04
0

Yours t should not store special control characters as a part of path like ", &, |, % and so on. If so, then you can just replace the ending ) character having interference with the parser:

(echo %t:)=^)%) > a

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .