I have recently run into a situation where a co-worker needed a script to move a file that is created from a nightly process to another directory for archival purposes. He wanted the file to be renamed with the creation date.
Years ago I had to create a vbs for a similar process, so I followed this guide from the scripting guy: And I came up with this script.
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 & "logs\" & strDate & "." & "txt"
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 & objFile.Path & _
strDate & "_" & i & "." & "txt"
strNameCheck = Replace(strNewName, "\", "\\")
End If
Loop
Next
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "\d{0}"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Logs\")
Set colFiles = objFolder.Files
For Each objFile in colFiles
strSearchString = objFile.Name
Set colMatches = objRegEx.Execute(strSearchString)
For Each strMatch in colMatches
strFolderName = strMatch.Value
strFolderName = Replace(strFolderName, "_", "")
strFolderName = "c:\Logs\backups" & strFolderName & strDate & "\"
If Not objFSO.FolderExists(strFolderName) Then
Set objNewFolder = objFSO.CreateFolder(strFolderName)
End If
Next
objFSO.MoveFile objFile.Path, strFolderName
Next
What this script does is lists all files under c:\Logs and puts them in a Backup folder according to its creation date. For instance a file that was copied to C:\Logs on 10/31/2014 will be archived to C:\Logs\Backup20141031. It will not change the name of the file. I will simply put all files that were copied to C:\Logs into a directory called backup+ the date that is was copied to C:\logs.
Years ago I had to create a vbs for a similar process, so I followed this guide from the scripting guy: And I came up with this script.
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 & "logs\" & strDate & "." & "txt"
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 & objFile.Path & _
strDate & "_" & i & "." & "txt"
strNameCheck = Replace(strNewName, "\", "\\")
End If
Loop
Next
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "\d{0}"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Logs\")
Set colFiles = objFolder.Files
For Each objFile in colFiles
strSearchString = objFile.Name
Set colMatches = objRegEx.Execute(strSearchString)
For Each strMatch in colMatches
strFolderName = strMatch.Value
strFolderName = Replace(strFolderName, "_", "")
strFolderName = "c:\Logs\backups" & strFolderName & strDate & "\"
If Not objFSO.FolderExists(strFolderName) Then
Set objNewFolder = objFSO.CreateFolder(strFolderName)
End If
Next
objFSO.MoveFile objFile.Path, strFolderName
Next
What this script does is lists all files under c:\Logs and puts them in a Backup folder according to its creation date. For instance a file that was copied to C:\Logs on 10/31/2014 will be archived to C:\Logs\Backup20141031. It will not change the name of the file. I will simply put all files that were copied to C:\Logs into a directory called backup+ the date that is was copied to C:\logs.