• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 943
  • Last Modified:

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
0
DancingFighterG
Asked:
DancingFighterG
1 Solution
 
csaroliCommented:
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

http://scottelkin.com/programming/delete-files-older-than-date-using-batch-files/
0
 
EnriquePhoenixCommented:
FORFILES /P "c:\test" /S /M *.* /D -90 /C "CMD /C del /Q @FILE"

this will delete any file over 90 day all extensions
0
 
DancingFighterGAuthor Commented:
EnriquePhoenix,

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.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
Amitkumar PSr. ConsultantCommented:
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.
http://blog.gofolo.com/2010/10/move-files-in-folders-and-subfolders-older-than-one-year/
0
 
EnriquePhoenixCommented:
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.
0
 
DancingFighterGAuthor Commented:
EnriquePhoenix:

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?
0
 
ThommyCommented:
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 
setlocal ENABLEDELAYEDEXPANSION 
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 
:epoch 
    setlocal ENABLEDELAYEDEXPANSION 
    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

0
 
DancingFighterGAuthor Commented:
EnriquePhoenix:

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

(\\machine\share) not supported
0
 
DancingFighterGAuthor Commented:
ok, I got it now Enrique. I didn't need the quotes around the path name.
0
 
DancingFighterGAuthor Commented:
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?
0
 
DancingFighterGAuthor Commented:
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
0
 
EnriquePhoenixCommented:
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"
pause

Open in new window


Made the change for txt files only. Make sure to test before using it live.
0
 
DancingFighterGAuthor Commented:
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

0
 
DancingFighterGAuthor Commented:
oh wait I do see this:


forfiles.jpg
0
 
DancingFighterGAuthor Commented:
Maybe I need to change directories first in the bat file first
0
 
EnriquePhoenixCommented:
cd..
cd..
cd..
cd..
D:

Yes I would change directories first. You should also on a local drive. Is this drive mapped?
0
 
EnriquePhoenixCommented:
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.
0
 
DancingFighterGAuthor Commented:
hmmm...this is weird
0
 
EnriquePhoenixCommented:
I just found out that forfiles it not native to XP. You would have to download it and put it into you system32 folder.
0
 
DancingFighterGAuthor Commented:
Where would download that from?
0
 
Bill PrewCommented:
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now