Luis Diaz
asked on
Excel VBA: add string at the beginning and at the end for files and folders
Hello experts,
I have the following procedure which allows me to add string on files:
EE question: https://www.experts-exchan ge.com/que stions/291 55167/Exce l-VBA-add- string-on- files.html
I would like to add the following requirements:
-Initial InputBox: "Enter 1 for adding string at the beginning 2 at the end"
-Apply the procedure for files and also for immediate subfolder. Recursive not needed.
If you have questions, please contact me.
Thank you for your help.
I have the following procedure which allows me to add string on files:
EE question: https://www.experts-exchan
Sub Add_String_On_Files()
' Local variables
Dim BaseFolder As String, NextFile As String, FileExt As String
Dim I As Integer
Dim StrToAdd As String
Dim MsgTxt As String
MsgTxt = "Before running this procedure, make sure to report following information in range A1:"
MsgTxt = MsgTxt & vbNewLine & " 1-Path directory in which you want are located files to rename"
Ans = MsgBox(MsgTxt & vbNewLine & vbNewLine & _
"Do you want to continue?", vbQuestion + vbYesNo, "Confirm Please!")
If Ans = vbNo Then Exit Sub
On Error GoTo Error_Routine
' Stop if no base folder provided
If ActiveSheet.Range("A1").Value = "" Then
MsgBox "A1 doesn't contain a directory string.", vbExclamation
Exit Sub
End If
' Stop if base folder provided does not exist
BaseFolder = ActiveSheet.Range("A1").Value
If Right(BaseFolder, 1) <> Application.PathSeparator Then BaseFolder = BaseFolder & Application.PathSeparator
If Len(Dir(BaseFolder, vbDirectory)) = 0 Then
MsgBox "Directory in A1 does not exist.", vbExclamation
Exit Sub
End If
' Get current date time
StrToAdd = InputBox("Enter the string that you want to add at the end of your files.")
If StrToAdd = "" Then
MsgBox "You didn't enter any string to add at the end of the files.", vbExclamation
Exit Sub
End If
' Process each file in the base folder
NextFile = Dir(BaseFolder & "\*.*")
Do While Len(NextFile) > 0
' Separate file name from extension
sFil = NextFile
FileExt = ""
I = InStrRev(NextFile, ".")
If I > 0 Then
FileExt = Right(sFil, Len(sFil) - I + 1)
sFil = Mid(sFil, 1, I - 1)
End If
' Rename file adding time stamp
Name BaseFolder & NextFile As BaseFolder & sFil & "_" & StrToAdd & FileExt
' Go to next file
NextFile = Dir
Loop
MsgBox "Files reported: " & BaseFolder & " have been renamed successfully.", vbInformation, "Files renamed!"
Shell "C:\WINDOWS\explorer.exe """ & BaseFolder & "", vbNormalFocus
Exit Sub
Error_Routine:
MsgBox Err.Description, vbExclamation, "Something went wrong!"
End Sub
I would like to add the following requirements:
-Initial InputBox: "Enter 1 for adding string at the beginning 2 at the end"
-Apply the procedure for files and also for immediate subfolder. Recursive not needed.
If you have questions, please contact me.
Thank you for your help.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I tested the code on your folder and the code adds the string in the beginning of each file in that folder and in each file in the immediate subfolder. There is only one immediate subfolder called "DestinationFolder" which has files in it and the code added string in each file of this subfolder. Rest two subfolders don't have any file in them.
What is not working?
What is not working?
ASKER
Hi Subodh,
Thank you for your feedback.
I expected to have string on files and also on subfolders not in files located on immediate subfolders.
Example: I have my folder reported in A1 which contains 2 files and 1 folder:
-titi.xls
-tata.csv
-folder-toto
If I report the following string at the beginning : 20190818_095557_
I expect to have the following:
-20190818_095557_titi.xls
-20190818_095557_tata.csv
-20190818_095557_folder-to to
Note: if folder-toto contains files they are not concerned by the procedure and they remain the same.
Let me know if it is clearer.
Thank you for your help.
Thank you for your feedback.
I expected to have string on files and also on subfolders not in files located on immediate subfolders.
Example: I have my folder reported in A1 which contains 2 files and 1 folder:
-titi.xls
-tata.csv
-folder-toto
If I report the following string at the beginning : 20190818_095557_
I expect to have the following:
-20190818_095557_titi.xls
-20190818_095557_tata.csv
-20190818_095557_folder-to
Note: if folder-toto contains files they are not concerned by the procedure and they remain the same.
Let me know if it is clearer.
Thank you for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes! I tested and it works! Sorry for those additional changes.
Anyway you were able to provide expected solution!
Thank you again!
Anyway you were able to provide expected solution!
Thank you again!
You're welcome! Glad it worked as desired in the end.
ASKER
Thank you for this proposal.
I tested and I have the string adding at the beginning for files and not for immediate subfolder.
Possible to add string for files and also for immediate subfolders?
I attached dummy test related to RootFolder in which are located files and immediate subfolders.
As you can see just file has been renamed.
Test.zip