Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
another delete folders by date

Reply  
Thread Tools
Nnyan's Avatar
Junior Member with 11 posts.
 
Join Date: Aug 2000
07-Aug-2009, 03:58 PM #1
another delete folders by date
Hello all,

Noob here and to start off I'll say that I've search these forums and I have run into a few posts about this topic (search function kept giving error) but nothing that answers my question exactly.

I'm currently running some batch scripts in windows command prompt that delete files that are older the X date using FOREFILES (EX):

forfiles /P "PATH TO FILES" /M *.txt /D -7 /C "CMD /C if @ISDIR==FALSE echo del @FILE &del @FILE"

This works great for files but I need something simple like this for directories. I don't need exact math so if it's a day or so out of whack no big deal. I just can't seem to get anything like this to work for folders.

Any ideas?

Thank you in advance.
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
07-Aug-2009, 10:01 PM #2
Code:
ForFiles /P "PATH TO FILES"  /D -7 /C "CMD /C if @ISDIR==TRUE echo RD /Q /S @FILE &RD /Q /S @FILE"
Note that this will delete the contents of all folders and all subfolders in the Specified Path. Remove the /Q to be prompted for each folder.
If the folders are empty, the /S and /Q is not needed. Leave those switches off to avoid deleting folders that are not empty.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

Nnyan's Avatar
Junior Member with 11 posts.
 
Join Date: Aug 2000
10-Aug-2009, 12:03 PM #3
awesome I'll give this a try and report back, that's exactly what I was looking for! Thank you!!!
Nnyan's Avatar
Junior Member with 11 posts.
 
Join Date: Aug 2000
10-Aug-2009, 12:22 PM #4
works perfectly. Forgot one thing in my orignal post. Is there anyway that I can use forefiles to do a conditional check? What I mean is IF there are files that are more then (ex) 7 days old do one thing (proceed to delete them) if there are no files 7 days or older then do something else (like GOTO :NOFILES)?

Thank you very much for your time!
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
10-Aug-2009, 09:58 PM #5
You might be able to just check the ERRORLEVEL after running the Forfiles command. If no files meet the criteria, it sets ERRORLEVEL to 1 and displays this message:
ERROR: No files found with the specified search criteria.

I don't know what other situations might set the ERRORLEVEL to 1 (or any other value) though, so it might not always work.

I tried an illegal command by renaming a file with a : in the name.
It displays Access Denied, But ERRORLEVEL is set to 0, so it may only get set to 1 if no files are found.

So this might work. The 2>Nul prevents the Forfiles error message from being displayed:
Code:
@Echo Off
SetLocal
Set _Path=C:\tstfldr1
ForFiles /P "%_Path%" /D -7 /C "%ComSpec% /C If @ISDIR==FALSE Echo Del @File & Del @File" 2>Nul
If %Errorlevel%==1 Goto _NoFiles
Echo Files were deleted
Goto :EOF
:_NoFiles
Echo No files were found to delete
The other option is to first check to see if there are files that meet the criteria:
Code:
@Echo Off
SetLocal
Set _Path=C:\tstfldr1
For /F "tokens=1 delims=" %%I In ('ForFiles /P "%_Path%" /D -7 2^>^&1') Do Set _Result=%%I
If "%_Result%"=="ERROR: No files found with the specified search criteria." Goto _NoFiles
ForFiles /P "%_Path%" /D -7 /C "%ComSpec% /C If @ISDIR==FALSE Echo Del @File & Del @File" 2>Nul
Echo Files were deleted
Goto :EOF
:_NoFiles
Echo No files were found to delete
EDIT: Tried a couple other commands like "%ComSpec% /C DIR G:\I don't Exist" and "%ComSpec% /C notarealcommand" and the Errorlevel returned is still 0. It does display the appropriate error message though
The system cannot find the path specified.
and
'notarealcommand' is not recognized as an internal or external command,
operable program or batch file.

If there is a syntax error (say you forget the /C switch before the command) it will also set errorlevel to 1
So it looks like the errorlevel will be 1 only if no matching files or found, or there is a syntax error
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions


Last edited by TheOutcaste; 11-Aug-2009 at 07:44 PM.. Reason: Removed my %dbg% flag from the code
Nnyan's Avatar
Junior Member with 11 posts.
 
Join Date: Aug 2000
11-Aug-2009, 12:37 PM #6
I like the 2nd option to check for the files (since that is what I initially wanted to do). When I run that script even when is SHOULD it does not goto the :_NoFiles I just keep getting the files deleted message (with nothing listed).

I played around with this a bit and tried things like " IF ERRORLEVEL=1 GOTO :NOFILES " but no go. Ideally I would just want a check that if the condition is true (or false is find I can work with that) EX: there are folders older then the specified age, then do the delete function. If not then just GOTO.
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
11-Aug-2009, 08:18 PM #7
You can try this to see just what it is finding:
Code:
SetLocal
Set _Path=C:\tstfldr1
For /F "tokens=1 delims=" %%I In ('ForFiles /P "%_Path%" /D -7') Do Echo Loop Variable is ==%%I==
This will display the files/folders ForFiles finds, or display the error message. In this case, the error message will not be displayed as the Loop Variable. You'd see the error message, then Loop Variable is ====
Could be something as simple as your system doesn't have the period at the end of the error message.
The IF statement is also case sensitive. You can add the /I switch to eliminate that as a possibility:
Code:
If /I "%_Result%"=="ERROR: No files found with the specified search criteria." Goto _NoFiles
And make sure the labels match. They are not case sensitive, but Goto NoFiles is not the same as Goto _NoFIles
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

Nnyan's Avatar
Junior Member with 11 posts.
 
Join Date: Aug 2000
11-Aug-2009, 08:46 PM #8
When I run the first one I get what looks to be a listing of all the files in the folder (ex

C:\>Echo Loop Variable is =="6f0c96.msi"==
Loop Variable is =="6f0c96.msi"==

C:\>Echo Loop Variable is =="6f0c97.mst"==
Loop Variable is =="6f0c97.mst"==

C:\>Echo Loop Variable is =="96500b.mst"==
Loop Variable is =="96500b.mst"==

C:\>Echo Loop Variable is =="aruser.chm"==
Loop Variable is =="aruser.chm"==

Using this:

@Echo Off
SetLocal
Set _Path=C:\TEMP

For /F "tokens=1 delims=" %%I In ('ForFiles /P "%_Path%" /D -45') Do Set _Result=%%I
If /I "%_Result%"=="ERROR: No files found with the specified search criteria." Goto _NoFiles

ForFiles /P "%_Path%" /D -7 /C "%ComSpec% /C If @ISDIR==FALSE Echo Del @File" 2>Nul
Echo Files were deleted
Goto :EOF

:_NoFiles
Echo No files were found to delete


Gets me a listing of all the files in the folder but not the actual folders that I need.
Nnyan's Avatar
Junior Member with 11 posts.
 
Join Date: Aug 2000
11-Aug-2009, 08:51 PM #9
I started playing around with your original first suggestion and when I changed the following line from Errorlevel 1 to 0:

If %Errorlevel%==0 Goto _NoFiles

It works and does what I need it to (doesn't check first but I can live with that).
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
11-Aug-2009, 08:55 PM #10
Remember to change the @ISDIR==FALSE to @ISDIR==TRUE when looking for folders
Nnyan's Avatar
Junior Member with 11 posts.
 
Join Date: Aug 2000
11-Aug-2009, 09:09 PM #11
OK

Fixed my screw-ups this works (no checks but again thats fine):

@Echo Off
SetLocal
Set _Path=C:\TEST

REM ForFiles /P "%_Path%" /D -4 /C "%ComSpec% /C If @ISDIR==TRUE Echo RD /Q /S @File"
ForFiles /P "%_Path%" /D -2 /C "CMD /C if @ISDIR==TRUE echo RD /Q /S @FILE" 2>Nul

If %Errorlevel%==1 Goto _NoFiles
Echo Files were deleted
Goto :EOF

:_NoFiles
Echo No files were found to delete
Nnyan's Avatar
Junior Member with 11 posts.
 
Join Date: Aug 2000
14-Aug-2009, 02:02 PM #12
Anyone know how I can do a check to see if there are files that meet the requirements before I do the delete?
Nnyan's Avatar
Junior Member with 11 posts.
 
Join Date: Aug 2000
14-Aug-2009, 02:12 PM #13
Playing around with these two lines:

For /F "tokens=1 delims=" %%I In ('ForFiles /P "D:\FS-BACKUP\Purged" /D -30 2^>^&1') Do Set _Result=%%I
If "%_Result%"=="ERROR: No files found with the specified search criteria." Goto _NoFiles

Gets me a check that seems to work BUT I get a listing for each file it finds. Anyway to stop that from listing?
Squashman's Avatar
Trusted Advisor with 18,706 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: Bocks of Rox
14-Aug-2009, 02:26 PM #14
You redirecting the output to standard output. Since standard output is not redirecting to a file it will redirect to the console.
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
14-Aug-2009, 03:49 PM #15
Those two lines will not echo anything to the screen. I've redirected STDERR (handle 2) to STDOUT (handle 1) so that any error will be set to the loop variable and end up in _Result rather than just be displayed on screen. This way we can check if files meeting the criteria were found.

If you are seeing the files on the screen, it's some other line, like this one:
Code:
ForFiles /P "%_Path%" /D -7 /C "%ComSpec% /C If @ISDIR==FALSE Echo Del @File & Del @File" 2>Nul
This is line 6 from the 2nd section of code in Post#5
The Echo statements in that line will Echo the output.

The only thing this will display is whether or not there are files to process. I pass the # of Days as a command line argument to make testing easier:
Code:
@Echo Off
SetLocal
Set _Days=%1
For /F "tokens=1 delims=" %%I In ('ForFiles /P "C:\tstfldr1" /D %_Days% 2^>^&1') Do Set _Result=%%I
If "%_Result%"=="ERROR: No files found with the specified search criteria." Goto _NoFiles
Echo There are files to process
Goto :EOF
:_NoFiles
Echo No files were found to Process
Here's the result:
Code:
C:\tstfldr1>check.cmd -1
There are files to process

C:\tstfldr1>check.cmd -100
No files were found to Process
Without seeing the complete code there's no way to tell what is generating the output.
__________________
Microsoft MVP - Windows Expert - Consumer
Of course I know all the answers ; I just don't always match the answers to the right questions

Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:28 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.