Moving a file x days old

Dell/ Latitude D610
January 22, 2007 at 07:29:57
Specs: WinXP, 2.86/1024
I am trying to create a script that will move log file to a new directory if they are 7 days old or older.
If any one can Help I would appreciate it.


See More: Moving a file x days old

Report •


#1
January 22, 2007 at 07:48:38
Nobody here has yet worked out the 'date math' issue. You can take my word for it or you can peruse several months worth of msgs.

One alternative is to keep X latest files.


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

M2



Report •

#2
January 22, 2007 at 08:47:57
You CAN do it with a VB Script.

Just copy and past into a text doc and name witht he vbs extension. You will just need to modify the SOURCE and TARGET variables to your situation. The script only moves files with the ".log" extension from the SOURCE to the TARGET if older than 7 days. There should probably be some more error handling, but this is the basics.

========BEGIN SCRIPT=========
Dim SOURCE, TARGET
SOURCE = "c:\logs"
TARGET = "c:\oldlogs\"

Dim fso, SourceObj, TargetObj, fileObj
Set fso = CreateObject("Scripting.FileSystemObject")
Set SourceObj = fso.GetFolder(SOURCE)

For Each fileObj in SourceObj.Files
If LCase(Right(fileObj.Name, 4)) = ".log" Then
If DateDiff("d", fileObj.DateLastModified, Now) > 7 Then
fileObj.Move (TARGET)
End If
End If
Next
=========END SCRIPT==========

Michael J


Report •

#3
January 22, 2007 at 09:16:21
Hi Michael,

I put in my paths and file types and got:

error line 12
file already exists


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

M2



Report •

Related Solutions

#4
January 22, 2007 at 09:30:36
Found my mistake.

SOURCE must NOT have trailing backslash.

[duh]



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

M2



Report •

#5
January 22, 2007 at 09:51:13
If you want to go with a Perl script, it could be simplfied.

=============================================

#!perl

use File::Copy;

$src_dir = 'c:/logs';
$dst_dir = 'c:/oldlogs';

foreach (<$src_dir/*.log>) { move $_, $dst_dir if -M > 7 }



Report •

#6
January 22, 2007 at 10:03:27
"If you want to go with a Perl script, it could be simplfied."

Sure, if you want to install PERL. I could also give a PHP script, but why? VBScrpt will run natively on XP.

Besides, IMHO, a verbose script is better for such tasks. It makes understaning, editing and modifying the code much easier. But, to each his own.

Michael J


Report •

#7
January 22, 2007 at 11:25:29
Michael,

I could be misinterpreting your response to my post, but it seems a little hostile. Why?

I did not attack you or your solution. I simply said that it [the solution] could be simplified with the use of Perl. Jbarnie didn't specify the choice of language and Perl is just as valid as VB Script. Granted, Perl isn't native to Windows, but it is becoming more and more common for people to add it to their collection of scripting utilities.

This is a very simple task so why would making the script more verbose and complex than needed be best?. Neither one of us included the proper amount of error handling, so I'll leave that subject alone.

Making a script more verbose doesn't always make it easier to understand, edit, modify (hmm...isn't that the same as edit) and in this case, IMO, VB Script has syntactic noise which makes it harder to read/understand. But, to each his own.

Ron


Report •

#8
January 22, 2007 at 11:57:10
@ FishMonger,

Sorry if you took that as hostile - was not my intention. But, without the OP stating that a particular environment is already installed I felt that providing a solution that would be guaranteed to run was the obvious choice.

I believe that if someone is going to provide a solution that requires a certain environment, then it should be stated.

As for making the code more verbose, I think it definitely makes it easier to work with the code. If I was going for efficiency I would have made it more compact. But, seeing as this is a relatively simple task, that was not necessary. With more verbose code you can identify the action taking place at each step so that you can easily make modifications.

Again, that is my opinion. And, as I stated at the end of my last post "to each his own".

Michael J


Report •

#9
January 22, 2007 at 12:26:45
Verbosity is the Microsoft way...let's use a paragraph instead of the cleaner, and easier to read sentence. :o)

There are times when verbosity is a must, but even then, you want to keep it within reason. I was helping someone in a (Perl) forum that needed to extract the current date and time and format it in a precise way. I posted a 2 line solution (several others posted almost the exact same solution) that was very easy to read, understand, and modify. Another person posted a more verbose solution that was 125 lines and used 4 separate subroutines. It was so messy that even experienced Perl programmers had a hard time following what it was doing.

Amazingly, the person needing the solution accepted the more verbose solution, even though he didn't understand it, simply because it was posted 5 minutes prior to the others. :(


Report •


Ask Question