3

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.

4

1 回答 1

2

您需要在批处理文件中使用 %%A。我将您的原始批处理文件代码更改为键入而不是删除

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

因为我不想删除我的文件。

于 2013-09-23T18:34:13.557 回答