Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't execute (Error 2) when using forfiles

Tags:

batch-file

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

like image 460
Matt Avatar asked Jul 09 '26 01:07

Matt


1 Answers

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"
like image 183
weloytty Avatar answered Jul 11 '26 21:07

weloytty



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!