VBA loop through files in a folder not working properly - excel

I am having 2 issues with the below code.
on the first loop it finds the same file, hence why I have it skip if the file is the same name. After that it will proceed as it should. On the 3rd loop instead of finding the 3rd file (fileName2 = Dir) becomes fileName2 = "".
When I want fileName to go to the second file (fileName = Dir) I get a run time 5 error.
*Note: I currently have 6 files in the folder that I am testing but I will want to use for folders that have 10,000 small files
Sub TestMD5()
Dim myfilepath As String
Dim myfilepath2 As String
Dim fileName As Variant
Dim fileName2 As Variant
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem & "\"
Set fldr = Nothing
fileName = Dir(GetFolder)
fileName2 = Dir(GetFolder)
Do While fileName <> ""
Do While fileName2 <> ""
myfilepath = GetFolder & fileName
myfilepath2 = GetFolder & fileName2
If myfilepath <> myfilepath2 Then
If FileToMD5Hex(myfilepath) = FileToMD5Hex2(myfilepath2) And FileToSHA1Hex(myfilepath) =
FileToSHA1Hex2(myfilepath2) Then
'Kill (myfilepath2)
Debug.Print "match - " & (fileName) & " & " & (fileName2)
Else
Debug.Print "no match - " & (fileName) & " & " & (fileName2)
End If
End If
fileName2 = Dir
Loop
'Set the fileName to the next file
fileName = Dir
Loop
End Sub

I mashed your code together with the "File system Object" approach, where we can do a For each loop on the files.
This at least gets you away from the whole run time 5 error. Maybe it could be of use.
Sub TestMD5()
Dim myfilepath As Variant, myfilepath2 As Variant
Dim sItem As String
Dim fso As Object
Dim fldr As Variant
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder(sItem & "\")
For Each myfilepath In fldr.Files
For Each myfilepath2 In fldr.Files
If Not myfilepath = myfilepath2 Then
If FileToMD5Hex(myfilepath) = FileToMD5Hex2(myfilepath2) And FileToSHA1Hex(myfilepath) = FileToSHA1Hex2(myfilepath2) Then
'Kill (myfilepath2)
Debug.Print "match - " & (myfilepath) & " & " & (myfilepath2)
Else
Debug.Print "no match - " & (myfilepath) & " & " & (myfilepath2)
End If
End If
Next myfilepath2
Next myfilepath
End Sub

I think FileDialog(msoFileDialogFilePicker) should be used instead of FileDialog(msoFileDialogFolderPicker)

Related

Rename files and copy the renamed files to subbolder in main folder

I am attempting to rename files found in a main folder, but then put the renamed files in the same directory as the files to be copied. This is my original folder structure:
Main Folder
|
|____file1.txt
|____file2.txt
|____file1.txt
I want to now create a folder under the Main Folder called "Renamed" and place the renamed files in there. The new folder structure should look like this after successfully executing the code:
Main Folder
|
|____Renamed
| |
| |____renamed-file1.txt
| |____renamed-file2.txt
| |____renamed-file3.txt
|
|____file1.txt
|____file2.txt
|____file1.txt
However, in the code that I have so far, I cannot create the "Renamed" folder under the Main Folder as I get the error message Run-time error '5': Invalid procedure call or argument that seem to occur at the line fso.CopyFolder sItem, strPath2. Can you help me create the folder structure with the renamed folder and files.
Here is my code:
Sub RenameFile()
Dim fldr As FileDialog
Dim sItem As String
Dim strPath As String
Dim strPath1 As String
Dim strPath2 As String
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
Dim z As String
Dim s As String
Dim V As Integer
Dim TotalRow As Integer
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
TotalRow = ActiveSheet.UsedRange.Rows.Count
NextCode:
strPath = sItem
strPath2 = fso.BuildPath(sItem, "Renamed")
' Create the folder "Renamed"
fso.CopyFolder sItem, strPath2
For V = 1 To TotalRow
' Get value of each row in columns 1 start at row 2
z = Cells(V + 1, 1).Value
' Get value of each row in columns 2 start at row 2
s = Cells(V + 1, 2).Value
Dim sOldPathName As String
sOldPathName = fso.BuildPath(strPath2, z)
sNewPathName = fso.BuildPath(strPath2, s)
Name sOldPathName As sNewPathName
On Error Resume Next
Name sOldPathName As s
Next V
MsgBox "Congratulations! You have successfully renamed all the files"
End Sub
Copy and Rename Files Using Dir and FileCopy
Using FileCopy is much faster, simpler, and more straightforward: it copies and renames in one go.
This is a simplified example to get you familiar with Dir and FileCopy. In your case, you would 'Dir' each name in column A and if the length of the filename is greater than 0 (confirming that the file exists), you would 'FileCopy the source path to the destination path (using the names in column B)'.
Sub RenameFiles()
' Source
Const sFilePattern As String = "*.*"
Dim sInitPath As String: sInitPath = Application.DefaultFilePath & "\"
' Destination
Const dSubFolderName As String = "Renamed"
Const dPrefix As String = "renamed-"
Dim sFolderPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a Folder"
.InitialFileName = sInitPath
If .Show <> -1 Then
MsgBox "You canceled.", vbExclamation
Exit Sub
End If
sFolderPath = .SelectedItems(1) & "\"
End With
Dim dFolderPath As String: dFolderPath = sFolderPath & dSubFolderName & "\"
If Len(Dir(dFolderPath, vbDirectory)) = 0 Then MkDir dFolderPath
Dim sFileName As String: sFileName = Dir(sFolderPath & sFilePattern)
If Len(sFileName) = 0 Then
MsgBox "No files found.", vbExclamation
Exit Sub
End If
On Error GoTo FileCopyError
Do Until Len(sFileName) = 0
FileCopy sFolderPath & sFileName, dFolderPath & dPrefix & sFileName
sFileName = Dir
Loop
On Error GoTo 0
MsgBox "Congratulations! You have successfully renamed all the files.", _
vbInformation
Exit Sub
FileCopyError:
Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description _
& vbLf & "Could not copy '" & sFileName & "'."
Resume Next
End Sub

Search a folder for keyword - FINDSTR Still does not work [duplicate]

I'm trying to use Excel VBA to find a string in a folder, but it seems the FINDSTR command line is not working.
I'm wondering if it could be a change in Windows (I'm using Win10), or if I don't have a have the correct "Reference" selected (I do have the Microsoft Scripting Runtime selected).
Sub ListFilesContainingString()
Dim myfile As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
wrd = InputBox("Word:", "Insert search word")
If wrd = "" Then
MsgBox "???"
Exit Sub
End If
myfile = FindFiles(GetFolder, wrd)
If (myfile <> "") Then MsgBox file
End Sub
Function FindFiles(ByVal path As String, ByVal target As String) As String
' Run The Shell Command And Get Output
Dim files As String
files = CreateObject("Wscript.Shell").Exec("FINDSTR /M """ & target & """ """ & path & "\*.*""").StdOut.ReadAll
FindFiles = ""
If (files <> "") Then
Dim idx As Integer
idx = InStr(files, vbCrLf)
FindFiles = Left(files, idx - 1)
End If
I fixed the typo but FINDSTR is still not working correctly. My current code is below. Note that there are several instances of FINDSTR and FIND so I can see what is being returned (the "If, Then x=x" code is only to place a breakpoint). FINDSTR returns "", FIND returns a file but it isn't a correct file.
FINDSTR does work using a dos/powershell window.
Sub ListFilesContainingString()
'this macro finds vendor information on the chosen file for each part in the origin file
Dim myfile As String
Dim fldr As FileDialog
Dim sItem As String
Dim wrd As String
''''''''''''''''''''''''''''''''''''''
Dim objFSO As Object
Dim objFolders As Object
Dim objFolder As Object
Dim DirFolderRename As String
Dim arrFolders() As String
Dim FolderCount As Long
Dim FolderIndex As Long
''''''''''''''''''''''''''''''''''''''
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
''''''''''''''''''''''''''''''''''''''
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolders = objFSO.GetFolder(sItem).SubFolders
FolderCount = objFolders.Count
If FolderCount > 0 Then
ReDim arrFolders(1 To FolderCount)
FolderIndex = 0
For Each objFolder In objFolders
FolderIndex = FolderIndex + 1
arrFolders(FolderIndex) = objFolder.Name
Next objFolder
Else
MsgBox "No folders found!", vbExclamation
End If
Set objFSO = Nothing
Set objFolders = Nothing
Set objFolder = Nothing
''''''''''''''''''''''''''''''''''''''
Set wrdAddr = Application.InputBox("Select First Word to Search For", "Obtain Range Object", Type:=8)
wrdCol = wrdAddr.Column
wrdRow = wrdAddr.Row
StartCell = Cells(wrdRow, wrdCol).Address
Range(StartCell).Activate
wrd = ActiveCell.Value
While (wrd <> "")
'wrd = InputBox("Word:", "Insert search word")
If wrd = "" Then
MsgBox "???"
Exit Sub
End If
For i = 1 To FolderCount
TheFolder = GetFolder & "\" & arrFolders(i)
myfile = FindFiles(TheFolder, wrd)
If (myfile <> "") Then
ActiveCell.Offset(0, 17).Value = ActiveCell.Offset(0, 17).Value & arrFolders(i) & ","
End If
Next i
ActiveCell.Offset(1, 0).Select
wrd = ActiveCell.Value
Wend
End Sub
Function FindFiles(ByVal path As String, ByVal target As String) As String
' Run The Shell Command And Get Output
Dim files1, files2, files3, files4 As String
Dim lines
'''' This works in the dos window
' findstr /M /S /I L298P C:\Users\Wm" "Boyd\Documents\Boyd" "Manufacturing\Customers\Inactive\*.xls?
''''
exec ("FINDSTR /M L298P C:\Users\Wm""Boyd\Documents\Boyd""Manufacturing\Customers\Inactive\*.xls?")
files1 = CreateObject("WScript.Shell").exec("FINDSTR /M """ & target & """ """ & path & "\*.xls?""").StdOut.Read
files2 = CreateObject("Wscript.Shell").exec("FINDSTR /M L298P C:\Users\Wm""Boyd\Documents\Boyd""Manufacturing\Customers\Inactive\*.xlsx").StdOut.ReadAll
If files1 <> "" Then
x = x
End If
files3 = CreateObject("Wscript.Shell").exec("FIND """ & target & """ """ & path & "\*.xls?""").StdOut.ReadAll
files4 = CreateObject("Wscript.Shell").exec("FIND """ & target & """ """ & path & "\*.*""").StdOut.ReadAll
lines = Split(files1, vbCrLf)
Dim curFile As String
Dim line
For Each line In lines
If (Left(line, 11) = "---------- ") Then
curFile = Mid(line, 12)
End If
If (curFile <> "") Then
FindFiles = curFile
Exit Function
End If
Next
'files = CreateObject("Wscript.Shell").Exec("FINDSTR """ & target & """ """ & path & "\*.*""").StdOut.ReadAll
'If (files <> "") Then
'Dim idx As Integer
'idx = InStr(files, vbCrLf)
'FindFiles = Left(files, idx - 1)
'End If
FindFiles = ""
End Function
I have created a log file to record the command executed and the response. This is a first step, if it works the next step would be to parse the response for the information you want.
Option Explicit
Sub ListFilesContainingString()
Const qq = """"
' get first word from sheet
Dim wrdCell As Range
Set wrdCell = Application.InputBox("Select First Word to Search For", _
"Obtain Range Object", Type:=8)
If Len(wrdCell.Value2) = 0 Then
MsgBox "No word selected", vbCritical
Exit Sub
End If
' start logging
Dim Folder As String, FSO As Object, tsLog As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim logfile As String, cmd As String, n As Long
Dim msg As String, W As Object, s As String
logfile = ThisWorkbook.path & "\" & "Log_" _
& Format(Now(), "yyyymmdd_hhmmss") & ".txt"
Set tsLog = FSO.CreateTextFile(logfile)
Set W = CreateObject("Wscript.Shell")
' get folders
Folder = GetFolder("C:\") 'start in c:\
If Len(Folder) = 0 Then Exit Sub
' scan for each words
Do While Not IsEmpty(wrdCell)
' message box
msg = msg & vbLf & wrdCell.Address & " " & wrdCell
' build command
s = qq & wrdCell & qq & " " & qq & Folder
cmd = "FINDSTR /M /S " & s & "\*.*" & qq
tsLog.writeLine "Command" & vbCrLf & cmd & vbCrLf
' execute
s = W.exec(cmd).StdOut.ReadAll
tsLog.writeLine "Result" & vbCrLf & ">" & s & "<" & vbLf
' next
n = n + 1
Set wrdCell = wrdCell.Offset(1)
Loop
tsLog.Close
MsgBox "Words searched for " & msg, vbInformation, "See " & logfile
Shell "notepad.exe " & logfile
End Sub
Function GetFolder(strPath) As String
Dim fldr As FileDialog
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then
MsgBox "Cancelled", vbExclamation
Exit Function
End If
GetFolder = .SelectedItems(1)
End With
End Function

File Name Has "[]" issue

I'm trying to activate the browse file. However, when the file name has a "[]" it has an error "Subscript out of range". I would like to recognize the "[]" to activate the file. I will copy the whole data on the file, that's why I will activate it. But, when I'm trying to manually remove the "[]" in the file name, it works successfully. Thanks for the help !
Here is my code:
Sub Test()
Dim fldr As FileDialog
Dim sItem As String
Dim shtName As String
'select folder
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
'Get the path
GetFolder = sItem
Set fldr = Nothing
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Check if exist the word in file name
wrd = "AS"
'get the file associated with wrd
strfile = Dir(GetFolder & "\*" & wrd & "*.xl*")
fc = 0
Do While Len(strfile) > 0
On Error GoTo endhere
fc = fc + 1
'open the file
Workbooks.Open (GetFolder & "\" & strfile)
Workbooks(strfile).Activate
here:
Workbooks(strfile).Close False
strfile = Dir
Loop
endhere:
Debug.Print Err.Description
End Sub
Try
Dim strFile1 as String
If InStr(strfile, "[") > 0 Then
strFile1 = Replace(Replace(strfile, "[", ""), "]", "")
Name GetFolder & "\" & strfile As GetFolder & "\" & strFile1
Workbooks.Open (GetFolder & "\" & strFile1)
Else
Workbooks.Open (GetFolder & "\" & strfile)
End If
If the characters to be removed do not belong to the file name, belonging to the path, please use the next way:
Dim strFile1 As String
If InStr(GetFolder & "\" & strfile, "[") > 0 Then
strFile1 = Replace(Replace(GetFolder & "\" & strfile, "[", ""), "]", "")
Name GetFolder & "\" & strfile As strFile1
Workbooks.Open (strFile1)
Else
Workbooks.Open (GetFolder & "\" & strfile)
End If
Anyhow, this second version can be use for both cases...
Not sure if this is Excel-Version dependent, for me it gives no error. I can open the file and access it with the name that the Dir returned, even if the Name-property of the workbook replaces the the square brackets with round one (interesting, didn't know that).
However, much more important is that you neither need to Activate the workbook nor use the name to open and close it. Workbooks.Open is a Function that returns a reference to the opened Workbook, so you should use that:
Dim wb As Workbook
Set wb = Workbooks.Open (GetFolder & "\" & strfile)
' (...) do your stuff using wb, eg copy a Worksheet:
wb.Sheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
wb.Close False

Converting multiple xlsl files to xls (97-2003 Worksheet) extension without changing the names

I am trying to loop through all the 'xlsx' files in a folder and convert them to 'xls' ( Excel 97-2003 Worksheet) format. I use the following codes but then the output files are still saved as 'xlsx' instead of 'xls'. I am a beginner and looking to learn more from others. Thanks for your help!
Sub Convert()
Dim strPath As String
Dim strFile As String
Dim strfilenew As String
Dim xWbk As Workbook
Dim xSFD, xRFD As FileDialog
Dim xSPath As String
Dim xRPath As String
Set xSFD = Application.FileDialog(msoFileDialogFolderPicker)
With xSFD
.Title = "Please select the folder contains the xls files:"
.InitialFileName = "C:\"
End With
If xSFD.Show <> -1 Then Exit Sub
xSPath = xSFD.SelectedItems.Item(1)
Set xRFD = Application.FileDialog(msoFileDialogFolderPicker)
With xRFD
.Title = "Please select a folder for outputting the new files:"
.InitialFileName = "C:\"
End With
If xRFD.Show <> -1 Then Exit Sub
xRPath = xRFD.SelectedItems.Item(1) & "\"
strPath = xSPath & "\"
strFile = Dir(strPath & "*.xlsx")
strfilenew = Dir(strPath & "*.xls")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While strFile <> ""
If Right(strFile, 4) = "xlsx" Then
Set xWbk = Workbooks.Open(Filename:=strPath & strfilenew)
xWbk.SaveAs Filename:=xRPath & strfilenew, _
FileFormat:=xlExcel18
xWbk.Close SaveChanges:=True
End If
strFile = Dir
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
There was a bit of a mix-up in your file naming, basically as evidenced by the several double-declarations that I removed. The really big mistake was here, Set xWbk = Workbooks.Open(Filename:=strPath & strfilenew) where you tried to open the old workbook by the new name. I think the confusion started here "Please select the folder contains the xls files:". Of course, this is the folder with the XLSX files. The recommended antidote is to use "meaningful" variable names but you chose to speak in riddles (like xSFD) which makes coding more difficult.
However, the code below is largely yours, and it does work.
Sub Convert()
' 230
Dim Spath As String ' path to read from (XLSX files)
Dim Rpath As String ' path to write to (XLS files)
Dim strFile As String ' loop variable: current file name
Dim Wbk As Workbook ' loop object: current workbook(strFile)
Dim Sp() As String ' split array of strFile
Dim strFileNew As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select the folder contains the XLSX files:"
.InitialFileName = "C:\"
If .Show <> -1 Then Exit Sub
Spath = .SelectedItems.Item(1) & "\"
End With
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder for outputting the new files:"
.InitialFileName = "C:\"
If .Show <> -1 Then Exit Sub
Rpath = .SelectedItems.Item(1) & "\"
End With
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
strFile = Dir(Spath & "*.xlsx")
Do While strFile <> ""
If Right(strFile, 4) = "xlsx" Then
Sp = Split(strFile, ".")
Sp(UBound(Sp)) = "xls"
strFileNew = Join(Sp, ".")
Set Wbk = Workbooks.Open(Filename:=Spath & strFile)
Wbk.SaveAs Filename:=Rpath & strFileNew, FileFormat:=xlExcel8
Wbk.Close SaveChanges:=True
End If
strFile = Dir
Loop
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Observe that the new file name is created by splitting the old name on periods, changing the last element, and reassembling the modified array.

How to list files names in folder

I'm trying to create a list of file names in folder for reference, The following code is listing all the file names with extension Filename.pdf
how do I exclude the extension from file name? .pdf
Option Explicit
Sub GetFileName()
Dim xlRow As Long
Dim sDir As String
Dim FileName As String
Dim sFolder As String
sFolder = "C:\Temp\"
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder"
.InitialFileName = sFolder
.Show
If .SelectedItems.Count <> 0 Then
sDir = .SelectedItems(1) & "\"
FileName = Dir(sDir, 7)
Do While FileName <> ""
Range("A1").Offset(xlRow) = FileName
xlRow = xlRow + 1
FileName = Dir
Loop
End If
End With
End Sub
I'm not 100% sure what you are asking, but I think that
If FileName Like "*.pdf" Then
Range("A1").Offset(xlRow) = Mid(FileName,1,Len(FileName)-4)
End If
might be what you are after.
If the filename itself does not contain a period, you can use Split():
Option Explicit
Sub GetFileName()
Dim xlRow As Long
Dim sDir As String
Dim FileName As String
Dim sFolder As String
sFolder = "C:\Temp\"
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder"
.InitialFileName = sFolder
.Show
If .SelectedItems.Count <> 0 Then
sDir = .SelectedItems(1) & "\"
FileName = Dir(sDir, 7)
Do While FileName <> ""
Range("A1").Offset(xlRow) = Split(FileName, ".")(0)
xlRow = xlRow + 1
FileName = Dir
Loop
End If
End With
End Sub

Resources