Solved

Deleting files from a command prompt

Posted on 2011-02-15
21
923 Views
Last Modified: 2012-05-11
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
Comment
Question by:DancingFighterG
21 Comments
 
LVL 1

Expert Comment

by:csaroli
ID: 34898142
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
 
LVL 5

Expert Comment

by:EnriquePhoenix
ID: 34898214
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
 

Author Comment

by:DancingFighterG
ID: 34898257
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 34898311
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
 
LVL 5

Expert Comment

by:EnriquePhoenix
ID: 34898708
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
 

Author Comment

by:DancingFighterG
ID: 34899674
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
 
LVL 19

Expert Comment

by:Thommy
ID: 34899771
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
 

Author Comment

by:DancingFighterG
ID: 34899818
EnriquePhoenix:

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

(\\machine\share) not supported
0
 

Author Comment

by:DancingFighterG
ID: 34899916
ok, I got it now Enrique. I didn't need the quotes around the path name.
0
 

Author Comment

by:DancingFighterG
ID: 34900175
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
 

Author Comment

by:DancingFighterG
ID: 34900288
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
 
LVL 5

Expert Comment

by:EnriquePhoenix
ID: 34900493
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
 

Author Comment

by:DancingFighterG
ID: 34900591
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
 

Author Comment

by:DancingFighterG
ID: 34900618
oh wait I do see this:


forfiles.jpg
0
 

Author Comment

by:DancingFighterG
ID: 34900624
Maybe I need to change directories first in the bat file first
0
 
LVL 5

Accepted Solution

by:
EnriquePhoenix earned 500 total points
ID: 34900810
cd..
cd..
cd..
cd..
D:

Yes I would change directories first. You should also on a local drive. Is this drive mapped?
0
 
LVL 5

Expert Comment

by:EnriquePhoenix
ID: 34900861
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
 

Author Comment

by:DancingFighterG
ID: 34900994
hmmm...this is weird
0
 
LVL 5

Expert Comment

by:EnriquePhoenix
ID: 34901408
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
 

Author Comment

by:DancingFighterG
ID: 34901573
Where would download that from?
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 34912730
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ok I have been working on this for some time having learned and gained certification in XenDesktop 4 along came version 5 which was released last month. Since then I have been working to deploy XenDesktop 5 in a small environment with only 2 virt…
We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question