windows - unable to use for loop output to set variable in batch -
i'm using loop acces text file bunch of files + directory formatted this:
//srv/something/somethingelse/movie.mpg //srv/something/somethingelse/movie2.mkv //srv/something/somethingelse/movie3.mpg //srv/something/somethingelse/movie4.mkv
i have replace .mpg , .mkv .xml, , write output away text file, i'm trying this:
for /f "tokens=*" %%a in (%~dp0temporary\movies.txt) ( set string=%%a set find=.mkv set replace=.xml set string=%%string:!find!=!replace!%% set find=.mpg set string=%%string:!find!=!replace!%% echo %string%>>%~dp0temporary\xml.txt )
the output want this:
//srv/something/somethingelse/movie.xml //srv/something/somethingelse/movie2.xml //srv/something/somethingelse/movie3.xml //srv/something/somethingelse/movie4.xml
but this:
echo off. echo off. echo off. echo off.
i have been searching on over hour can't find works
here rewritten batch code produces expected output input file.
@echo off setlocal enabledelayedexpansion set "vidloc=//srv" set "resultloc=c:" del "%~dp0temporary\xml.txt" 2>nul /f "usebackq delims=" %%a in ("%~dp0temporary\movies.txt") ( set "filenamewithpath=%%a" set "filenamewithpath=!filenamewithpath:.mkv=.xml!" set "filenamewithpath=!filenamewithpath:.mpg=.xml!" set "filenamewithpath=!filenamewithpath:%vidloc%=%resultloc%!" echo !filenamewithpath!>>"%~dp0temporary\xml.txt" ) endlocal
all environment variable references enclosed in percent signs expanded on parsing entire for block. environment variable references enclosed in exclamation marks expanded delayed on executing command. can seen on opening command prompt window , running there batch file without @echo off
@ top or line being changed @echo on
.
executing in command prompt window set /?
results in getting of command output on several window pages usage of delayed expansion for , if blocks explained on simple example.
and running in command prompt window for /?
prints of command for output window.
for replacing file extension use:
@echo off del "%~dp0temporary\xml.txt" 2>nul /f "usebackq delims=*" %%a in ("%~dp0temporary\movies.txt") ( echo %%~dpna.xml>>"%~dp0temporary\xml.txt" )
but faster code changes forward slashes /
backslashes \
backslash character directory separator on windows.
Comments
Post a Comment