Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Rename and move files base on extension

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Rename and move files base on extension
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Rename and move files base on extension - 12/20/2007 10:54:45 AM   
  azimzores

 

Posts: 8
Score: 0
Joined: 12/20/2007
Status: offline


Hey guys I have searched this forum along with other scripting tutorials and have been left empty handed.
What I am basicly trying to do is rename the logs to todays date an move them to a diffrent folder.
Below is the script that i have currently the main problem i have is it renames all files in the source folder not just .log files





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

Set FileList = objWMIService.ExecQuery _
   ("ASSOCIATORS OF {Win32_Directory.Name='C:\Documents and Settings\svc-user\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data'} Where " _
       & "ResultClass = CIM_DataFile")

For Each objFile In FileList
   strDate = date()
   strNewName = objFile.Drive & "\backup\" & "Backup Log " & strDate & ".log"
   strNameCheck = Replace(strNewName, "\", "\\")

    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 & "\backup\" & "Backup Log " & _
               strDate & "_" & i & "." & "log"
           strNameCheck = Replace(strNewName, "\", "\\")
       End If
   Loop
Next







Any help is appreciated only thing i need the script to do is only grab the log files not every file in the directory
 
Tony
 
 
Post #: 1
 
 RE: Rename and move files base on extension - 12/20/2007 11:01:51 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi azimzores,

I fed

  "ResultClass = CIM_DataFile" extension

to Google; the first link found was

   http://www.microsoft.com/technet/scriptcenter/guide/sas_fil_ygwy.mspx?mfr=true

I wonder, if that would be of help.

Good luck!

ehvbs

(in reply to azimzores)
 
 
Post #: 2
 
 RE: Rename and move files base on extension - 12/20/2007 11:13:53 AM   
  azimzores

 

Posts: 8
Score: 0
Joined: 12/20/2007
Status: offline
Ye i have already  looked at that and it does not help, I have got the script to rename a .log file but since i am renaming to the date if there is moer then one log file it crashes, that why i am incrementing the filename with _#. Thanks for the post but still trying to fix the issue.

Tony

(in reply to ehvbs)
 
 
Post #: 3
 
 RE: Rename and move files base on extension - 12/20/2007 11:47:09 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi azimzores,

ad your problem: "it renames all files in the source folder not just .log files "

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" ...
    Set FileList = objWMIService.ExecQuery ...
    For Each objFile In FileList
        If objFile.Extension = "log" Then
            ... do what you want with .log files only ....
        End If
    Next

ad renaming: I don't understand from your posts, whether this works
or not. If not, put some WScript.Echo strNewName in your code and
check.

Good luck!

ehvbs

P.S. I knew it was there, but it took some time to find it:

    http://www.visualbasicscript.com/fb.aspx?m=41267

deals with the rename problem and uses the FSO which is much easier on mind
and machine as the WMI.

< Message edited by ehvbs -- 12/20/2007 11:52:25 AM >

(in reply to azimzores)
 
 
Post #: 4
 
 RE: Rename and move files base on extension - 1/4/2008 4:42:15 AM   
  azimzores

 

Posts: 8
Score: 0
Joined: 12/20/2007
Status: offline
BTW forgot to post back, thanks for the help was a simple If statement in the wrong location, moved it and everything fel into place.

Thanks again

(in reply to ehvbs)
 
 
Post #: 5
 
 
 
  

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 >> Rename and move files base on extension Page: [1]
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