Solved

Script to rename log files

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 2017, ransomware will become so virulent and widespread that if you aren’t a victim yourself, you will know someone who is.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now