Maximilian Sarte
Maximilian Sarte

Reputation: 51

Command runs in commandline but not in a BAT file

I have a one liner batch command that sorts files by date and then deletes everything but the last 10. This command runs just fine when I run it in a CMD window. However, when I place it in a BAT file, I get errors.

Command (works OK in CMD window):

for /f "skip=10 delims=" %A in ('dir /a:-d /b /o:-d /t:c *.jpg ^2^>nul') do del %A

Errors I get if trying to run it in a batch file:

Q:\Testbk>test1
-d was unexpected at this time.

Q:\Testbk>for /f "skip=10 delims=" -d /b /o:-d /t:c *.jpg ^2^>nul") do del A

Any idea as to how to fix it to run in a BAT file would be very much appreciated.

Upvotes: 3

Views: 1406

Answers (1)

dmm
dmm

Reputation: 495

You need %%A in the batch file. I changed your original batch-file code to type rather than delete

for /f "skip=4 delims=" %%A in ('dir /a:-d /b /o:-d /t:c *.jpg 2^>nul') do type "%%A"

because I didn't want to delete my files.

Upvotes: 2

Related Questions