Link to home
Start Free TrialLog in
Avatar of oconnork00
oconnork00Flag for United States of America

asked on

VB script Runtime Error

Hi all,

A while ago a great expert here helped me with a question about a script that renames a file when it gets exported into a folder. I have attached the code below.

The program runs fine up to a point where an error pops up:
script: D:\Keyword Elite Exports\FileMonitor.vbs
Line: 54
Char: 19
Error: Path not found
Code: 800A004c
Course: Microsoft VBScript runtime error



Now just to let you know that that folder does exist and it has been renaming files. It just seems to spit out this error every now and again - maybe 2mins sometimes 3mins or even less than a minute at times.


Can any experts here help stop this happening?

Thanks in advance

Kevin
OK, here's something for you to test......
 
Save the code I post below into a VBS file.
 
I know it looks complicated, but it does most of the work for you.
 
The only lines you need to modify are these two:
strProgramToRun = "notepad.exe"
strFolder = "N:\Scripting\Test Scripts\Rename Files Added to Folder"
 
where in strProgramToRun you specify the full path to the program executable, and in strFolder
you specify the full folder where you will be placing your output files from the program.
 
What this script will do is launch your program, and monitor the folder for any files that you output.  It will then rename a new file to <filename>_Auto####.<ext>
 
Then, when you have finished exporting files, close your program, and the script should quit itself, and no longer monitor the folder.
 
'==========================
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strProgramToRun = "notepad.exe"
strFolder = "N:\Scripting\Test Scripts\Rename Files Added to Folder"
Set objExec1 = objShell.Exec(strProgramToRun)
 
strThisScript = WScript.ScriptFullName
strMonitor = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "FileMonitor.vbs"
 
'***** FileMonitor.vbs code for this script to write *****
strCode = "If WScript.Arguments.Count = 0 Then" & VbCrLf & _
      "      MsgBox ""Script cannot run without arguements.""" & VbCrLf & _
      "      WScript.Quit" & VbCrLf & _
"End If" & VbCrLf & _
"" & VbCrLf & _
"Set objFSO = CreateObject(""Scripting.FileSystemObject"")" & VbCrLf & _
"" & VbCrLf & _
"strCallingScript = WScript.Arguments.Item(0)" & VbCrLf & _
"strFolder = WScript.Arguments.Item(1)" & VbCrLf & _
"strComputer = "".""" & VbCrLf & _
"intSeconds = 2" & VbCrLf & _
"" & VbCrLf & _
"Set objWMIService = GetObject(""winmgmts:\\"" & strComputer & ""\root\cimv2"")" & VbCrLf & _
"" & VbCrLf & _
"Set colMonitoredEvents = objWMIService.ExecNotificationQuery _" & VbCrLf & _
"      (""SELECT * FROM __InstanceCreationEvent WITHIN "" & intSeconds & "" WHERE "" _" & VbCrLf & _
"            & ""Targetinstance ISA 'CIM_DirectoryContainsFile' and "" _" & VbCrLf & _
"                  & ""TargetInstance.GroupComponent= "" _" & VbCrLf & _
"                        & ""'Win32_Directory.Name="""""" & Replace(strFolder, ""\"", ""\\\\"") & """"""'"")" & VbCrLf & _
"" & VbCrLf & _
"Do" & VbCrLf & _
"      Set objLatestEvent = colMonitoredEvents.NextEvent" & VbCrLf & _
"      strNewFile = objLatestEvent.TargetInstance.PartComponent" & VbCrLf & _
"      arrNewFile = Split(strNewFile, ""="")" & VbCrLf & _
"      strFileName = arrNewFile(1)" & VbCrLf & _
"      strFileName = Replace(strFileName, ""\\"", ""\"")" & VbCrLf & _
"      strFileName = Replace(strFileName, Chr(34), """")" & VbCrLf & _
"      If LCase(WScript.ScriptFullName) <> LCase(strFileName) And _" & VbCrLf & _
"      LCase(strCallingScript) <> LCase(strFileName) Then" & VbCrLf & _
"            'MsgBox ""New file added: "" & strFileName" & VbCrLf & _
"            strBaseName = objFSO.GetBaseName(strFileName)" & VbCrLf & _
"            boolRename = False" & VbCrLf & _
"            If Len(strBaseName) > 9 Then" & VbCrLf & _
"                  strCheckAuto = Right(strBaseName, 9)" & VbCrLf & _
"                  If Left(strCheckAuto, 5) <> ""_Auto"" Then" & VbCrLf & _
"                        boolRename = True" & VbCrLf & _
"                  End If" & VbCrLf & _
"            Else" & VbCrLf & _
"                  boolRename = True" & VbCrLf & _
"            End If" & VbCrLf & _
"            If boolRename = True Then" & VbCrLf & _
"                  For intNum = 0 To 9999" & VbCrLf & _
"                        If intNum < 10 Then" & VbCrLf & _
"                              strNewName = objFSO.GetParentFolderName(strFileName) & ""\"" & strBaseName & ""_Auto000"" & intNum & ""."" & objFSO.GetExtensionName(strFileName)" & VbCrLf & _
"                        ElseIf inNum < 100 Then" & VbCrLf & _
"                              strNewName = objFSO.GetParentFolderName(strFileName) & ""\"" & strBaseName & ""_Auto00"" & intNum & ""."" & objFSO.GetExtensionName(strFileName)" & VbCrLf & _
"                        ElseIf inNum < 1000 Then" & VbCrLf & _
"                              strNewName = objFSO.GetParentFolderName(strFileName) & ""\"" & strBaseName & ""_Auto0"" & intNum & ""."" & objFSO.GetExtensionName(strFileName)" & VbCrLf & _
"                        Else" & VbCrLf & _
"                              strNewName = objFSO.GetParentFolderName(strFileName) & ""\"" & strBaseName & ""_Auto"" & intNum & ""."" & objFSO.GetExtensionName(strFileName)" & VbCrLf & _
"                        End If" & VbCrLf & _
"                        If objFSO.FileExists(strNewName) = False Then Exit For" & VbCrLf & _
"                  Next" & VbCrLf & _
"                  objFSO.MoveFile strFileName, strNewName" & VbCrLf & _
"            End If" & VbCrLf & _
"      End If" & VbCrLf & _
"Loop"
'***** End of FileMonitor code *****
 
Set objFileMonitor = objFSO.CreateTextFile(strMonitor, True)
objFileMonitor.Write strCode
objFileMonitor.Close
Set objFileMonitor = Nothing
 
Set objExec2 = objShell.Exec("wscript """ & strMonitor & """ """ & strThisScript & """ """ & strFolder & """")
 
While objExec1.Status = 0
      WScript.Sleep 100
Wend
 
objExec2.Terminate
MsgBox "Program has been closed. Folder will no longer be monitored."
objFSO.DeleteFile strMonitor, True
'==========================
 
Regards,
 
Rob.

Open in new window

Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, in the full code, try changing line 82 from this:
"                  objFSO.MoveFile strFileName, strNewName" & VbCrLf & _

to this
"                  If objFSO.FileExists(strFileName) = True Then objFSO.MoveFile strFileName, strNewName" & VbCrLf & _

which should check if the file still exists before renaming it.

Keep an eye on that folder though, to make sure it still renames correctly.

Regards,

Rob.
Avatar of oconnork00

ASKER

Hi Rob,
Thanks for stopping by!

Got a different error this time:

script: D:\Keyword Elite Exports\FileMonitor.vbs
Line: 54
Char: 65
Error: Path not found
Code: 800A004c
Course: Microsoft VBScript runtime error



Hi, sorry for the delay.  So we're going to need to do some debugging...change this:
"                  If objFSO.FileExists(strFileName) = True Then objFSO.MoveFile strFileName, strNewName" & VbCrLf & _


to this
"                  On Error Resume Next" & VbCrLf & _
"                  If objFSO.FileExists(strFileName) = True Then objFSO.MoveFile strFileName, strNewName" & VbCrLf & _
"                  If Err.Number <> 0 Then MsgBox ""Could not move "" & strFileName & "" to "" & strNewName" & VbCrLf & _
"                  Err.Clear" & VbCrLf & _
"                  On Error GoTo 0" & VbCrLf & _


and then you'll get a MsgBox each time this error would occur.  Then you can see if the source file actually exists, and see what it was trying to rename it to.

Regards,

Rob.
Hi Rob,

A message box appears, image attached.

Is it anything to do with the file names getting too long?
error.JPG
Oh bugger!  It's up to 10,000.  Is that right?  The code only accounts for up to 9999 at the moment....do you want to make it account for up to 999,999?  If we do that, all the numbering will start at
filename_Auto000001

with five zeros....

Does filename_Auto9999.ext exist in that folder?

Regards,

Rob.
Hi Rob,

No, there's no file with that name... I ahve attached a screen shot of all the files that were renamed before the error occured.

The error happened on the 5th file rename - the screen shot shows all the files that went through.

Thanks for the help Rob,

Kevin
error2.JPG
Hmmm, there's no way it should get to 10000, I'll do some testing....

Rob.
Thanks Rob, I truly appreciate your help.

Kevin.
OK, I set up another script to constantly place a new file in the watch folder every ten seconds, and eventually, I can see that as soon as it hits _Auto0100, it keep appending to the file name....bad!!

I'll attempt to fix that now...

Regards,

Rob.
why can't you just have it add a numer w/out all the elseif

do until file doesn't exist
filename & x
x = x +1
loop

Weellio, because we are padding the name to always have four digits with the numbers.....
I though about using my Pad_String function, but to add that to the self-created script file would be a bit cumbersome.

I've just fixed it now anyway, by checking the lower bound in the Else If statements as well.  I had to wait for my testing script to dump over 1000 files in there while this processes them.  I'll post that in a minute....

Regards,

Rob.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great code Rob, thanks a billion. It is working great, and saving me hours of time.

Thanks
Kevin
No problem.  Sorry I didn't test it right the first time round.

Regards,

Rob.