Link to home
Start Free TrialLog in
Avatar of DancingFighterG

asked on

Deleting files from a command prompt

Hello, I have to delete a large amount of files from a windows director and the operating system is having a hard time doing this. So instead I want to use command prompt commands to delete these files. With that being said I need the to keep the last three months of files but delete the rest. What would be the cmd commands to do this.

I know how to overall delete a file from the cmd:

del "filename 1.txt" "filename 2.txt" del "filename 3.txt"

but how do I do this so that it will use the current date and delete 3 months back. Since the files are important I'm simply asking so I don't delete the wrong files. I know there is some wildcarding involved
Avatar of csaroli

you can't do that using a simple "del" command.  You need another program or use a batch file.

check out this site where someone created a batch file to delete files before a specific date
Avatar of EnriquePhoenix
FORFILES /P "c:\test" /S /M *.* /D -90 /C "CMD /C del /Q @FILE"

this will delete any file over 90 day all extensions
Avatar of DancingFighterG



So I would run this on the cmd or in a batch file? Also, let me clarify I want to keep the last three months of files and delete the rest. We currently have files that date back to 6/1/2010 so I want to keep all of February, January, December but delete the rest.
Another thing you can do is that move unnecessary files to some temparory folder using XCOPY and then remove it. XCOPY also allows a /d switch to specify the date.

For e.g.

xcopy *.* /d:04-12-2011 e:\ToBeRemoved

rmdir e:\ToBeRemoved /s/q

Also, check the following link that might helpful to you.
Yes you can change the 90 to any other number 90=90 days

To test:
create a test folder on the C: drive
copy old files to it (older than 90 days old)
create new files test files less than 90 days
copy and paste the command in the CMD window

You can copy the command to a txt file and rename the txt extension to cmd or bat. Then you can run it without opening the cmd window.

This is what I have now

FORFILES /P "\\mwjobsv2\d$\test\*.*" /S /M *.* /D -90 /C "CMD /C del /Q @FILE"

and I have a file in my test that is dated 6/25/2010 and 3/19/2009. This should be deleting this files correct?
Deletes all files in folder dSource older than 90 days

You have to adapt dsource to your special folder, in which you want to delete the files

You can adapt  the days to keep in line 5:
set /a strip=day*90  means to keep 90 days (three months)

@echo off 
set day=86400 
set /a year=day*365 
set /a strip=day*90 
set dSource=d:\temp 

call :epoch %date% 
set /a slice=epoch-strip 
for /f "delims=" %%f in ('dir /a-d-h-s /b /s %dSource%') do ( 
    call :epoch %%~tf 
    if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^) 
exit /b 0 
rem Args[1]: Year-Month-Day 
    for /f "tokens=1,2,3 delims=-" %%d in ('echo %1') do set Years=%%d& set Months=%%e& set Days=%%f 
    if "!Months:~0,1!"=="0" set Months=!Months:~1,1! 
    if "!Days:~0,1!"=="0" set Days=!Days:~1,1! 
    set /a Days=Days*day 
    set i=1&& for %%m in (31 28 31 30 31 30 31 31 30 31 30 31) do if !i! LSS !Months! (set /a _months=!_months! + %%m*day&& set /a i+=1) 
    set /a Months=!_months!
    set /a Years=(Years-1970)*year 
    set /a Epoch=Years+Months+Days 
    endlocal& set Epoch=%Epoch% 
    exit /b 0

Open in new window


I also get the following error when I run the cmd from the cmd prompt

(\\machine\share) not supported
ok, I got it now Enrique. I didn't need the quotes around the path name.
Using this code

FORFILES /P d:\JAMSLogs\ /S /M *.* /D -90 /C "CMD /C del /Q @FILE"

What would I need to add so that I can write the name of the files to a text file so I know what is being deleted?
Also, when I try to run this command as a .bat file I get the following error:

FORFILES is not recognized as an internal or external command, operable program or batch file

The command works as it deleted all the records I needed but  I had to input in the line in the cmd prompt manually instead of running it as a batch file
I used this exact command as "erase.cmd" off my desktop and it runs fine with my Vista box.  Except the folder was located on my C: drive. I would leave the pause in there so you could see where the error is located.

FORFILES /P D:\JAMSLogs\ /S /M *.txt /D -90 /C "CMD /C del /Q @FILE"

Open in new window

Made the change for txt files only. Make sure to test before using it live.
I have the pause there already. That's how I can see the error

FORFILES is not recognized as an internal or external command, operable program or batch file

oh wait I do see this:

Maybe I need to change directories first in the bat file first
Avatar of EnriquePhoenix
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just tried it to a mapped drive, without changing directories and it still worked fine. Gonna look for a XP machine to try it on.
hmmm...this is weird
I just found out that forfiles it not native to XP. You would have to download it and put it into you system32 folder.
Where would download that from?