Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Creating a script to change logs files to the creation date

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,38088
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Creating a script to change logs files to the creation date
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Creating a script to change logs files to the creation ... - 9/26/2006 4:54:09 AM   
  devinw1

 

Posts: 11
Score: 0
Joined: 9/26/2006
Status: offline
I am new to the scripting world and am needing some assistance on a project.  Currently I have some log files that get named monday.log, tuesday.log, etc...and every week we have to manually change the filename to the date it was created, ex 20060926.log.  I have been trying to come up with a way to automate this process, and have found a good method.  However this method will change everything in that particular folder.  I am hoping to find a way to select only the files with *day in the filename.  Here's what I have so far:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
   ("ASSOCIATORS OF {Win32_Directory.Name='c:\logs'} Where " _
       & "ResultClass = CIM_DataFile")

For Each objFile In FileList
   strDate = Left(objFile.CreationDate, 8)
   strNewName = objFile.Drive & objFile.Path & strDate & ".log"
   errResult = objFile.Rename(strNewname)
   
Next

Any suggestions would be greatly appreciated.  Thanks!

_____________________________

Devin
 
 
Post #: 1
 
 RE: Creating a script to change logs files to the creat... - 9/26/2006 5:40:20 AM  1 votes
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
   ("ASSOCIATORS OF {Win32_Directory.Name='c:\logs'} Where " _
       & "ResultClass = CIM_DataFile")

For Each objFile In FileList
   If InStr(objFile.Name, "day") > 0 Then
    strDate = Left(objFile.CreationDate, 8)
      strNewName = objFile.Drive & objFile.Path & strDate & ".log"
      errResult = objFile.Rename(strNewname)
  End If
   
Next

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to devinw1)
 
 
Post #: 2
 
 RE: Creating a script to change logs files to the creat... - 9/26/2006 5:51:12 AM   
  devinw1

 

Posts: 11
Score: 0
Joined: 9/26/2006
Status: offline
That worked!  Thanks for your help

_____________________________

Devin

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: Creating a script to change logs files to the creat... - 9/26/2006 8:32:05 AM   
  devinw1

 

Posts: 11
Score: 0
Joined: 9/26/2006
Status: offline
One more minor detail that I was hoping to figure out.  What if I would want to move the created file to a server.  Example \\servername\logs .  Thanks

_____________________________

Devin

(in reply to ebgreen)
 
 
Post #: 4
 
 RE: Creating a script to change logs files to the creat... - 9/26/2006 7:49:32 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
how would you get it to run on a server share, it runs fine locally. e.g. c:\logs
One other question where I have the name logon if the file name is Logon and I change it in the script to Logon it wont rename it, why's that? (it needs to be in lower case even though the name is in upper case)

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")


Set FileList = objWMIService.ExecQuery _
   ("ASSOCIATORS OF {Win32_Directory.Name='\\server\Share'} Where " _
       & "ResultClass = CIM_DataFile")

For Each objFile In FileList
   If InStr(objFile.Name, "logon") > 0 Then                       'What does > 0 do
   strDate = Left(objFile.CreationDate, 8)                        'Is this how old created in days?
   strNewName = objFile.Drive & objFile.Path & _
      strDate & "." & "log"
   strNameCheck = Replace(strNewName, "\", "\\")
End If
   i = 1
   Do While True
       Set colFiles = objWMIService.ExecQuery _
           ("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
       If colFiles.Count = 0 Then
           errResult = objFile.Rename(strNewName)
           Exit Do
       Else
           i = i + 1
           strNewName = objFile.Drive & objFile.Path & _
               strDate & "_" & i & "." & "log"
           strNameCheck = Replace(strNewName, "\", "\\")
       End If
   Loop
Next

for devinw1 check out http://www.microsoft.com/technet/scriptcenter/resources/qanda/dec04/hey1209.mspx on how to move it. The scripting guy explains it.

(in reply to ebgreen)
 
 
Post #: 5
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 12:45:47 AM   
  devinw1

 

Posts: 11
Score: 0
Joined: 9/26/2006
Status: offline
The scripting guy was actually where I got the first part of my script.  I wasn't worried about the second half, mainly because all of the files would be different names.  As far as moving the file from one location to another...the scripting guy talks about moving it locally, but not to a server location.  Thanks for the post, and let me know if you know the syntax to move something from a local directory to a server location.  Thanks!

_____________________________

Devin

(in reply to gdewrance)
 
 
Post #: 6
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 1:03:37 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Did you try doing it the same way as moving it locally but using the UNC path? Experimentatation will teach you far faster than we can.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to devinw1)
 
 
Post #: 7
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 1:07:58 AM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
yip, tried that didnt work.
Has it got somthing to do with Win32_Directory.Name=

(in reply to ebgreen)
 
 
Post #: 8
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 1:14:19 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Please post the code that you are running along with the specific error that it gives you.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to gdewrance)
 
 
Post #: 9
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 1:23:59 AM   
  devinw1

 

Posts: 11
Score: 0
Joined: 9/26/2006
Status: offline
After adding the additional step at the bottom (that checks to make sure the file doesn't already exist..and if it does it will add _2 to the end), the script is back to changing all of the .log files in that folder.  Also, it will only move one of the files to the c:\logs\logs\ directory.  I'm thinking that we need to put the "If InStr(objFile.Name, "day) > 0 Then" statement somewhere in the second half of the script.  Ideas?  Thanks


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set FileList = objWMIService.ExecQuery _
   ("ASSOCIATORS OF {Win32_Directory.Name='c:\logs'} Where " _
   & "ResultClass = CIM_DataFile")
For Each objFile In FileList
  If InStr(objFile.Name,"day") > 0 Then
   strDate = Left(objFile.CreationDate, 8)
    strNewName = objFile.Drive & "\logs\logs\" & _
    strDate & "." & "log"
  strNameCheck = Replace(strNewName, "\", "\\")
    
  End If
 
  i = 1
  Do While True
      Set colFiles = objWMIService.ExecQuery _
          ("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
      If colFiles.Count = 0 Then
         errResult = objFile.Rename(strNewName)
          Exit Do
      Else
          i = i + 1
          strNewName = objFile.Drive & objFile.Path & _
          strDate & "_" & i & "." & "log"
          strNameCheck = Replace(strNewName, "\", "\\")
      End If
  Loop
Next

_____________________________

Devin

(in reply to gdewrance)
 
 
Post #: 10
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 1:43:06 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
I would drop WMI altogether. I don't see where it is providing any added by using WMI. This script is written off the cuff so there may be (probably are) errors in it but it should get you satrted.

Option Explicit

Dim oFSO
Dim oFile
Dim colFiles
Dim strDate
Dim strNewName

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = oFSO.GetFolder("C:\Logs").Files
For Each oFile in colFiles
   If InStr(oFile.Name, "day", 1) > 0 Then
       strDate = Left(oFile.CreationDate, 8)
       strNewName = strDate & "." & "log"
       oFile.Name = strNewName
       oFSO.MoveFile oFile.FullName, "\\SomeServer\SomeShare"
   End If
Next

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to devinw1)
 
 
Post #: 11
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 2:03:43 AM   
  devinw1

 

Posts: 11
Score: 0
Joined: 9/26/2006
Status: offline
How about this error:
Line:    12
Char:    4
Error:    Type mismatch: '[string:"Tuesday.log"]'
Code:   800A000D
Source:    Microsoft VBScript runtime error.

_____________________________

Devin

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 2:11:02 AM   
  DiGiTAL.SkReAM


Posts: 1170
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
WMI = icky
If there were any way to do something WITHOUT using WMI, I would...

I would probably do it like this:


      



_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to devinw1)
 
 
Post #: 13
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 2:53:32 AM   
  devinw1

 

Posts: 11
Score: 0
Joined: 9/26/2006
Status: offline
Alright, any idea's on this error message:

Line:  1
Char:  51
Error:  Expected identifier
Code:  800A03F2

_____________________________

Devin

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 14
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 2:56:40 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Whack that last comma off the first line.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to devinw1)
 
 
Post #: 15
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 3:03:01 AM   
  devinw1

 

Posts: 11
Score: 0
Joined: 9/26/2006
Status: offline
After I that, and after I added a closing ) on the end of line 8, it gives this error:

Line:  5
Char:  1
Error:  Invalid procedure call or argument

_____________________________

Devin

(in reply to ebgreen)
 
 
Post #: 16
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 3:06:19 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Please post the exact code you are running and indicate which line is 5 for you.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to devinw1)
 
 
Post #: 17
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 3:08:05 AM   
  devinw1

 

Posts: 11
Score: 0
Joined: 9/26/2006
Status: offline
Dim oFSO, sSourceDir, sTargetDir, sFile, sNewName
Set oFSO = CreateObject("Scripting.FileSystemObject")
sSourceDir = "c:\logs"
sTargetDir = "\\servername\test"
For Each sFile In oFSO.GetFolder(sDir).Files
If InStr(LCase(sFile),"day") <> 0 Then
sNewName = Split(Replace(oFSO.GetFile(sFile).DateCreated,"/","")," ")(0) & ".log"
If oFSO.FileExists(sTargetDir & "\" & sNewName) Then
  sNewName = Split(Replace(oFSO.GetFile(sFile).DateCreated,"/","")," ")(0) & "_1.log"
End If
oFSO.GetFile(sFile).Move sTargetDir & "\" & sNewName
End If
Next

_____________________________

Devin

(in reply to ebgreen)
 
 
Post #: 18
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 3:38:26 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Change this:

For Each sFile In oFSO.GetFolder(sDir).Files

to this:

For Each sFile In oFSO.GetFolder(sSourceDir).Files

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to devinw1)
 
 
Post #: 19
 
 RE: Creating a script to change logs files to the creat... - 9/27/2006 4:57:34 AM   
  DiGiTAL.SkReAM


Posts: 1170
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Doggonnit!  Sorry about all the errors, I should have stated that it was aircode.
As you could probably tell. lol

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to ebgreen)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Creating a script to change logs files to the creation date Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts