Solved

Script to rename log files

Posted on 2004-03-23
3
325 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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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