1

I'm using a for loop to acces a text file with a bunch of files + their directory formatted like this:

//srv/something/somethingelse/movie.mpg
//srv/something/somethingelse/movie2.mkv
//srv/something/somethingelse/movie3.mpg
//srv/something/somethingelse/movie4.mkv

I have to replace .mpg and .mkv with .xml, and then write that output away to another text file, which I'm trying to do like this:

for /F "tokens=*" %%A in (%~dp0temporary\movies.txt) do (
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 I want is this:

//srv/something/somethingelse/movie.xml
//srv/something/somethingelse/movie2.xml
//srv/something/somethingelse/movie3.xml
//srv/something/somethingelse/movie4.xml

But what I get is this:

Echo is off.
Echo is off.
Echo is off.
Echo is off.

I have been searching on this for over an hour but I can't find anything that works

  • @Mofi I've done that; but I still get the same output. I've updated the question with the new code I use – Warre Dujardin Jul 23 '15 at 15:01
  • You also need to change your variable-syntax. Take a look to the [delayed expansion trap](http://stackoverflow.com/a/30284028/2152082) – Stephan Jul 23 '15 at 15:08

2 Answers2

1

Here is the rewritten batch code which produces the expected output from input file.

@echo off
setlocal EnableDelayedExpansion
set "vidLoc=//srv"
set "resultLoc=c:"
del "%~dp0temporary\xml.txt" 2>nul
for /F "usebackq delims=" %%A in ("%~dp0temporary\movies.txt") do (
    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 are expanded already on parsing entire for block. Just the environment variable references enclosed in exclamation marks are expanded delayed on executing the command. This can be seen on opening a command prompt window and running from there the batch file without @echo off at top or with this line being changed to @echo on.

Executing in a command prompt window set /? results in getting help of this command output on several window pages where usage of delayed expansion for for and if blocks is explained on a simple example.

And running in a command prompt window for /? prints help of command for into the output window.

For just replacing the file extension you could also use:

@echo off
del "%~dp0temporary\xml.txt" 2>nul
for /F "usebackq delims=*" %%A in ("%~dp0temporary\movies.txt") do (
    echo %%~dpnA.xml>>"%~dp0temporary\xml.txt"
)

But this faster code changes also all forward slashes / to backslashes \ as the backslash character is the directory separator on Windows.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Another question, how can i use variables to find and replace something with your first code snippet? I need to find the content of the parameter %vidLoc% and replace it with the content of parameter %resultLoc% %vidLoc% will be on //srv and %resultLoc% will usually be on c:/, but sometimes on //srv also – Warre Dujardin Jul 23 '15 at 16:00
0

Mofi is right: move the line with setlocal enabledelayedexpansion out of any code block enclosed in (parentheses).
However, try next approach using Command Line arguments (Parameters) modifier ~:

@ECHO OFF >NUL
@SETLOCAL enableextensions 
for /F "tokens=*" %%A in (%~dp0temporary\movies.txt) do (
    rem full_path=%%~dpnA
    rem extension=%%~xA
    echo %%~dpnA.xml 
)>%~dp0temporary\xml.txt
JosefZ
  • 28,460
  • 5
  • 44
  • 83