Hello,
in module 2 I would adapt code to deal with pdf files too.
currently I can add text to fle name after add amount with just EXCEL files . also I would do that with PDF files.
the code ask me enter amount based on files names are existed in column B after that ask me enter text to add to last part for file name then will change file name in column B and directory in column C and add new file name in column A and change within folders and subfolders. this happens for just EXCEL files but I would also do that with PDF files
Sub test()
Dim myDir$, s$, suf$, x, n&
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then myDir = .SelectedItems(1)
End With
If myDir = "" Then Exit Sub
Do
s = InputBox("Enter amount")
If s = "" Then Exit Do
suf = InputBox("Enter word")
If suf = "" Then Exit Do
x = SearchFiles(myDir & "\", "*" & s & ".xls*")
If x <> "" Then
n = n + IIf(n, 1, 2)
Cells(n, 2) = CreateObject("Scripting.FileSystemObject").GetBaseName(x)
Cells(n, 3) = x
If Not IsFileOpen(x) Then
Name x As Application.Replace(x, InStrRev(x, "."), 0, " " & suf)
Cells(n, 1) = Cells(n, 2) & " " & suf
Else
Cells(n, 4) = "Currently in USE " & Format(Now, "yyyy/mm/dd hh:mm:ss")
End If
Else
MsgBox "Not found"
End If
If MsgBox("Do you want to continue?", vbQuestion + vbYesNo) <> vbYes Then Exit Do
Loop
End Sub
Function SearchFiles$(myDir$, myFileName$)
Dim fso As Object, myFolder As Object, myFile As Object
Set fso = CreateObject("Scripting.FileSystemObject")
For Each myFile In fso.GetFolder(myDir).Files
If (Not myFile.Name Like "~$*") * (myFile.Name <> ThisWorkbook.Name) _
* (myFile.Name Like myFileName) Then
SearchFiles$ = myDir & myFile.Name
Exit Function
End If
Next
For Each myFolder In fso.GetFolder(myDir).SubFolders
SearchFiles = SearchFiles(myFolder.Path & "\", myFileName)
Next
End Function
Function IsFileOpen(ByVal fName$) As Boolean
Dim ff&, errNum&
On Error Resume Next
ff = FreeFile
Open fName For Input Lock Read As #ff
Close ff
errNum = Err
On Error GoTo 0
IsFileOpen = (errNum <> 0)
End Function
thanks