Loop Through All Files In Subdirectories Of A Directory
Feb 26, 2010
I am having a bit of trouble with a bit of code. I have a bunch of code that I want to run on all excel files that live one level down from my main directory.
I have a general directory (orders) which contains a variable amount of subfolders each month (in picture below, 'Dan', 'Frank', 'Steve'), and I would like to look through all the excel files in 'Dan', 'Frank', 'Steve', but not in the main directory (orders), or any subdirectories within 'Dan', 'Frank', 'Steve'.
I am able to loop through all the excel files in the subdirectories individually if I hardcode the directory names, but as they are variable,
I am trying to scan through all files within a directory and all subdirectories to find files where the name begins with certain characters. Below is code that will accomplish this for a certain directory, but I do not know how to look through the subdirectories within the specified directory. So, the issues are:
1. How do I loop through each subdirectory within a specified directory?
2. I am trying to return the file name, path name (separate from the file name), and modified date. I can return the name. I can return the path, but it includes the name. Is there a way to return just the path?
3. What is the code to return the modified date of the file?
The script collects all the files in directory and sub-directories and list them in ascending format, I want them to get in transpose format. like for example: Root folder has many sub directories and in them a sub directory XYZ has 5 excel files, it will get the sub directory name in Col A and transpose all .xls files.
Col A | Col B________|Col C_________ |Col D___________|Col E________| XYZ__|C:/root/test.xls|C:/root/Sales.xls |C:/root/Report.xls |C:/root/sam.xls|
I wrote the following routine to loop files in folder. I have 4 .xls files in my test folder. This code is only grabbing 1 then exiting. I have used the Dir() loop before with no problems.
Code: Private Sub GetDataMultipleFiles(ByVal importExt As String, ByVal xFileNum As Integer, ByVal importLoc As String) Dim Sep As String, myFile As String Sep = Application.PathSeparator myFile = Dir(importLoc & Sep & "*" & importExt & "*") Do While Len(myFile) > 0
Lots of XLS files in a directory (C: emp). Each book has only a sheet1 with the data.
I have another workbook that i need to use to overwrite certain values in one column from each book.
The workbook I have has a column A and B, ID and value.
In each file inside the directory, A3 to last row used in A had each ID. If the id matches to an id in the workbook with the new values, I want to take the value from B in the override table and put it in C in the file. Look at each ID in each file against each ID in the override, replace and close.
I have the following Sub test() Set fs = Application.FileSearch With fs .LookIn = "\directory" .Filename = "3000333" If .Execute > 0 Then MsgBox "There were " & .FoundFiles.Count & _ " file(s) found." For i = 1 To .FoundFiles.Count MsgBox .FoundFiles(i) Next i Else MsgBox "There were no files found." End If End With End Sub
It looks into a specific directory, and tells me how many files beginning with 3000333 there are in that folder. I have over a thousand files in the folder, 13 of which begins with 3000333. However, according to this code, it only finds 10. After searching for various different strings, I've found out that it doesn't search for any file that has a modified date later than 6/2/2006. However, if i just use .Filename = "3000333" , that is, search for all the files in the directory, I get all the files, even those that were modified later than 6/2/2006 that I could not previously find.
I've a master sheet called records. It consolidates all the data from various worksheets. Overtime, the lists gets very long. So I'm proposing this. I want a macro which - Shift all the data yearly into a new directory at
G:MMTPMVarious StockArchive. When it saves at this directory it should bear the year as the folder name. The dates are retrieved from column H. - it should be operated at the every end of the year.
The title pretty much sums it up, actually. I have a macro, and a beautiful piece of work it is. It selects a particular selection of the active workbook, parses it in a particularly thrilling way, and writes the result to a text file. I'd like it to do this for ALL the files in a particular directory (somewhere over 500 files, if that matters).
If sDirectory = "" Then If iColumn = 1 Then sPath = "C:PensionDataPensionPath" ' Set the path, must end with backslash Else sPath = "C:PensionDataWorkBench" ' Set the path, must end with backslash End If Else If Right(sDirectory, 1) <> "" Then sPath = sDirectory & "" Else sPath = sDirectory End If End If
sFileName = Dir(sPath, lstAttr) ' Retrieve the first entry. lDataRow = 1 lRows = 1
This works on my PC and it works on other PCs. However, the one PC that it should and must work on, it does not. There is no file listing created.
I have duplicated the files and the folders on my PC and the files in the directories are listed. However, on that one PC, there is no file listing.
Hope i can get some help here as my vba experience is extremely limited. I'm trying to run a macro from a spreadsheet that will go down a list of file names that i have entered in a worksheet where the macro resides and open those spreadsheets and rename the worksheets in each file according to a list of names that i have entered in the 10 columns next to the file name. It's easier to explain with the layout of my macro spreadsheet: ....
I'm using a piece of code I found here on Ozgrid to create a list of files in a specific folder. And even though this worked fine for me a week ago, now when I run this same macro, the. zip files are left out of the list, even though I'm specifying msoFileTypeAllFiles. Archive files with other extensions, such as .tar.gz, do make the list. I get the feeling that my zip files are being considered folders instead of files. Do you have any idea why this is happening, or what to do to get that list of zip files?
Sub HyperlinkXLSFiles() Dim lCount As Long Application. ScreenUpdating = False Application.DisplayAlerts = False Application.EnableEvents = False On Error Resume Next With Application.FileSearch .NewSearch 'Change path to suit .LookIn = "o:DataFeeds" .FileType = msoFileTypeAllFiles.....................
My text files look like this(each has different number of rows): 2006-08-25 13:33:20 82,8 g 2006-08-25 13:34:10 58,5 g
between each column there is Tab
To import them from one directory (directories are named as month e.g. 8) I used:
Sub Consolidate() Dim mnthNum As Integer Dim myBook As Workbook Dim myRows As Long
With Application .DisplayAlerts = False .EnableEvents = False . ScreenUpdating = False End With
and it works almost perfectly.. the problem is that each time it loads next file it overwrites the previous..
e.g. if i have in my directory 4 files and sum of the rows is 20 it will show only 17. The import is good because I checked it with debugger but each time a new file is copied it starts from last row of previous file.. that's the problem I can't solve...
I am trying to specify all files and folders in a directory and the only help I found was the following thread: Create Index Of Files In Folder That works really well but, what I need is to specify all the folders and all the files under a directory, does any of you awsome hacks have an idea of how I can enhance the macro if the thread posted above?
Is it possible to create a VBA script that will list the filenames of files in a set directory? I have about 100 directories, each containing 1000 files and they need to go into an excel spreadsheet (each file on a new row)so they can be audited... ;(
I need a macro to loop through a dir and the sub directories to find xlsm files, when it finds one open, go to the sheet named data, look at c1 and if the value is between 12.1 and 13.4 then i need it to write the file path, filename to a1 in my excell sheet and then write the value from c1 in the original file to A2 of line in my excel sheet.
I have hobbled pieces of code together without any good results.
Macro which loops through a number of files and calls the same macro in each of them. Unfortunately when I add "Application.Run..." to the code, it no longer loops through the process and instead stops after updating the first file in the loop. If I remove the "Application.Run..." code and add any other code, the loop works fine and it continues through the process repeating all the steps for each file found.
Why it stops after one file when using "Application.Run..." to call the macros?
NB I have a list of path and file names starting in row 8 of columns A and C. Each file in the list has a macro called UpdateS1 and promoupdate1.
Sub C_Run_Loop_Macro() Dim lastRow As Long Dim i As Long
I wish to create a macro that looks in a specific directory "D:Records" . It finds the files which are modified after a date "January 01, 2012" in this directory (pdf files). Then it imports these files names into a worksheet.
is it possible if I have a directory like the following
with .csv files which are placed in arithmetic order 01,02,03 etc to determine if one .csv file is missing and print an error that this file is missing? I think that if I knew how many files I got I would create an array and check one by one the files.. But in case I have a dynamic directory and the number of files is not the same all the time , is this possible?
In my search I found several example similar to what I need, but nothing I could adapt to filt my needs, (at least not wim my limited skills).
Here what I need to do:
I have multilple Excel files in a directory. (M:/Archived PO Responses/Domestic). On a daily basis, these files are processed via VBA, and deleted after processing. What I need to do is, prior to processing and deleting these files, create an ongoing log of the filenames in that directory.
The macro would open an Excel file named "Processed Orders.xls", which is stored on the network drive "M:". It would then append all of the filenames in the directory mentioned above to a sheet named "Processed Orders" in that workbook, below any filenames that already exist.
I would like to list all files in a directory (display the full path) and make hyperlinks to all listed files at the same time.
I came accross the code below which works well for files in a directory:
Sub HyperlinksToDirectory() ' puts hyperlinks to each of the files in a directory of your choice ' into the active sheet starting at the active cell Dim stDir As String Dim stFile As String Dim R As Range Set R = ActiveCell stDir = InputBox("Directory?", , Default:=CurDir()) stFile = Dir(stDir & "*.*") Do Until stFile = "" R.Hyperlinks.Add R, stDir & "" & stFile, , , stFile Set R = R.Offset(1) stFile = Dir() Loop R.CurrentRegion.Sort key1:=R, order1:=xlAscending, header:=xlNo End Sub
I currently have a macro working where I can populate the A Column with files from a directory and based on its file extension, however I am having problems trying to populate the second (B) column with that files date modified. I have searched the forum to no avail and I must admit I not the best with vba.
Sub INP_files() ' searches within the file location ' C:Arenium_Projects31-1853012_Barnawartha BOSCADDMX Dim rngOut As Range Dim strPath As String Dim strfile As String ' Sheets("INI_FILES").Select
I need some VBA code to cycle through each and every folder and file in a given root directory and write the file name string to a cell. (does not need to look at the content of sub-folders only the names of all objects sitting directly under the given root.)
Code that loops through all folders in a directory, to check each workbook in each of those folders. If any workbook name in any of these folders matches a predefined string, then open this workbook for some other codes to run.
I have been trying to process Excel files in a directory with the following
Sub FindExcelFiles() Dim foldername As String Dim FSO As Object Dim fldr As Object Dim file As Object Dim cnt As Long foldername = "c:usersseagreendesktopTuesdayFeb102009week ending feb 7 2009 esting2" Set FSO = CreateObject("Scripting.FilesystemObject") Set fldr = FSO.GetFolder(foldername) For Each file In fldr.Files If file.Type Like "*Microsoft Office Excel*" Then cnt = cnt + 1 End If Application.StatusBar = "Now working on " & ActiveWorkbook.FullName DoSomething ActiveWorkbook Next file Set file = Nothing Set fldr = Nothing Set FSO = Nothing Range("A1").Value = cnt End Sub Here's the stub for the subroutine that's being called:
Sub DoSomething(inBook As Workbook) 'Massage each workbook 'Debug.Print "Hello" Debug.Print ActiveWorkbook.FullName End Sub I am using Excel 2007. I found out I cannot use Application.Filesearch as Microsoft has dropped this method for 2007. My problem now is that I just see "Now working on c:usersseagreendesktopTuesdayFeb102009week ending feb 7 2009 esting2file1.xls written six times in the immediate window.