user3355987
user3355987

Reputation: 23

Batch file that keeps the x latest files in a specific folder

How can you specify a specific folder? for instance in "c:\temp with spaces"?

this command works, but only in the same folder as where it is executed:

for /f "skip=7 eol=: delims=" %%F in ('dir /b /o-d *.txt') do @del "%%F"

But how can i specify a certain folder? This is not working:

for /f "skip=3 eol=: delims=" %%F in ('dir /b /o-d "c:\temp\ with spaces*.txt"') do @del "%%F"

it seeks for the txt files in the current directory, and if the .bat file is in another folder, it does not work

help would be greatly appreciated

Upvotes: 0

Views: 475

Answers (2)

MC ND
MC ND

Reputation: 70923

Just change to the desired folder

pushd "x:\some folder\with\files" && (
    for /f "skip=7 eol=: delims=" %%F in ('dir /b /o-d *.txt') do @del "%%F"
    popd
)

Upvotes: 1

woxxom
woxxom

Reputation: 73526

As you can see by running manually dir /b without specifying /s lists only the filenames without the full path so @del "%%F" tries to remove the file from current folder.

Simply specify the full path in del too:

@del "c:\temp\%%F"

Upvotes: 1

Related Questions