FOR loop question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Scott1710
Posts: 4
Joined: 28 Aug 2012 22:42

FOR loop question

#1 Post by Scott1710 » 28 Aug 2012 22:50

Hi Guys,

I'm new here as you can see, but I've been searching around the net for a while now trying to figure this out without any luck, so I'm hoping somebody here can help me out.

I have the following script:

Code: Select all

setlocal enabledelayedexpansion

set Dupe=1

for /R C:\ %%F in (*.mp3, *.m4a) do (
  :timer
  if exist "E:\Temp1\%%~nF-%Dupe%%%~xF" (
    set /a Dupe=Dupe+1
    goto timer
    ) else (copy "%%~fF" "E:\Temp1\%%~nF-%Dupe%%%~xF")
  )
pause


Which is supposed to copy all the MP3 and M4A files from my C drive to my E drive, renaming duplicates as it goes.
However, for some reason I've got an issue with it; If it DOES encounter a duplicate, it fails.

Hope somebody can help me out.

Thanks,


Scott

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FOR loop question

#2 Post by foxidrive » 28 Aug 2012 23:24

The first issue is to use !dupe! rather than %dupe% with delayed expansion.

Scott1710
Posts: 4
Joined: 28 Aug 2012 22:42

Re: FOR loop question

#3 Post by Scott1710 » 28 Aug 2012 23:30

I've tried that, but it makes no difference to the script whatsoever.
I think my problem is trying to process labels within a for loop, which doesn't work, but I can't find a way around that.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FOR loop question

#4 Post by foxidrive » 28 Aug 2012 23:36

Your code will skip files. I think this is more robust and avoids problems with ! characters but other poison characters will still be an issue, mainly % characters.

Code: Select all

@echo off
set Dupe=1
for /R C:\ %%a in (*.mp3 *.m4a) do call :next "%%~fa"
echo done
pause
goto :EOF
:next
echo copying "%~1"
if not exist "E:\Temp1\%~nx1" copy /b "%~1" "E:\Temp1" & goto :EOF
:loop
if not exist "E:\Temp1\%~n1-%Dupe%%~x1" copy /b "%~1" "E:\Temp1\%~n1-%Dupe%%~x1" & goto :EOF
set /a Dupe=Dupe+1
goto :loop

Scott1710
Posts: 4
Joined: 28 Aug 2012 22:42

Re: FOR loop question

#5 Post by Scott1710 » 28 Aug 2012 23:42

This seems to do nothing at all.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FOR loop question

#6 Post by foxidrive » 28 Aug 2012 23:51

It works here.

Try copy/pasting it again. There was a typo there for 1/2 min and you may have the previous version.

Scott1710
Posts: 4
Joined: 28 Aug 2012 22:42

Re: FOR loop question

#7 Post by Scott1710 » 28 Aug 2012 23:56

That works perfectly, thank you.
I'm not too concerned about missing the odd file if they have bad characters in them - this is really just for grabbing large numbers of not-too-important files quickly and easily.

Thanks for all of your help.
:)

Post Reply