Matt
Matt

Reputation: 33

can't execute (Error 2) when using forfiles

I wanted a one liner to delete all files older than 14 days in certain folder.

This is my command

forfiles -px:\logs -s -m*.log -d-14 -c"CMD /C del @file"

but when executed it outputs "can't execute (error 2)" for every file it finds. If the -c "CMD /c del @file " is omitted it works fine producing a list of files older than 14 days. I first thought it might be a permission issue; but any action on the file produces the same result and I am admin on the comptuer.

It is however a network share if that makes a diffrence.

Thanks

Upvotes: 3

Views: 4929

Answers (3)

Sun
Sun

Reputation: 2715

I am using FORFILES v 1.1 from April 1998.

In my situation, if the @PATH or @FILE contains a path with a space in it, this will not be handled correctly. Paths with spaces require double quotes around it. Since the executing command in forfiles requires double quotes, this is what I use in my batch files. The 0x22 gets saved to batch file as double quotes. I find that explicitly surrounding the path and file with double quotes make for reliable processing of files.

forfiles -pC:\pictures\ -m*.jpg -d-30 -c"cmd /c echo del 0x22@PATH\@FILE0x22" > c:\temp\delold_pics.bat

call c:\temp\delold_pics.bat

Upvotes: 0

Kausty
Kausty

Reputation: 859

I found the answer to this:

set DP_DIR=C:\Scripts

forfiles -p%DP_DIR% -s -m*.exe -d-3 -c"cmd /c del \"@FILE""

This works fine.

Upvotes: 1

weloytty
weloytty

Reputation: 6098

What is the value of @file, and what is the current directory when you're running the code?

winerror.h says that error 2 is

#define ERROR_FILE_NOT_FOUND             2L

so I would try fully qualifying your path to the file that is getting deleted.

--EDIT:

which you can do by doing

forfiles -px:\logs -s -m*.log -d-14 -c"CMD /C del @path"

instead of

forfiles -px:\logs -s -m*.log -d-14 -c"CMD /C del @file"

Upvotes: 1

Related Questions