0

I am attempting to create a batch file that will rename a series of file based on an XML file. It works until it encounters a file name with the same name at which point it skips the file. I was able to amend the script to create an incremental version of the file, the issue that I am having now is the script will cycle through just a few files and then it exits. Any ideas why it's doing that?

for %%z in ("C:\Recordings\AT1*.WAV") do (
  for /f tokens^=4^,8^,10^,12^ delims^=^" %%a in ('type "C:\Recordings\index.xml"^|find /i "%%~nxz"') do (
    for /f "tokens=1,2 delims=:" %%t in ("%%b") do (
      ren "%%z" "%%c-%%t-%%u_%%d%%~xz" 2>nul
      if errorlevel 1 set "Number=2" & call :NumberedRename "%%z" "%%c_%%t-%%u_%%d%%~xz"
      goto :EOF
    )
  )
)
)

:NumberedRename
if exist "%~n2_%Number%%~x2" set /A "Number+=1" & goto NumberedRename
    ren %1 "%~n2_%Number%%~x2"
    goto :EOF

Here is an example of the output that I am able to see at the moment:

C:\Recordings>for %z in ("C:\Recordings\AT1*.WAV") do (for /F tokens=4,8,10,12 delims=" %a in ('type "C:\Recordings\index.xml"|find /i "%~nxz"') do (for /F "tokens=1,2 delims=:" %t in ("%b") do (
ren "%z" "%c-%t-%u_%d%~xz"  2>nul
 if errorlevel 1 set "Number=2"   & call :NumberedRename "%z" "%c_%t-%u_%d%~xz"
 goto :EOF
) ) )

C:\Recordings>(for /F tokens=4,8,10,12 delims=" %a in ('type "C:\Recordings\index.xml"|find /i "AT1_ID1_TT3_ID6-1626034093.52156.WAV"') do (for /F "tokens=1,2 delims=:" %t in ("%b") do (
ren "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "%c-%t-%u_%d.WAV"  2>nul
 if errorlevel 1 set "Number=2"   & call :NumberedRename "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "%c_%t-%u_%d.WAV"
 goto :EOF

C:\Recordings>if exist "John Doe 1_2021-07-11 15-08_9394056960_2.WAV" set /A "Number+=1"   & goto NumberedRename

C:\Recordings>ren "C:\Recordings\AT1_ID1_TT3_ID6-1626034093.52156.WAV" "John Doe 1_2021-07-11 15-08_9394056960_2.WAV"
) ) )
aschipfl
  • 33,626
  • 12
  • 54
  • 99
user1664305
  • 181
  • 7
  • 18
  • 1
    what do you think, `goto :EOF` does within a loop? – Stephan Jul 12 '21 at 19:51
  • With my limited knowledge of scripting, I wasn't sure what `goto :EOF` did until it dawned on me that it stands for End of File. Shortly after I posted this script I removed the first mention of EOF and ran a test. It seems to work flawlessly at this point. For additional reference, the original question that I asked elsewhere, advised placing the `EOF` where I did, at least from what I could tell of the comment that was made. – user1664305 Jul 12 '21 at 20:03
  • 1
    Sometimes, you *want* to break a loop. Then `goto` is the way to go. (unlike other languages, batch hasn't a way to break just an inner loop in a nested loop structure, though. Every `goto` breaks *any* loop) – Stephan Jul 12 '21 at 21:08
  • @user1664305 I wrote in my comment on your previous question [Batch script rename .wav files if duplicate exists append a number](https://stackoverflow.com/questions/68329604/): "... and __append__ to the batch file `goto :EOF` to avoid a fall through to the subroutine defined next with ...". Look on your updated batch file code. Is `goto :EOF` appended to the batch file or have you inserted this command inside the batch file? You have it inserted inside the batch file and have not appended it at end after last `)` closing the command block of most outer __FOR__ loop. – Mofi Jul 13 '21 at 17:02

1 Answers1

0

Shortly after posting the question I tinkered with the script and realized that one of the goto :EOF was causing the script to end prematurely. I removed the goto :EOF and it seems to be working as intended.

Updated Script

for %%z in ("C:\Recordings\AT1*.WAV") do (
  for /f tokens^=4^,8^,10^,12^ delims^=^" %%a in ('type "C:\Recordings\index.xml"^|find /i "%%~nxz"') do (
    for /f "tokens=1,2 delims=:" %%t in ("%%b") do (
      ren "%%z" "%%c-%%t-%%u_%%d%%~xz" 2>nul
      if errorlevel 1 set "Number=2" & call :NumberedRename "%%z" "%%c_%%t-%%u_%%d%%~xz"

    )
  )
)
)

:NumberedRename
if exist "%~n2_%Number%%~x2" set /A "Number+=1" & goto NumberedRename
    ren %1 "%~n2_%Number%%~x2"
    goto :EOF
user1664305
  • 181
  • 7
  • 18
  • The complete removal of first `goto :EOF` is not the correct solution. The correct solution would be to insert first `goto :EOF` above the line `:NumberedRename`. See also: [Where does GOTO :EOF return to?](https://stackoverflow.com/a/37518000/3074564) You should remove also the indents of the last two lines as there are not part of the __IF__ condition above. Finally there is one `)` too much in your code. See also: [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) which explains the operator `&`. – Mofi Jul 15 '21 at 06:39