Hi,
I'm having sporadic problems with a code to copy and rename photos.
This code is supposed to loop around a directory of photos (named after a project), and then copy a new photos (from another directory) to this folder, numbering them and the logging them in a table tphotos.
The first time a copy photos to a new directory the code works fine. And I think I had it working fine before, but now when I copy photos to a pre-existing directory that has already photos, it only overwrites the first photo, instead looking for the last photo and attributing the new photo number n+1.
I've done debug.print in the code, and it looks like is not going into If NewFileName = f1.Name, therefore doesn't compare the newfilename with the files in the folder.
Does anyone can help me understanding why this only works when I copy the photos the first time.:banghead:
I'm having sporadic problems with a code to copy and rename photos.
This code is supposed to loop around a directory of photos (named after a project), and then copy a new photos (from another directory) to this folder, numbering them and the logging them in a table tphotos.
The first time a copy photos to a new directory the code works fine. And I think I had it working fine before, but now when I copy photos to a pre-existing directory that has already photos, it only overwrites the first photo, instead looking for the last photo and attributing the new photo number n+1.
I've done debug.print in the code, and it looks like is not going into If NewFileName = f1.Name, therefore doesn't compare the newfilename with the files in the folder.
Does anyone can help me understanding why this only works when I copy the photos the first time.:banghead:
Code:
Private Sub cmdAddPhoto_Click()
Dim fs, f, f1, fc, s
Dim fDial As FileDialog
Dim SourcefileName, destDir, FileExt, NewFileName, prjID As String
Dim intSel, FileNumber As Integer
Dim varSelFile As Variant
Dim rsPhoto As Recordset
Dim myDB As Database
Set fDial = Application.FileDialog(msoFileDialogFilePicker)
Set fs = CreateObject("Scripting.FileSystemObject") 'here comands access to open an object which is the destination directory
Set f = fs.GetFolder(Me.tpath.Value)
Set fc = f.Files
Set myDB = CurrentDb
prjID = Me.cPrj.Value
destDir = Me.tpath.Value
FileNumber = 0
With fDial
.Title = "Choose Files to copy to " & destDir
.Filters.Add "All image files", "*.bitmap; *.BMP; *.CDR; *.CPT; *.CR2; *.Exif; *.GIF; *.ICO; *.icon; *.j2k; *.jp2; *.jpc; *.JPEG; *.jpg; *.jpx; *.pcc; *.pcx; *.pmm; *.psd; *.psp; *.RAW; *.tif; *.TIFF; *.wmf; *.xbm; *.XCF; *.xpm", 1
.AllowMultiSelect = True
.InitialFileName = "C:\"
If .Show = -1 Then
For Each varSelFile In .SelectedItems 'for each of the selected items
SourcefileName = Dir(varSelFile, vbDirectory) 'find the name of the file
FileExt = (Mid(SourcefileName, (InStr(1, SourcefileName, ".")))) 'find the file extension to add to the later copy
1 NewFileName = prjID & Format(FileNumber, "000") & FileExt 'atributes a new temporary file name - newfilename
For Each f1 In fc
If NewFileName = f1.Name Then ' loop and compares the newfilename with the files in the destination directory, to ensure is not overwriting.
FileNumber = FileNumber + 1 ' if finds a match, adds one value to the photo number filenumber = filenumber + 1
GoTo 1
End If 'if doesn't find a match, loops until last file and copies the file with the new name that is referenced to the project
Next
FileCopy varSelFile, destDir & NewFileName
Set rsPhoto = myDB.OpenRecordset("SELECT [tPhotos].* From [tPhotos] WHERE photoID = null ", DB_OPEN_DYNASET)
With rsPhoto
.AddNew
rsPhoto!prjID.Value = Me.tPrjID.Value
rsPhoto!photoPath.Value = destDir & NewFileName
rsPhoto!photoAuthor.Value = Me.tAuthor.Value
rsPhoto!PhotoComment.Value = Me.tcomment.Value
rsPhoto!photoPlaceTaken.Value = Me.tPlace.Value
rsPhoto!photoName.Value = NewFileName
rsPhoto!PhotoLog.Value = Forms!flog_in.uName
.Update
.Close
End With
Next varSelFile
Else
End If
End With
Set fDial = Nothing
With Me.sfPhotoDetail.Form
.Filter = "prjID = " & Me.tPrjID.Value
.FilterOn = True
End With
Me.sfPhotoDetail.Requery
Exit Sub
error_HGen:
MsgBox "Error:(" & Err.Number & ") " & Err.Description, vbCritical
End Sub