Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 358
Maximilian Sarte Avatar asked Dec 10 '25 05:12

Maximilian Sarte


1 Answers

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.

like image 112
dmm Avatar answered Dec 12 '25 17:12

dmm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!