Computing.Net > Forums > Programming > Need more code

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.

Need more code

Reply to Message Icon

Name: William Lockie (by blockie)
Date: June 7, 2008 at 20:39:49 Pacific
OS: WXP SP2
CPU/Ram: 1.4/512
Product: Clone
Comment:

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 MMDDYYYY

FOR /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"



Sponsored Link
Ads by Google

Response Number 1
Name: dtech10
Date: June 10, 2008 at 07:01:39 Pacific
Reply:

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=30

rem Get File list
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 %%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


0

Response Number 2
Name: William Lockie (by blockie)
Date: June 10, 2008 at 14:43:35 Pacific
Reply:

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"


0

Response Number 3
Name: dtech10
Date: June 11, 2008 at 13:10:40 Pacific
Reply:

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".



0

Response Number 4
Name: William Lockie (by blockie)
Date: June 12, 2008 at 20:44:37 Pacific
Reply:

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.
Bill

William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"


0

Response Number 5
Name: dtech10
Date: June 13, 2008 at 14:56:02 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: William Lockie (by blockie)
Date: June 14, 2008 at 12:16:07 Pacific
Reply:

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=0

rem Get File list
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 %%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

Note 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"


0

Response Number 7
Name: dtech10
Date: June 15, 2008 at 10:10:03 Pacific
Reply:

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 %%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.
rem ---------
pause
rem ---------
)
)
exit /b


0

Response Number 8
Name: William Lockie (by blockie)
Date: June 17, 2008 at 17:42:25 Pacific
Reply:

Here is copy of "filelist.txt"

Volume in drive C is drive_c
Volume Serial Number is E8BC-41C9

Directory 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.CDX

William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"


0

Response Number 9
Name: dtech10
Date: June 18, 2008 at 14:27:08 Pacific
Reply:

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=30

rem Get File list
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
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


0

Response Number 10
Name: William Lockie (by blockie)
Date: June 19, 2008 at 08:51:25 Pacific
Reply:

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.
Bill

William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"


0

Response Number 11
Name: dtech10
Date: June 19, 2008 at 12:57:56 Pacific
Reply:

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%]
--


0

Response Number 12
Name: William Lockie (by blockie)
Date: June 19, 2008 at 18:24:57 Pacific
Reply:

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?
Bill

William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"


0

Response Number 13
Name: dtech10
Date: June 20, 2008 at 15:01:33 Pacific
Reply:

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.


0

Response Number 14
Name: William Lockie (by blockie)
Date: June 21, 2008 at 06:44:10 Pacific
Reply:


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.
Bill

setlocal enabledelayedexpansion

--
rem Delete Files on Age in Days
--

rem File Age in Days to Delete
set FileAge=4

rem Get File list
dir D:\test\*.* /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 %%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"


0

Response Number 15
Name: dtech10
Date: June 21, 2008 at 16:48:18 Pacific
Reply:

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 /b

Note 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.



0

Response Number 16
Name: William Lockie (by blockie)
Date: June 21, 2008 at 21:23:43 Pacific
Reply:

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"


0

Response Number 17
Name: dtech10
Date: June 22, 2008 at 14:21:32 Pacific
Reply:

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=0

rem Get File list
rem change to pushd d:\DriveImage when tested
pushd d:\Test
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
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


0

Response Number 18
Name: William Lockie (by blockie)
Date: June 22, 2008 at 15:02:53 Pacific
Reply:

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-D89A

Directory 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"


0

Response Number 19
Name: dtech10
Date: June 22, 2008 at 15:55:01 Pacific
Reply:

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 else

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


0

Response Number 20
Name: dtech10
Date: June 22, 2008 at 16:08:17 Pacific
Reply:

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


0

Response Number 21
Name: William Lockie (by blockie)
Date: June 23, 2008 at 09:15:12 Pacific
Reply:

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=30

rem Get File list
rem change to pushd d:\DriveImage when tested

dir 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"


0

Response Number 22
Name: William Lockie (by blockie)
Date: June 23, 2008 at 14:16:50 Pacific
Reply:

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=30

rem Get File list
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
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

William Lockie
"OS: WXP SP2
CPU/Ram: 3.0/2GB
Manufacturer/Model: clone"


0

Response Number 23
Name: dtech10
Date: June 23, 2008 at 15:56:45 Pacific
Reply:

Hi William

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



0

Response Number 24
Name: William Lockie (by blockie)
Date: June 23, 2008 at 16:09:35 Pacific
Reply:

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"


0

Response Number 25
Name: dtech10
Date: June 24, 2008 at 12:24:41 Pacific
Reply:

Hi William



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 Programming Forum Home


Sponsored links

Ads by Google


Results for: Need more code

need source code / tips www.computing.net/answers/programming/need-source-code-tips/3582.html

SMS - Sender (all code already done www.computing.net/answers/programming/sms-sender-all-code-already-done/5048.html

Need Help!! Coding a drop down list www.computing.net/answers/programming/need-help-coding-a-drop-down-list/3243.html