Solved

Script to rename log files

Posted on 2004-03-23
3
320 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
Comment Utility
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
Comment Utility
That works a treat.

Many thanks
Dave
0
 
LVL 16

Expert Comment

by:JamesDS
Comment Utility
welcome

Useful too, I will be using it here!
Cheers

JamesDS
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

15 Experts available now in Live!

Get 1:1 Help Now