Computing.Net > Forums > Windows XP > dos command for Deleting by date

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

dos command for Deleting by date

Reply to Message Icon

Name: CONSTANCIO
Date: June 21, 2008 at 20:09:09 Pacific
OS: XP Pro
CPU/Ram: intel
Product: Dell
Comment:

Hi Guys,
I would like to write a batch file that deletes files based on their date stamps. For example, i'd like to delete files in a folder that are 2 weeks old or more. Is there a way to do this with DOS?
Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 22, 2008 at 00:16:04 Pacific
Reply:

In XP, no way I know of. And in DOS, not a chance.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: HelpingGuy123
Date: June 22, 2008 at 00:22:19 Pacific
Reply:

CONSTANCIO,

It can be done with batch file, depending on your batch filling skills.

Here is the trick,

Redirect your DIR command output to a text file as,

DIR /s > 2weekold.txt

This way you get the list of directory in the form of text file.

Now, use DELETE or ERASE command with IF condition to get rid of old files. You surely need to have good command over DOS batch programming. Good luck :)

Technology Bytes Plus
http://learnitfast.blogspot.com


0

Response Number 3
Name: constancio
Date: June 22, 2008 at 08:00:06 Pacific
Reply:

I would like for this batch file to compare the file dates with the system time so that the batch can be run automatically at computer startup. Once the user logs in, the batch would run and delete files that are 2 weeks old based on the system time.Once i have the batch, i'd copy it to the startup folder.


0

Response Number 4
Name: dtech10
Date: June 22, 2008 at 14:49:07 Pacific
Reply:

Hi Constancio

I have written one for WinXP, but it depends
a lot on your date format ect.

If you "echo %date%" at the command prompt and tell me what the format is ie. mm/dd/yyyy
ect. and also try the command "dir *.* /a-d-h" and give an a sample of the output.
I,ll see what I can do.


0

Response Number 5
Name: CONSTANCIO
Date: June 22, 2008 at 20:06:37 Pacific
Reply:

HI Dtech10,
Here is what my output is after running the date and dir command:
Output starts here
------
C:\>echo %date%
Sun 06/22/2008

C:\>dir *.* /a-d-h
Volume in drive C is Laptop Hard Disk
Volume Serial Number is 68C1-C276

Directory of C:\

06/25/2007 09:03 PM 0 AUTOEXEC.BAT
03/12/2008 09:49 PM 80 BIOSID.TXT
06/25/2007 09:03 PM 0 CONFIG.SYS
09/09/2007 10:43 AM 76 DVDPATH.TXT
06/25/2007 09:08 PM 1,047 install_Log.txt
05/15/2008 06:23 PM 24 LS_TRAYAPP.ini
06/20/2008 09:23 AM 5,755 lxcj.log
06/20/2008 09:23 AM 11,610 lxcjcomx.log
06/20/2008 09:25 AM 44,143 lxcjscan.log
03/12/2008 06:26 AM 672,144 lxcjUNST.csv
03/12/2008 04:52 PM 63,750 PollSt.txt
07/21/2000 10:40 AM 2,048 w2ksect.bin
06/26/2007 01:30 PM 146 YServer.txt
13 File(s) 800,823 bytes
0 Dir(s) 2,686,590,976 bytes free
-----------------
output ends
Hope this helps
thanks
Constancio


0

Related Posts

See More



Response Number 6
Name: Valerie (by Garibaldi)
Date: June 23, 2008 at 02:32:21 Pacific
Reply:

Forfiles might be helpful..

http://www.ss64.com/nt/forfiles.html


0

Response Number 7
Name: CONSTANCIO
Date: June 23, 2008 at 16:23:50 Pacific
Reply:

Valerie,
FORFILES does not seem to work in XP PRO SP2. When i try to run it, i get an error like the one below:
C:\>FORFILES -pC:\ -s -m*.TXT -c"CMD /C Echo @FILE is a text file"
'FORFILES' is not recognized as an internal or external command,
operable program or batch file..
Let me know if there is anything else i can do.
Thanks
Constancio


0

Response Number 8
Name: dtech10
Date: June 23, 2008 at 16:26:18 Pacific
Reply:

Hi
Try this file deletion by date in a batch is quite tricky due the different date and time formats.
Valerie program looks a better bet.
If want to a batch file here's my version.
The del is remmed out for safety.

@echo off
setlocal enabledelayedexpansion

--
rem Delete Files on Age in Days
--

rem File Age in Days to Delete
set FileAge=0

rem Get File list
rem Change pushd to dir required
pushd f:\
dir *.* /a-d-h | find /v "(s)" > FileList.txt

rem Get Todays Day Number
call :DayNo %Date%
set Today=%DateNo%


rem Get File list and delete if over %FileAge% days old
for /f "skip=4 tokens=1-4,*" %%a in (FileList.txt) do (
call :DayNo %%a
rem Calc File Age and delete if over %FileAge% days old
set /a CalcAge=%Today%-!DateNo!
if !CalcAge! GEQ %FileAge% (
echo %%e %%a !CalcAge! days old
echo del /p "%%e" : rem Remed out for safety
echo.
)
)
popd
rem del FileList.txt when ok
rem del FileList.txt
exit /b


rem Get Date Number
: DayNo %1
for /f "tokens=1-3 delims=/- " %%a in ("%1") do (
set /a mm=%%a
set /a dd=%%b
set /a yy=%%c
)
set /a DateNo=%dd%+(%mm%*306001/10000)+(%yy%*1461/4)
exit /b


0

Response Number 9
Name: jonathan-s
Date: July 6, 2008 at 18:07:50 Pacific
Reply:

Hello, I just ran across this post looking to accomplish the same task. I used the script you created above and changed the directory and age, but get errors when it runs.

This is what I see in cmd:
F:\>Remove_files_by_date.bat
Missing operand.
Missing operand.
Missing operand.
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

I am using XP Pro, and my echo %date% matches his as well.


0

Response Number 10
Name: Mechanix2Go
Date: July 7, 2008 at 01:29:35 Pacific
Reply:

Nobody likes to hear 'I tokd you so' but you'll notice this has been marinating for a couple weeks.

As soon as somebody makes 'date math' work, I'll be all ears.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 11
Name: jp21in
Date: July 15, 2008 at 08:54:42 Pacific
Reply:

If you parse a 2 digit date, you'll get 08 for Aug, 09 for September... DOS numeric vars treat numbers starting with 0 as "octal".
"set /a monthvar = 07" is fine because octal 7 is also decimal 7, but "set /a monthvar = 08" is not valid because there is no such thing as octal 8, 9, A, B... octal only goes from 0 to 7... The fix... "set /a monthvar = 1%mm% - 100", now you're talking! So if "mm" equals "08", monthvar will get 8...


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Windows XP Forum Home


Sponsored links

Ads by Google


Results for: dos command for Deleting by date

dos commands PLEASE DONT DELETE www.computing.net/answers/windows-xp/dos-commands-please-dont-delete/68822.html

Batch file to delete by date www.computing.net/answers/windows-xp/batch-file-to-delete-by-date-/135330.html

NET SHARE DOS command does not work www.computing.net/answers/windows-xp/net-share-dos-command-does-not-work/69853.html