General discussion

Locked

VB Problem

By khall3 ·
This is a question I posted about a month ago and I closed it because I thought everything was working great. However, I didn't notice one problem.

"Ok here is the problem. I have a directory that contains over 35,000 pictures that correspond to products. Each filename is associated with a particular item number. For example, item 00001 has a picture file by the name of 00001.jpg. This is the same for all 35,000 or so pictures. The company has recently been bought by another company that uses the same products but different item numbers. So what I need is a program that will copy the pictures from the picture directory and move them into a different directory with the new companys item number. Example, company 1 has 00001.jpg for item number 00001, company 2 needs 00001.jpg for item number 12345 so the file must be copied and renamed as 12345.jpg. I realize this might be hard to understand so if you need some more explanation let me know."

Nikki96 gave me this code that worksfairly well. However, if the file is listed in the text file and not in the directory the program stops. I need an error checking section that will allow the program to continue if the file is not found.

Private Sub cmdCopy_Click()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts, s, a
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile("C:\Documents and Settings\me\My Documents\VBapps\imageCopy\list.txt")
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
While Not ts.AtEndOfStream
a = Split(ts.ReadLine, ",")
fs.CopyFile a(0), a(1)
Wend
ts.Close
End Sub

This conversation is currently closed to new comments.

8 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

VB Problem

by khall3 In reply to VB Problem

well i have a text file that is set up like this

c:\ghi\00001.jpg, c:\hig\12345.jpg
c:\ghi\00002.jpg, c:\hig\12346.jpg
c:\ghi\00004.jpg, c:\hig\98723.jpg

the numbers on the left of the comma are from one company and the numbers on the right are from the other company. so it must pull 00001.jpg and copy, rename, and move it to 12345.jpg in a different directory.

Collapse -

VB Problem

by MedievalDude In reply to VB Problem

This might be too basic of a solution and not what you want, but I'll try. How about using:
On Error Resume Next

You could at that point also check for your specific error with something like:
... (your code)
fs.CopyFile a(0), a(1)
If Err.Number > 0 Then
MsgBox(Err.Description)
' or do something else
End If
Wend
ts.Close
On Error Goto 0 ' turn off error resume
End Sub

(I'm assuming that the error comes with the copyfile - if not, put the if statement under the line that gives the error.)

Collapse -

VB Problem

by khall3 In reply to VB Problem

Poster rated this answer

Collapse -

VB Problem

by nikki96 In reply to VB Problem

This'll do ya up.

Private Sub cmdCopy_Click()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts, s, a
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile("C:\Documents and Settings\hunn\My Documents\VBapps\imageCopy\list.txt")
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
While Not ts.AtEndOfStream
a = Split(ts.ReadLine, ",") On Error GoTo errorhandler
fs.CopyFile a(0), a(1)
Wend
GoTo endIt
errorhandler:
a = MsgBox("Error: Number " & Err.Number & vbLf & "Description: " & Err.Description & vbLf & "Exit?", vbYesNo)
If a = vbYes Then
GoToendIt
Else
Resume Next
End If
endIt:
ts.Close
End Sub

Collapse -

VB Problem

by khall3 In reply to VB Problem

Thank you for your help again.

Collapse -

VB Problem

by Chris.Lewis In reply to VB Problem

Real easy check before the copy will eliminate your problem:

If fs.FileExists(a(0)) Then
fs.CopyFile a(0), a(1)
Else
'File does not exists, log error, msgbox, etc.
End if

You might also want to check a(1) to make sure you are not overwriting it.

Collapse -

VB Problem

by khall3 In reply to VB Problem

Poster rated this answer

Collapse -

VB Problem

by khall3 In reply to VB Problem

This question was closed by the author

Back to Web Development Forum
8 total posts (Page 1 of 1)  

Related Discussions

Related Forums