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!
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
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.
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: