Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script to rename log files

Posted on 2004-03-23
3
Medium Priority
?
332 Views
Last Modified: 2010-05-18
I have been trying to write a script that does the following but have been failing miserably any help is greatly appreciated:

Log files from several servers will be moved onto a central share, the event logs will have names like server1_sys.txt, server2_sys.txt etc.

So what I need is a script that goes through the shared folder and renames all of the log files by placing the date in front of the name, so server1_sys.txt would become 20040323_server1_sys.txt. I have managed to hack a script together that can rename single files but I can't figure out how to make it go through all the files and only add the date to the beginning and keep the rest intact.

Here's the script I have so far (please don't laugh ;) ):


dtmThisDay = Day(Now)
dtmThisMonth = Month(Now)
dtmThisYear = Year(Now)
strBackupName = dtmThisYear & "_" & dtmThisMonth & "_" & dtmThisDay
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set filebackup = objWMIService.ExecQuery _
    ("SELECT * FROM Cim_Datafile WHERE Name = " _
        & "'c:\\scripts\\server1_sys.txt'")
For Each objFile in filebackup
    errResult = objFile.Rename("c:\scripts\" & strBackupName & _
        "_server1_sys.txt'")
Next


Thanks in advance
Dave
0
Comment
Question by:davidjw
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
JamesDS earned 2000 total points
ID: 10660306
Here you go, fully tested and it works nicely
Take out the wscript.echo when you're happy :)

dtmThisDay = Day(Now)
dtmThisMonth = Month(Now)
dtmThisYear = Year(Now)
strBackupName = dtmThisYear & "_" & dtmThisMonth & "_" & dtmThisDay & "_"
strComputer = "."
strFolder = "C:\scripts\"

Set fso = CreateObject("Scripting.FileSystemObject")
Set CurrentFolder = fso.GetFolder(strFolder)

Set Files = CurrentFolder.Files

For Each File in Files
      WScript.Echo File.name
      File.move strFolder & strBackupName & File.name
Next

Cheers

JamesDS
0
 

Author Comment

by:davidjw
ID: 10665737
That works a treat.

Many thanks
Dave
0
 
LVL 16

Expert Comment

by:JamesDS
ID: 10665744
welcome

Useful too, I will be using it here!
Cheers

JamesDS
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
How to fix a SonicWall Gateway Anti-Virus firewall blocking automatic updates to apps like Windows, Adobe, Symantec, etc.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question