Forum Discussion
le_hieu
May 12, 2024Copper Contributor
auto add colum date modified in foder
Please help me. I have several thousand CSV files and need to add a modified date column:
Browse the folder-> get the list of files-> open each file to check if the date modified column exists. If not, add it.
Thank you very much!!!
- le_hieuCopper ContributorI'm looking for a way to do it. Please help me. Thank you very much
Please test carefully. It will be slow!
Sub AddColumn() ' Change the path to that of the folder with the csv files Const fld = "C:\MyFiles\" ' The exact name of the date modified column Const col = "Date Modified" Dim fil As String Dim wbk As Workbook Dim wsh As Worksheet Dim rng As Range Application.ScreenUpdating = False Application.DisplayAlerts = False ' Find the first file name fil = Dir(fld & "*.csv") ' Loop as long as we find a file Do While fil <> "" ' Open the csv file Set wbk = Workbooks.Open(fld & fil) ' Reference to its worksheet Set wsh = wbk.Worksheets(1) ' Try to find the column name Set rng = wsh.Rows(1).Find(What:=col, LookAt:=xlWhole, MatchCase:="False") If rng Is Nothing Then ' If not found, get the next available column Set rng = wsh.Cells(1, wsh.Columns.Count).End(xlToLeft).Offset(0, 1) ' Set the column header rng.Value = col ' Save and close the workbook wbk.Close SaveChanges:=True Else ' Otherwise, close the csv file without saving it wbk.Close SaveChanges:=False End If ' Get the next file name fil = Dir Loop Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub