opps! typo error in previous post, loop through and return highest file
opps! typo error in previous post, loop through and return highest file
am 18.10.2007 14:48:11 von dobbine
hi there folks,
I was wondering if I have a folder with the following files in it,
can
I return 00012ac.jpg?
00012.jpg
00012a.jpg
00012b.jpg
00012c.jpg
right up to ...
00012z.jpg
00012aa.jpg
00012ab.jpg
00012ac.jpg
Here is a snippet of my code:
'Begin filtering files in the directory for FILENUM pattern and parse
out the file name.
For Each file In strDestFolder.files
If Left(file.Name, 5) = Trim(myForm!txtFILENUM) Then
strFileName = Left(file.Name, Len(file.Name) - 4)
lngAlpha = Len(strFileName)
For lngI = lngAlpha To 1 Step -1
'(strFileName) 'This is where i'm stuck
Debug.Print lngAlpha
Debug.Print strFileName
Next lngI
End If
Next file
End If
End If
'Name strInitFile As strDestFile
'FileCopy strSourceFile, strDestFile
Set fs = Nothing
End Sub
Any help or suggestions would be greatly, greatly appreciated.
Is this pattern of a,b,c the best to use? I could probably rename all
the files using 001,002,003 instead.
Re: opps! typo error in previous post, loop through and return highestfile
am 18.10.2007 17:13:02 von Salad
dobbine@gov.nl.ca wrote:
> hi there folks,
>
> I was wondering if I have a folder with the following files in it,
> can
> I return 00012ac.jpg?
> 00012.jpg
> 00012a.jpg
> 00012b.jpg
> 00012c.jpg
> right up to ...
> 00012z.jpg
> 00012aa.jpg
> 00012ab.jpg
> 00012ac.jpg
>
>
> Here is a snippet of my code:
>
>
> 'Begin filtering files in the directory for FILENUM pattern and parse
> out the file name.
> For Each file In strDestFolder.files
> If Left(file.Name, 5) = Trim(myForm!txtFILENUM) Then
> strFileName = Left(file.Name, Len(file.Name) - 4)
> lngAlpha = Len(strFileName)
> For lngI = lngAlpha To 1 Step -1
> '(strFileName) 'This is where i'm stuck
> Debug.Print lngAlpha
> Debug.Print strFileName
> Next lngI
> End If
> Next file
> End If
> End If
>
>
> 'Name strInitFile As strDestFile
> 'FileCopy strSourceFile, strDestFile
>
>
> Set fs = Nothing
> End Sub
>
>
> Any help or suggestions would be greatly, greatly appreciated.
> Is this pattern of a,b,c the best to use? I could probably rename all
> the files using 001,002,003 instead.
>
I don't understand the question completely or what strDestFolder.files is.
You can do something like
intCnt = 0
strFile = Dir("C:\Test\00001*.JPG"
Do while strFile > ""
msgbox strFile
intCnt = intCnt + 1
strFile = Dir()
Loop
msgbox "there were " & intCnt " files."
What I don't understand is where you are selecting a specific file like
"00001C.JPG". You could stuff those values into a combo box or into a
table to show the selection I guess. What do you mean "highest file"?
Re: opps! typo error in previous post, loop through and return highest file
am 18.10.2007 20:15:30 von dobbine
Hi Salad,
If I have a folder with the following files:
00012.jpg
00012a.jpg
00012b.jpg
00012c.jpg
I would like to know how to return 00012c.jpg
I think I need to use the Chr() function to return the ANSI value to
do it but I don't know how
Maybe first Parse out the letter by striping off '00012' and '.jpg'
parts.
Here is more of my code
Sub AutoUpdate(strSourceFile)
Dim file, v
Dim fs, strDestFolder, strDestFile, strAlphaPart, strNumPart,
strDestFileNew, strFileName
Dim strAlphaInput As String
Dim dtmSourceFileDate As Date
Dim myForm As Form
Dim lngI As Long, lngFileName As Long
Dim varFile As Variant
Dim intFileName As Integer
Dim lngAlpha As Long
Set myForm = Screen.ActiveForm
'If the user has updated or added a photo in N:\wwwroot\WORKS
\BLDGPIC1 directory, Then...
'First check to see if the field PhotoDateModified in BLDGMSTR has
the same photo date if
'not then synchronize. Use the DateLastModified property of the
FileSystemObject as this
'property does not change when a file is copied from one directory
to another.
Set fs = CreateObject("Scripting.FileSystemObject")
Set strSourceFile = fs.getfile(strSourceFile) '=N:\wwwroot\WORKS
\BLDGPIC1\00012.jpg
dtmSourceFileDate = strSourceFile.DateLastModified
If dtmSourceFileDate <> myForm!txtPhotoDateLastModified Then 'A
new picture has been added to N drive
myForm!txtPhotoDateLastModified = dtmSourceFileDate
'Synchronize the field PhotoDateModified in table BLDGMSTR
v = myForm!cboVOLUME
If v = "9" Then
v = "0"
End If
Set strDestFolder = fs.GetFolder("F:\User\BLDG
\FacilitiesFilingCabinet\Volume" & Trim(v) & "\" & Trim(myForm!
txtBLDGNUM) & "\" & Trim(myForm!txtSUBNAME))
strDestFile = Dir(strDestFolder & "\" & Trim(myForm!
txtPHOTO)) '00012.jpg
'if the destination directory is empty then simply copy the
file
If strDestFile = "" Then
FileCopy strSourceFile, strDestFolder & "\" & Trim(myForm!
txtPHOTO)
Else 'otherwise begin AutoUpdate
'Begin filtering files in the directory for FILENUM pattern and
parse out the file name.
For Each file In strDestFolder.files
If Left(file.Name, 5) = Trim(myForm!txtFILENUM) Then
strFileName = Left(file.Name, Len(file.Name) - 4)
lngAlpha = Len(strFileName)
For lngI = lngAlpha To 1 Step -1
'(strFileName) 'This is where i'm stuck
Debug.Print lngAlpha
Debug.Print strFileName
Next lngI
End If
Next file
End If
End If
'Name strDestFile As strDestFileNew 'Please Ignore
'FileCopy strSourceFile, strDestFile 'Please Ignore
Set myForm = Nothing
Set fs = Nothing
Set strSourceFile = Nothing
Set strDestFolder = Nothing
End Sub
Re: opps! typo error in previous post, loop through and return highestfile
am 19.10.2007 04:04:28 von Salad
dobbine@gov.nl.ca wrote:
> Hi Salad,
> If I have a folder with the following files:
> 00012.jpg
> 00012a.jpg
> 00012b.jpg
> 00012c.jpg
>
> I would like to know how to return 00012c.jpg
>
> I think I need to use the Chr() function to return the ANSI value to
> do it but I don't know how
> Maybe first Parse out the letter by striping off '00012' and '.jpg'
> parts.
> Here is more of my code
>
>
>
> Sub AutoUpdate(strSourceFile)
> Dim file, v
> Dim fs, strDestFolder, strDestFile, strAlphaPart, strNumPart,
> strDestFileNew, strFileName
> Dim strAlphaInput As String
> Dim dtmSourceFileDate As Date
> Dim myForm As Form
> Dim lngI As Long, lngFileName As Long
> Dim varFile As Variant
> Dim intFileName As Integer
> Dim lngAlpha As Long
>
> Set myForm = Screen.ActiveForm
>
> 'If the user has updated or added a photo in N:\wwwroot\WORKS
> \BLDGPIC1 directory, Then...
> 'First check to see if the field PhotoDateModified in BLDGMSTR has
> the same photo date if
> 'not then synchronize. Use the DateLastModified property of the
> FileSystemObject as this
> 'property does not change when a file is copied from one directory
> to another.
>
> Set fs = CreateObject("Scripting.FileSystemObject")
> Set strSourceFile = fs.getfile(strSourceFile) '=N:\wwwroot\WORKS
> \BLDGPIC1\00012.jpg
>
> dtmSourceFileDate = strSourceFile.DateLastModified
> If dtmSourceFileDate <> myForm!txtPhotoDateLastModified Then 'A
> new picture has been added to N drive
> myForm!txtPhotoDateLastModified = dtmSourceFileDate
> 'Synchronize the field PhotoDateModified in table BLDGMSTR
>
> v = myForm!cboVOLUME
> If v = "9" Then
> v = "0"
> End If
>
> Set strDestFolder = fs.GetFolder("F:\User\BLDG
> \FacilitiesFilingCabinet\Volume" & Trim(v) & "\" & Trim(myForm!
> txtBLDGNUM) & "\" & Trim(myForm!txtSUBNAME))
> strDestFile = Dir(strDestFolder & "\" & Trim(myForm!
> txtPHOTO)) '00012.jpg
> 'if the destination directory is empty then simply copy the
> file
> If strDestFile = "" Then
> FileCopy strSourceFile, strDestFolder & "\" & Trim(myForm!
> txtPHOTO)
> Else 'otherwise begin AutoUpdate
>
> 'Begin filtering files in the directory for FILENUM pattern and
> parse out the file name.
> For Each file In strDestFolder.files
> If Left(file.Name, 5) = Trim(myForm!txtFILENUM) Then
> strFileName = Left(file.Name, Len(file.Name) - 4)
> lngAlpha = Len(strFileName)
> For lngI = lngAlpha To 1 Step -1
> '(strFileName) 'This is where i'm stuck
> Debug.Print lngAlpha
> Debug.Print strFileName
> Next lngI
> End If
> Next file
> End If
> End If
>
> 'Name strDestFile As strDestFileNew 'Please Ignore
> 'FileCopy strSourceFile, strDestFile 'Please Ignore
>
> Set myForm = Nothing
> Set fs = Nothing
> Set strSourceFile = Nothing
> Set strDestFolder = Nothing
> End Sub
>
Dim lngMax As String
DIm strAlpha As String 'extension
Dim strMax As String
Dim strF As String
Dim strMyForm As String
Dim strChar As String
Dim strNewFile As String
Dim intFor As Integer
Dim strHold As String
strF = file.Name
strMyForm = myForm!txtFILENUM
For Each file In strDestFolder.files
If Left(strF, 5) = Trim(strMyForm) Then
'remove prefix and ".jpg"
strAlpha = mid(strF,6,len(strF)-9)
If strF <> "" Then
If Len(strAlpha) > Len(strMax) then
'"aa" < "z" so check for len
strMax = strAlpha
Elseif strAlpha > strMax then
strMax = strAlpha
Endif
Else
strMax = chr(64) 'first char before "a"
Endif
Endif
'you now have the highest extension. now increment
If LCase(Right(strMax,1)) = <> "z" then
'if a, make b
strChar = Chr(asc(Right(strMax,1)) + 1)
strExt = left(strExt,len(strExt)-1) & strChar
Else
strChar = ""
For intFor = len(strMax) to 1 step -1
strChar = right(strMax,intfor,1)
If strChar <> "z" then
strHold = strHold & Chr(asc(Right(strMax,1)) + 1)
If right(strMax,intFor,1)
If right(strMax,intFor,1) <> "z" then
Next
Endif
endif
strNewFile = Left(strF, 5) & strExt & ".jpg"
msgbox "The old file was " & strF & " and the new is " & strNewFile