Rename files for archiving last N days

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Rename files for archiving last N days

Post by Bob Hansen » Mon Jan 06, 2003 12:30 am

In an offline email with Edward, I reminded him that Movefile also does Rename also. (I had actually requested RenameFile as a new command and had to be told that Movefile already did that! What a dummy, me!) :oops:

He had suggested that our offline email content should be on the forum, and I agreed. As an example of using Movefile to Rename, I provided him with a small sample of using it to keep the last 5 days of an Outlook file. This is stripped down, ignoring paths, etc. It is part of a daily backup routine.

After 5 days of running you will see 5 outlook files in this folder, the current one, outlook.pst, and the copies from the last 5 days, modified names 001 through 005.

//=================================
Let>CF_OVERWRITE=1
Movefile>outlook_004.pst,outlook_005.pst
Movefile>outlook_003.pst,outlook_004.pst
Movefile>outlook_002.pst,outlook_003.pst
Movefile>outlook_001.pst,outlook_002.pst
Copyfile>outlook.pst,outlook_001.pst
//=================================


This simple version can obviously be used for any filenames. And more days can be archived by duplicating the Movefile lines.

That was a sample that I sent to Edward, but he asked for a working copy, so that is provided next.

Here is working code, a more complex version that allows any file name to be archived up to 999 copies. User enters filename, path and number of files to archive. Running daily

//========================================
//Developed by Bob Hansen, 1/5/03

Label>UserVariables
//=============================
//Files will be archived as "filenamebase_nnn.ext" format
//where nnn will be incremented and packed with leading zeroes
//Example: win.ini will become "win_001.ini"
Let>filenamebase=win
Let>Ext=ini
Let>path=c:\windows
//User defined number of copies to archive. Current program will allow up to 999 copies.
Let>copies=15
Label>EndUserVariables
//=============================
Let>CF_OVERWRITE=1

Label>Start
Change Directory>%path%
Let>todo=%copies%

Label>Archive
//=============================
Label>CalcNew
Let>new=00%todo%
Length>%new%,len
If>len=3,CalcOld
If>len=4,Remove1New

Label>Remove2New
//Remove extra zeroes
MidStr>new,3,3,new
Goto>CalcOld

Label>Remove1New
//Remove extra zero
MidStr>new,2,3,new

Label>CalcOld
Let>old=%new%-1
Let>old=00%old%
Length>%old%,len
If>len=3,MakeArchive
If>len=4,Remove1Old

Label>Remove2Old
//Remove extra zeroes
MidStr>old,3,3,old
Goto>MakeArchive

Label>Remove1Old
//Remove extra zero
MidStr>old,2,3,old

Label>MakeArchive
Movefile>%filenamebase%_%old%.%ext%,%filenamebase%_%new%.%ext%
Sub>todo,1

//Check to end archiving
If>%todo%>1,Archive

//Copy current file as most recent archive
Copyfile>%filenamebase%.%ext%,%filenamebase%_001.%ext%

Label>End

//===============================

This routine could also be set up with loops to change the filenames and paths to do this for multiple files in a nightly backup. It is provided as a starting point. Hope it is useful.

helpdsk
Newbie
Posts: 19
Joined: Thu Jan 02, 2003 10:15 pm
Location: Saint Paul, Minnesota

More filenames based upon today's date

Post by helpdsk » Mon Jan 06, 2003 11:01 am

Bob,

I am sorry to always ask you the hard questions, but does this script idea of yours also work for files created with today's date? If it does, what do I type in for the command
Let>filenamebase=
Can I run two scripts in the same directory using two different commands
Let>filenamebase=
Or do I need to create two different directories, with a different script for each directory?

For example, daily .log files, or .txt files created with today's dates in a specific directory, for programs like Norton AntiVirus or Zone Alarm Pro.

For December 31, 2002, here is the format for
Norton AntiVirus
12312002.Log
and here is the format for
Zone Alarm Pro
ZALog2002.12.31.txt
And here is a simulated screen print of the exact directory involved. The path is E:\WinNT\Edward\00Logs

Here is the Before listing of the specific directory
01012003.Log
01022003.Log
01032003.Log
01042003.Log
12282002.Log
12292002.Log
12302002.Log
12312002.Log
ZALog.txt
ZALog2002.12.28.txt
ZALog2002.12.29.txt
ZALog2002.12.30.txt
ZALog2002.12.31.txt
ZALog2003.01.01.txt
ZALog2003.01.02.txt
ZALog2003.01.03.txt
ZALog2003.01.04.txt
Here is what I want the directory to look like After I run both of my scripts
01022003.Log
01032003.Log
01042003.Log
ZALog.txt > > > > > > > I want this one also
ZALog2003.01.02.txt
ZALog2003.01.03.txt
ZALog2003.01.04.txt
Is this possible using two different scripts, using your original script idea?

Is there an adjustment that could be made to your script to keep the filenames intact and not use your command which creates
filenamebase_nnn.ext
It's no biggie if you can't... Just an idle curiosity...

Thanks for bearing with me.

Edward
Things should be made as simple as possible -- but no simpler.

Albert Einstein

Ernest

Post by Ernest » Mon Jan 06, 2003 12:40 pm

Hi,
move files sorted to specific folders:

Run Program>cmd /c move E:\WinNT\Edward\00Logs\*.Log "C:\Norton AntiVirus\*.*"

Run Program>cmd /c move E:\WinNT\Edward\00Logs\*.txt "C:\Zone Alarm Pro\*.*"

Rgds,
Ernest

Ernest

Post by Ernest » Mon Jan 06, 2003 3:52 pm

Ooops - I did it again! :roll:

You can use MScheds MoveFile> instead of the DOS move command. So it's ...

Code: Select all

MoveFile>E:\WinNT\Edward\00Logs\*.Log "C:\Norton AntiVirus\*.*"

MoveFile>E:\WinNT\Edward\00Logs\*.txt "C:\Zone Alarm Pro\*.*" 
Cheers
Ernest

helpdsk
Newbie
Posts: 19
Joined: Thu Jan 02, 2003 10:15 pm
Location: Saint Paul, Minnesota

I can move files by using DOS

Post by helpdsk » Mon Jan 06, 2003 8:58 pm

Ernest,

I can move files from one directory to another directory by using DOS, or by using Second Copy 2000, which I already talked about. :wink:

Take another look at Bob's post, and my post, and you will see that the issue at hand is to reduce the number of files in a specific directory to a set number on a daily basis. This set number would always be the "nn" most current files in this specific directory. :arrow: The rest would be discarded on a daily basis.

For example, in Bob's script it was 15 of the most current files. I would like my script to use my two formats to reduce the number of each format to the last 3 most current files. And to not touch any "odd" files found in that directory.

Any thoughts on this new suggestion? :idea:

Edward
Things should be made as simple as possible -- but no simpler.

Albert Einstein

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Jan 07, 2003 12:06 am

Just concepts here, no code....

You wrote:
For example, in Bob's script it was 15 of the most current files. I would like my script to use my two formats to reduce the number of each format to the last 3 most current files. And to not touch any "odd" files found in that directory.
Some immediate, shallow, uncooked thoughts:
1. In my script, just change the copies from 15 to 3. The oldest file is automatically deleted by being overwritten by the renaming..
2. Add a line at beginning of my script to check the basic file to be copied. Use IfFileChanged>, to decide to continue. If file not changed, then skip archiving process, goto End>.
3. Not sure what "odd" files are, but my script is very specific re filenames. No files would be processed but those specifically named. The script could be modified to use wild cards, or looped to change to additional specific filenames.
4. Another macro could be set up to read the specific filenames listed in a file and processed using ReadLn> to bring in new values for the variables path, ext, and filenamebase. They could be passed to this macro script as the variables.
5. Add the IfFileChanged> line after the variables so that you can read in the variables, if file not changed then go to End> and continue with next file name passed in to be processed.
=======================
Just some thoughts, no time to work on this now....good luck.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Tue Jan 07, 2003 5:38 am

Hello Edward.....

Here is another approach, that is more specific to your needs. I now see what you meant with the filenames having dates that change vs. my solution that had static file names.

Again, no time for code at this point, but here are my thoughts for the concept. I am sure that others on the forum will be able to help fill in the code to make this happen for you.

============================================
This process will do a bubble sort for filenames that are made from dates, newer files are v1, v2, v3 as oldest. This is just a flow chart concept at this point, not tested.

1. Delete all files from temp work directory.

2. Copy files format1 (*.log) to temp work directory

3. Read next filename from directory

4. Parse date portion into %vtemp%, (change to format = yyyymmdd for comparisons)

5. If %vtemp% greater than %v1% (is newer), then v3=%v2%, v2=%v1%, v1=%vtemp%
Else if %vtemp% newer than %v2%, then v3=%v2%, v2=%vtemp%
Else if %vtemp% newer than %v3%, then v3=%vtemp%

6. If not end of directory list, loop again at step 3, else go to step 7.

7. When done, v1, v2, v3 will be 3 latest files.

8. Read each filename from original directory.

9. If date portion of filename (format1) is not v1, v2, v3, then delete file from original directory. If you want to keep other files (ZALog.txt > > > > > > > I want this one also)?, enter their names as variables at beginning of this script and compare before deleting.

10. When done, delete all files from temp work directory.

11. Repeat steps 1 thru 10 for files format2 (ZAlog*.txt)

===========================================
This is crude at this point. I don't know the content of the original directory, so there might be other controls needed to decide if a file is deleted or not.

Hope you can work with this......
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Lumumba

Replicate Files From One Directory To Another...

Post by Lumumba » Thu Jan 09, 2003 10:37 pm

Hi,
Nice VBScript source: http://cwashington.netreach.net/, maybe helpfull.

Replicate Files From One Directory To Another...
Description:

Script replicates a source directory into a target directory. It does so efficiently by comparing the contents. It deletes files that no longer exist in the source from the target. It compares the file modified dates for files that exist in both and overwrites them if necessary. It copies files that do not exist in the target. File also created a log of the activity. I use the script in combination with a scheduler to backup the files in my remote offices over VPN. Speed is of the essence so I do not want to copy files that have not been changed.

show me the code

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Fri Jan 10, 2003 12:25 pm

Hi Lumumba,

Without a doubt, MacroScript combined with VBScript could attain the file comparision algorithm. Nonetheless, I strongly reommend Beyond Compare 2 to handle the file comparisons and sychronizations. Like Macro Scheduler, Beyond Compare 2 includes a script language. The most powerful and time-saving solution is to use Macro Scheduler script to control Beyond Compare 2 on the fly.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts