[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Add basic error handling to vsbscript

Posted on 2004-11-29
5
Medium Priority
?
458 Views
Last Modified: 2008-01-09
I have some vbs script that converts filenames from a hex number to a decimal in a local folder c:\jpg. I am trying to run this script on on about 10gb of 20-30k images but it has now stopped with the error message"
ERROR:FILE ALREADY EXISTS
CODE: 800A003A
SOURCE: Microsoft VBScript runtime error
it has processed some of the files and if i start it again i get the same error message.

I am after some code that will skip the problem filename and carry on. logginf that filename to an error log would be good also.

Cananyone help 500 points for this as its v-urgent.

My script loooks like this

Dim fso, folder, oRe, oMatch, oMatches, i , j, d, s

folder="e:\jpg" 'folder to process
'folder=WScript.Arguments(0) 'uncomment this line to pass the folder name as command line argument

Set re = New RegExp
re.Pattern = "^([0-9A-Z]{8})\.JPG$"
re.IgnoreCase = True  

Set fso = createobject("scripting.filesystemobject")
For each f in fso.getfolder(folder).Files
  If re.Test(f.Name) Then 'file as a name like HHHHHHHH.JPG    
    i=CLng("&h" & Left(f.name,8)) 'convert to decimal
    If i>=0 and i<=999999999 Then
      d=CStr(i) 'convert to string
      d=String(9-Len(d),"0") & d 'pad with 0
      'calc check digit
      s=0
      For j=1 to 9
        s=s+(11-j)*CInt(Mid(d,j,1))
      Next
      s=(11-(s mod 11)) mod 11
      If s=10 Then
           d=d & "X"
      Else
        d=d & CStr(s)
      End If
      d = d & ".JPG"
'      wscript.echo "renaming " & f.name & " to " & d
      f.move fso.BuildPath(f.parentfolder.path, d)
    End If    
  End If  
Next
0
Comment
Question by:boldnbrash73
  • 2
3 Comments
 

Author Comment

by:boldnbrash73
ID: 12696736
I wasnt sure which area to post in as XP comes with the scripting host without the need for VB so could may be  covered in the  Windows XP section, but then again VBS is another flavour of Visual Basic so its going to fit in there too.  I''ve had no luck here yet so If you think ill get a better response then yes, please do move it.

thanks
Mark
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 2000 total points
ID: 12697181
At the top of your script, add the following line of code:

On Error Resume Next
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 12697243
As for logging, you could do something like this:


On Error Resume Next

Dim fso, folder, oRe, oMatch, oMatches, i , j, d, s
Dim errLog    '***** error log


folder="e:\jpg" 'folder to process
'folder=WScript.Arguments(0) 'uncomment this line to pass the folder name as command line argument

Set re = New RegExp
re.Pattern = "^([0-9A-Z]{8})\.JPG$"
re.IgnoreCase = True  

Set fso = createobject("scripting.filesystemobject")
Set errLog = fso.OpenTextFile("C:\myerrors.txt", 2, True)

For each f in fso.getfolder(folder).Files
  If re.Test(f.Name) Then 'file as a name like HHHHHHHH.JPG    
    i=CLng("&h" & Left(f.name,8)) 'convert to decimal
    If i>=0 and i<=999999999 Then
      d=CStr(i) 'convert to string
      d=String(9-Len(d),"0") & d 'pad with 0
      'calc check digit
      s=0
      For j=1 to 9
        s=s+(11-j)*CInt(Mid(d,j,1))
      Next
      s=(11-(s mod 11)) mod 11
      If s=10 Then
           d=d & "X"
      Else
        d=d & CStr(s)
      End If
      d = d & ".JPG"
'      wscript.echo "renaming " & f.name & " to " & d
      f.move fso.BuildPath(f.parentfolder.path, d)

      If Err.Number <> 0 Then
          'error occurred - log it
          errLog.WriteLine "Error renaming " & f.name & " to " & d
          Err.Clear
      End If

    End If    
  End If  
Next

         
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question