Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am using the following scrip to determine the date code to append to my backup files so they are unique from one another. Now I would like a piece of code that will build on this scrip to delete files older than 30 days. If I do this I will always have room on my second HDD to make a backup.
Any ideas? This is not a code I developed.
echo on
@REM Seamonkey's quick date batch (MMDDYYYY format)
@REM Setups %date variable
@REM First parses month, day, and year into mm , dd, yyyy formats and then combines to be MMDDYYYYFOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Hi William
echo off
rem I've assumed the date format is mm/dd/yyyy and
rem the dir command produces the date in the same format
rem the date being the first item.setlocal enabledelayedexpansion
--
rem Delete Files on Age in Days
--rem File Age in Days to Delete
set FileAge=30rem Get File list
dir *.* /a-d-h | find /v "(s)" > FileList.txtrem 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 %%b %%a !CalcAge! days old
del /p %%b : rem Delete /p if required Be carefull
echo.
)
)
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

Dtech10,
Thanks for your input. I copied the script to a batch file and placed it in a folder that I had a bunch of dummy text file. All were named 'mmddyyyyxx". ie; 03023008xx.txt, 05022008xx.txt , etc. Notice that the "/" are not in the date sting. Filenames cannot contain forward or back slashes.
I ran the batch file and it did not delete any files although it generated a list of files in the folder. Also it seems that the code you provided generated a date string for me to attach to the front end of the backup files.
Bill
I am not a scrip programmer so its hard for me to understand the code. Where is a good place for me to get a list of the commands?William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Hi William
Try http://www.computerhope.com/msdos.htm
The program doe's not rename your files,
you can call them what you like.When you say dummy text files were they older or equal to 30 days old. If you just copied them there they would have that days date stamp.
Did the program display anything on the screen.
To check that it's working ok change this line "set FileAge=30" to "set FileAge=0"
don't type the surrounding quotes.
This should display all files and ask for confirmation.
Then change it back to "set FilkeAge=30" and try it on any files it will not delete any if you type "N".

I did as you suggested , set FileAge=0. AS lot of activity in the command window but that was all. The txt file did list all the files in the folder, they were 2 or more days old. Clicked View....Refresh and all the files are still there.
It's not deleting files.
BillWilliam Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Hi William
What activity is on the scrren it should be
somthing like.D653F3EC.TMP 10/06/2008 3 days old
f:\D653F3EC.TMP, Delete (Y/N)?
test.bat 10/06/2008 3 days old
f:\test.bat, Delete (Y/N)?if not post a section on the text file here.

IO need a way to slow thje action in the Dos promp window down so I can answer yur question.
I used the code like this:
setlocal enabledelayedexpansion
--
rem Delete Files on Age in Days
--rem File Age in Days to Delete
set FileAge=0rem Get File list
dir *.* /a-d-h | find /v "(s)" > FileList.txtrem 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 %%arem Calc File Age and delete if over %FileAge% days old
set /a CalcAge=%Today%-!DateNo!
if !CalcAge! GEQ %FileAge% (
echo %%b %%a !CalcAge! days old
del /p %%b : rem Delete /p if required Be carefull
echo.
)
)
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 /bNote that I changed the "FileAge" to 0.
Other than what was in the command window (which is too fast for me) this batch file produced a filelist.txt
Bill
William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Hi William
Do are all the files mmddyy or do some have spaces in them.
Still waiting for a sample of the FileList.txt
Add a pause as below.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 %%arem Calc File Age and delete if over %FileAge% days old
set /a CalcAge=%Today%-!DateNo!
if !CalcAge! GEQ %FileAge% (
echo %%b %%a !CalcAge! days old
del /p %%b : rem Delete /p if required Be carefull
echo.
rem ---------
pause
rem ---------
)
)
exit /b

Here is copy of "filelist.txt"
Volume in drive C is drive_c
Volume Serial Number is E8BC-41C9Directory of C:\aa
06/10/2008 02:11 PM 0 04122008aa.txt
06/10/2008 02:11 PM 0 04242008aa.txt
06/10/2008 02:11 PM 0 04252008aa.txt
06/10/2008 02:10 PM 0 05122008aa.txt
06/10/2008 02:13 PM 0 06012008aa.txt
06/10/2008 02:12 PM 0 06022008aa.txt
06/10/2008 02:12 PM 0 06042008aa.txt
06/15/2008 04:25 PM 836 deletedates.bat
06/15/2008 04:31 PM 0 FileList.txt
06/11/2008 07:58 AM 14,848 xfer.CDXWilliam Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Now we might be getting somewhere I seen your filelist, your time format AM/PM was an extra item.
Try This......
Note Ive set Fileage back to 30
Change to zero to test every file.
I've also modified it to include filenames with spaces in them.
I think you know by now that you don't need the extra code the this thread started with
@echo off
setlocal enabledelayedexpansion--
rem Delete Files on Age in Days
--rem File Age in Days to Delete
set FileAge=30rem Get File list
dir *.* /a-d-h | find /v "(s)" > FileList.txtrem 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
del /p "%%e" : rem Delete /p if required
echo.
)
)
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

I set file age to 0 and tried it. Doesn't work.
I'm not sure what you mean by I don't need the original code. It starts my backup program with various switches and works perfectly. It also attached a date to the file name so I can identify the backup. Lets leave that part alone until we get the file deletion with your program settled.
BillWilliam Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Hi William
The code changes the date variable and my program needs this to work. it's a bad idea to change a system variable.
change the last line of your code to
SET Xdate=%mm%%dd%%yyyy%
and use that instead of SET date=%mm%%dd%%yyyy% or this code will do the same thing-----------------------
@echo off
for /f "tokens=1-3 delims=/- " %%a in ('date/t') do (
set mm=%%a
set dd=%%b
set yy=%%c
)
set XDate=%mm%%dd%%yy%
echo [%XDate%]
--

Dtech10,
Incorporated your code to my backup scriopt and ran it. Worked great. Thanks.Now if we can get the delete file scrip to working I can also incorporate it.
If I read the code correctly you are deleting files by looking at their date stamps and not the filename. Right?
BillWilliam Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Hi
We got there in the end.
Yes it looks at the date stamps, your files can be left to their original names if required.

I changed the code a bit to NOT ask permission to delete. Removed the "P" switch.
Worked great except I deleted ALL files in the folder including the batch file. I then worked on it a bit more and added the drive and path of some test files.. Got a list of the test file on drive D: but they did not delete. What else should I change?Hers the code I am now using.
Billsetlocal enabledelayedexpansion
--
rem Delete Files on Age in Days
--rem File Age in Days to Delete
set FileAge=4rem Get File list
dir D:\test\*.* /a-d-h | find /v "(s)" > FileList.txtrem 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 %%arem Calc File Age and delete if over %FileAge% days old
set /a CalcAge=%Today%-!DateNo!
if !CalcAge! GEQ %FileAge% (
echo %%b %%a !CalcAge! days old
del %%b : rem Delete /p if required Be carefull
echo.
)
)
pause
exit /b
rem Get Date Number
: DayNo %1
for /f "tokens=1-3 delims=/- " %%a in ('date/t') do (
set mm=%%a
set dd=%%b
set yy=%%c
)
set XDate=%mm%%dd%%yy%
echo [%XDate%]William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Hi
Where is this section of code.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 /bNote the /a is needed.
From which folder do you run this batch file.
It's designed to run from the folder your files are in.
Do you always use the same folder for deleting files older than x days or different
folders.Do you know about adding to the system path.
I need to know how you want to use this batch file.

I have already incorporated the missing code into the script that renames the backups just before conducting the backup operation.
I run this batch file from C:\scripts\
This is the folder that I have all my batch files in and they are controlled by the Windows scheduler.I always use the batch file to delete files older than "fileage" in folder D:\DriveImage\.
I wish to use the batch file to make room on my D: drive for additional recent backups. Also to generate a date code so that all backup files are identified uniquely. Then run the backup program. Finally put this batch file on the Windows scheduler so that this all occurs on Saturday of each week at 11:30 PM.
I only have to get the file deletion code accurate and I will combine all of this.
Bill
William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Hi William
Try this from your c:\scripts folder.
I left the del /p in for safety.@echo off
setlocal enabledelayedexpansion--
rem Delete Files on Age in Days
--rem File Age in Days to Delete
set FileAge=0rem Get File list
rem change to pushd d:\DriveImage when tested
pushd d:\Test
dir \*.* /a-d-h | find /v "(s)" > FileList.txtrem 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
del /p "%%e" : rem Delete /p if required
echo.
)
)
rem del FileList.txt
popd
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

I placed this code in my scripts folder and ran it. It only produced a text file with the following contents,
Volume in drive D is drive_d
Volume Serial Number is 3CDC-D89ADirectory of D:\
Thats all.
The folder d:\test had 43 files in it. After running the batch file it had 44 files. The extra file was the "FileList.TXT" file.William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Hi William
Add this "exit /b" to this section of code
Are you in D:\test and the dir command must list the files in d:\test. I don't understand how it could be anything elserem Get File list
rem change to pushd d:\DriveImage when tested
pushd d:\Test
dir \*.* /a-d-h | find /v "(s)" > FileList.txt
exit /b

Hi William
It's just occurrred to me that your files don't have an extensions, is that correct.
If so you need to change the dir statement.
Try this then modify the main code if ok.rem Get File list
rem change to pushd d:\DriveImage when tested
pushd d:\Test
dir /a-d-h | find /v "(s)" > FileList.txt
exit /b

dtech 10,
I placed the "exit /b in as you suggested. Of course it stopped right there. However, it made a file list of the C: root. So I changed a bit of code to explicitly create the file list from D:\test\. That worked fine . A file list of D:\test\ was created and placed in the C:\scripts\ folder and it contained the files in D:\test. A copy of the modified code follows;
@echo on
setlocal enabledelayedexpansion
rem Delete Files on Age in Days
rem File Age in Days to Delete
set FileAge=30rem Get File list
rem change to pushd d:\DriveImage when testeddir d:\Test\*.* /a-d-h | find /v "(s)" > FileList.txt
exit /b
NOTE that I tuned echo on so I could see what it was doing.I then removed the "exit /b" from the above code to continue the batch file. And I used "pause" in various places so that I could read the screen. The batch file does go to the second batch file and get a date. It also looks at the filelist and reads each file. It does NOT offer to delete older files though. I suspect that it has something to do with looking in D:\test\.
William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Have been fooling with it some more. I have come to the conclusion that the batchfile is better operated from d:\test\. I think, maybe, we were trying to work two folders. What do you think of this idea. In the c:\scripts\ folder have a batch file that copies your file to D:\test| and executes it from there.
Here is the code I am now using:
@echo off
setlocal enabledelayedexpansion--
rem Delete Files on Age in Days
--rem File Age in Days to Delete
set FileAge=30rem Get File list
dir *.* /a-d-h | find /v "(s)" > FileList.txtrem 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
del /p "%%e" : rem Delete /p if required
echo.
)
)
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 /bWilliam Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

Hi William
I was orginally designed to work from the folder where the files required deleteing.

Good. I placed the batch file in the folder that needs cleaning up but all it does is create a texfile and examine it. It does not offer to delete any files. Is there any way for me to send you a copy of what it is doing?
William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"

![]() |
![]() |
![]() |

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