PDA

View Full Version : MyCheck Fuction



barrymahboub
10-05-2012, 07:41 AM
I have some code that can change tif & pdf files to the following format YYMMDD - CR - N, plesae see code. The problem is if pressed twice it duplicates I need a way of stopping the duplication. Many thanks.

Sub ChangeFilename()
'"R:\DFS\Strategy and Compliance\FINEST\Projects\Imaging Trial\Documents\Creditors\test\"
Dim strfile As String, fileext As String, filepath As String, filenum As String 'My variables
filepath = BrowseForFolder("R:\DFS\Strategy and Compliance\FINEST\Projects\Imaging Trial\Documents\Creditors\") & "\"
strfile = Dir(filepath)

Do While strfile <> ""
Debug.Print strfile
Call MyCheck
If Right$(strfile, 3) = "tif" Or Right$(strfile, 3) = "pdf" Then
filenum = Left(strfile, Len(strfile) - 4)
fileext = Right(strfile, 3)
'End of each file name with - Cr -N
Name filepath & strfile As filepath & filenum & "-CR-" & Format(Now(), "YYMMDD") & "-N" & "." & fileext
End If
strfile = Dir
Loop
End Sub

p45cal
10-05-2012, 08:27 AM
Change:
Call MyCheck
to:
If Not MyCheck(strfile) Then
add an:
End If
next to the existing one
then add the MyCheck function to your code:Function MyCheck(TheFileName) As Boolean
If InStr(TheFileName, "-CR-") > 0 And InStr(TheFileName, "-N.") > 0 Then MyCheck = True
End Function
This will fail to change a file name if it happens already to have both "-CR-" and "-N." in it, but I'd like to hope this is very unlikely unless it has already been changed.

barrymahboub
10-08-2012, 12:52 AM
Thank ever so much for replying it does work and I appologise for not answering sooner, I had one of those weekends were the house hold has gone down with a sickness bug.

barrymahboub
10-08-2012, 01:40 AM
Thank ever so much for replying it does work and I appologise for not answering sooner, I had one of those weekends were the house hold has gone down with a sickness bug.

barrymahboub
10-08-2012, 05:49 AM
Change:
Call MyCheck
to:
If Not MyCheck(strfile) Then
add an:
End If
next to the existing one
then add the MyCheck function to your code:Function MyCheck(TheFileName) As Boolean
If InStr(TheFileName, "-CR-") > 0 And InStr(TheFileName, "-N.") > 0 Then MyCheck = True
End Function
This will fail to change a file name if it happens already to have both "-CR-" and "-N." in it, but I'd like to hope this is very unlikely unless it has already been changed.