Solved

Deleting files from a command prompt

Posted on 2011-02-15
21
920 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
 
LVL 20

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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

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 51

Expert Comment

by:Bill Prew
ID: 34912730
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Are you unable to synchronize your OST (Offline Storage Table) file with Microsoft Exchange Server? Is your OST file exceeding 2 GB size limit? In Microsoft Outlook 2002 and earlier versions, there is a 2 GB size limit for the OST file. If the file …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now