Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch file stopped working.

Status
Not open for further replies.

cycledick

Technical User
Oct 6, 2005
141
US
So, here is my simple batch file for backing up my files onto a USB drive (drive F:). It checks to see how many folders there are and if there are more than 18, it deletes the oldest ones, regardless of how many more than 18 there are. Although, when it's working properly, there is never more than one to delete, but I have tested it by creating say 25 folders, and it will delete all but 18. Then it creates a new directory with the current date and copies all my files into it. The batch file was working just fine, every day, for months. But, now it doesn't.

I get an error that says "%%a was unexpected at this time." I do know it's the first "%%a" after the "skip=18 tokens=* delims= " section that it's complaining about, but I don't know why, especially considering it was working for so long.

Anyone have any ideas? Thanks in advance!

--------------------------Batch File--------------------------

for /f "skip=18 tokens=* delims= " %%a in ('dir F:\ /ad /b /o-d') do (rmdir /s /q F:\%%a)
rmdir /s /q F:\$RECYCLE.BIN
xcopy /C /E /H /I /K /R /Y C:\Files F:\%date:~-4,4%"-"%date:~-10,2%"-"%date:~-7,2%
 
I just figured it out. It now works with just %a instead of %%a. But, I don't understand why. Anyone?
 
Ok, I lied. It only kind of works. If I run the first command from a command prompt, it works. If I try and run the batch file, it does not work. It give me an error that says "\ was unexpected at this time." WTF?
 
Okay, I give up. I just changed it back to what is was originally ("%%a" in that first line), and now the batch file works perfectly again. I'm annoyed.

Oh, and I found out ('dir F:\ /ad /b /o-d') should have read ('dir F:\ /ad /b /o-n') because it was sorting by creation date, not the actual name. I understand they should technically be the same, but for testing purposes it definitely didn't work when I made 10 folders all at once. It considered those the newest even though they had older names.
 
C:\>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable.
Variable names are case sensitive, so %i is different
from %I.
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top