Solved

Script to rename log files

Posted on 2004-03-23
3
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

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…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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