We help IT Professionals succeed at work.

forfiles in batch

1,018 Views
Last Modified: 2012-05-08
FORFILES /s  /d -1 /c "CMD /C del /Q /F @FILE"

I have multilpe files in this folder and it will not delete them.  what's up?

I called it forfiles.bat
Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Try this (no space after /d).

FORFILES /s /d-1 /c "CMD /C del /Q /F @FILE"

~bp
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Actually, maybe that's not it.  I would recommend that you run it with ECHO in front of your DEL though, until you dial in the right syntax.  I think the version of ForFiles I have may be different than yours, the header when I type in forfiles alone the header says:

FORFILES v 1.1 - by Emmanuel Boersma - 4/98

~bp

Author

Commented:
No, that didn't work.  Maybe I should explain what I'm trying to do:
delete the files in this folder that are older than 1 day.  Actually, deleting any files in this folder would be nice.  XP box - I have admin control.  could be used on a 2003 in the future.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Okay, I have the Win2003 version now, and your command seemed to work.  I tried the following on my system and it returned files.  Try changing your DEL to an ECHO first just to see if it finds the files.  If it does, try the DEL without the /Q so that you will see any errors it might get.  Could this be a permission problem?

FORFILES /s  /d -1 /c "CMD /C echo @FILE"

~bp

Author

Commented:
Well, I'm on my home computer.  How do you find out if you have forfiles working and if it is not, how could I get it to work?  Is there a special update I should install?  Is there some configuration I need to do?  No, the echo does not echo

FORFILES /s /d-1 /c "CMD /C ECHO @FILE"  

nor did
forfiles /s /m *.* /d -1 /c "cmd /c echo @file is at least one year old."

for /f %%a in ('forfiles /D-1') do del %%a  
- this did not work
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Finally got back to it ... it works from the command line but not in a batch file:  forfiles.bat

forfiles /p c:\backup /s /d -7 /c "CMD /C echo @FILE"

is there anything I could change to get it to work from a bat file...

Sorry for being dense - just got lots of temp files to delete from some crazy program that leaves its garbage lying around.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
It works in a BAT file here.

Do you get any errors?

Also, if you actually did name the batch file FORFILES.BAT, change the name to something else.  Naming a batch file the same as an executable can often cause problems.

Is the FORFILES.EXE in your PATH?

~bp

Author

Commented:
yup - can't name it the same as the command - even with the bat on it
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Does that mean by renaming it, it worked (just want to be sure).

~bp
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.