Ok, I wrote the script below to rip files from a CD and if the filename match a regular expression them move the file to network folder A and write to a file otherwise move to network folder B and write to an error log. My disk drive is D:\ and everything works fine however when I gave this script to a user his CD drive was G:\. So I modified the script to read from G:\. This produces a Permission Denied Error on line 47 (the first MoveFile method instance).
Now what's weird is that I went back to my PC and changed the letter for the CD drive from D: to G: and then I got the same error. I rebooted my machine and then it worked. What could this be? Rebooting the user machine still produces the same error.
PS. If I use D:\ on the user's machine the script works (at least no errors are present), however D: is not the CD drive for the user so I'm still in the same boat.
Now what's weird is that I went back to my PC and changed the letter for the CD drive from D: to G: and then I got the same error. I rebooted my machine and then it worked. What could this be? Rebooting the user machine still produces the same error.
PS. If I use D:\ on the user's machine the script works (at least no errors are present), however D: is not the CD drive for the user so I'm still in the same boat.
Code:
Option Explicit
Dim objFSO
Dim objFolder
Dim objFolder2
Dim colFiles
Dim objSubFile
Dim strFolder
Dim strFolder2
Dim File
Dim objFile
Dim path
Dim filetxt
Dim WriteStuff
Dim NewString
Dim errorlog
Dim WriteLog
Dim ErrorString
Dim E
strFolder = "G:\"
strFolder2 ="\\optical-servr\Imaging\PPOutput\Production\Drawings\Errors"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
Set colFiles = objFolder.Files
filetxt= "\\optical-servr\Imaging\PPOutput\Production\Drawings\FortisIndex.txt"
errorlog= "\\optical-servr\Imaging\PPOutput\Production\Drawings\Errors\ErrorLog.txt"
Set WriteStuff = objFSO.OpenTextFile(filetxt, 2, True)
For Each File in colFiles
set objFile = objFSO.GetFile(strFolder & "\" & File.Name)
If DateDiff("d", objfile.DateLastModified, Now) < 834 Then
Dim RegEx : Set RegEx = New RegExp
RegEx.Pattern = "[A-Z]+:\\((\w+)\s(\w+)\s(\w+)\s(\w+)\.\w+)"
RegEx.IgnoreCase = True
Dim strTemp : strTemp = objFile
If RegEx.Test(strTemp) Then
NewString = RegEx.Replace(strtemp, """$2$3"",""$4"","""","""",""$5"","";\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\$1""")
WriteStuff.WriteLine(NewString)
objFSO.MoveFile objFile,"\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\"
Else
RegEx.Pattern = "([A-Z]+:\\((\w+)\s(\w+)\s(\w+)\.\w+))" 'D:\F50B721747090D1 F01 1.tif
If RegEx.Test(strTemp) Then
NewString = RegEx.Replace(strtemp, """$3"",""$4"","""","""",""$5"","";\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\$2""")
WriteStuff.WriteLine(NewString)
objFSO.MoveFile objFile,"\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\"
Else
RegEx.Pattern = "([A-Z]+:\\(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\.\w+)"
If RegEx.Test(strTemp) Then
NewString = RegEx.Replace(strtemp, """$2$3$4$5$6"",""$7"","""","""",""$8"","";\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\$1""")
WriteStuff.WriteLine(NewString)
objFSO.MoveFile objFile,"\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\"
Else
'D:\FGFD-212060A1-C05-A3.tif
RegEx.Pattern = "([A-Z]+:\\+(\w+)\-(\w+)\-(\w+)\-(\w+)\.\w+)"
If RegEx.Test(strTemp) Then
NewString = RegEx.Replace(strtemp, """$2$3"",""$4"","""","""",""$5"","";\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\$1""")
WriteStuff.WriteLine(NewString)
objFSO.MoveFile objFile,"\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\"
Else
RegEx.Pattern = "([A-Z]+:\\(\w+)\s(\w+)\s(\w+)\s(\w+)\s([A-Z]+\d+\d+)\s(\d+).\w+)"
If RegEx.Test(strTemp) Then
NewString = RegEx.Replace(strtemp, """$2$3$4$5"",""$6"","""","""",""$7"","";\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\$1""")
WriteStuff.WriteLine(NewString)
objFSO.MoveFile objFile,"\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\"
Else
RegEx.Pattern = "[A-Z]+:\\((\w+)\s(\w+)([A-F]+\d+\d)\s(\d+)\.\w+)"
If RegEx.Test(strTemp) Then
NewString = RegEx.Replace(strtemp, """$2$3"",""$4"","""","""",""$5"","";\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\$1""")
WriteStuff.WriteLine(NewString)
objFSO.MoveFile objFile,"\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\"
Else
RegEx.Pattern = "[A-Z]+:\\((\w+)\s(\w+)\s(\w+)\s(\w+)\s(\w+)\.\w+)"
If RegEx.Test(strTemp) Then
NewString = RegEx.Replace(strtemp, """$2$3$4"",""$5"","""","""",""$6"","";\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\$1""")
WriteStuff.WriteLine(NewString)
objFSO.MoveFile objFile,"\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\"
Else
RegEx.Pattern = "[A-Z]+:\\((\w+[a-zA-Z]+\d+)([A-Z]+\d+\d)\s(\d+)\.\w+)"
If RegEx.Test(strTemp) Then
NewString = RegEx.Replace(strtemp, """$2"",""$3"","""","""",""$4"","";\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\$1""")
WriteStuff.WriteLine(NewString)
objFSO.MoveFile objFile,"\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\"
Else
RegEx.Pattern = "[A-Z]+:\\((\w+[a-zA-Z]+\d+)-?([A-Z]+\d+\d)-?(\d+)\.\w+)" 'D:\F2MB700735442A3C01-1.tif
If RegEx.Test(strTemp) Then
NewString = RegEx.Replace(strTemp, """$2"",""$3"","""","""",""$4"","";\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\$1""")
WriteStuff.WriteLine(NewString)
objFSO.MoveFile objFile,"\\optical-servr\Imaging\PPOutput\Production\Drawings\To Fortis\"
Else
Dim Wshshell
Dim strUserName
Set WshShell = CreateObject("Wscript.Shell")
strUserName = WshShell.ExpandEnvironmentStrings("%username%")
Set WriteLog = objFSO.OpenTextFile(errorlog, 8, True)
ErrorString = Now() &" User: " & strUserName &" : Filename Not Validated by Regex - " & objFile.Path
WriteLog.WriteLine(Errorstring)
WriteLog.close
objFSO.MoveFile objFile,"\\optical-servr\Imaging\PPOutput\Production\Drawings\Errors\"
Set WshShell = Nothing
Set strUserName = Nothing
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
Next
Set objFolder2 = objFSO.GetFolder(strFolder2)
E = objFolder2.Files.Count
If E > 2 Then
MsgBox "Processing has completed with " & (E-2) &" error(s). MIS has been notified, no further action required."
Else
Msgbox "Processing has Finished. Ok to remove CD from Drive"
End If
set objFSO = Nothing
set objFolder = Nothing
set objFolder2 = Nothing
set colFiles = Nothing
set objSubFile = Nothing
set strFolder = Nothing
set strFolder2 = Nothing
set File = Nothing
set objFile = Nothing
set path = Nothing
set filetxt = Nothing
set WriteStuff = Nothing
set NewString = Nothing
set errorlog = Nothing
set WriteLog = Nothing
set ErrorString = Nothing
Set E = Nothing
wscript.Quit