VBScript to move file with wildcard, if it exists

user3157550 picture user3157550 · Jan 3, 2014 · Viewed 21k times · Source

I am attempting to create a script that checks for the existence of archived eventlog files and, if any files exist, moves them to another folder. Running this script does nothing and gives no errors. I believe the wildcard in the If statement is what is giving me issues. I am new to vbscript, and scripting in general, and would appreciate some advice.

Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists("d:\eventlogs\Archive*.evtx")) Then
    FSO.CopyFile "d:\eventlogs\Archive*.evtx" , "d:\eventlogs\archive\"
    FSO.Deletefile "d:\eventlogs\archive*.evtx"
End if

Answer

Ahmad El-Hoss picture Ahmad El-Hoss · Apr 9, 2015

You can replicate a wild card search by using a combination of instr() and right(), or just multiple instr().

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "d:\eventlogs\"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
   if instr(objFile.Name,"Archive") <> 0 AND instr(objFile.Name,".evtx") <> 0 then
       objFSO.MoveFile objFile.Name, "archive\" + objFile.Name
   end if
Next