I've a folder with thousands of .txt files I'm delimiting and creating xlsx files. I need to save the new xlsx files with date AND time (even if it's just the hour) into the file name.
Reason is, each day three files are saved at random intervals and I'll need to identify the files that were the last ones saved each day whilst I'm converting these ugly notepad files into excel workbooks.
Any help welcomed!
Sub LoopAllFiles()
Dim sPath As String, sDir As String
Dim sLoc As String
sPath = "C:\Users\MyUserName\OneDrive\DailySnapshots\"
sLoc = "C:\Users\MyUser\OneDrive\Repository\"
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
sDir = Dir$(sPath & "*.txt", vbNormal)
Do Until Len(sDir) = 0
Workbooks.Open (sPath & sDir)
With ActiveWorkbook
Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="|", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
.SaveAs Filename:=sLoc & Left(.Name, InStrRev(.Name, ".")) & "xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
.Close
End With
sDir = Dir$
Loop
End Sub
I'm unable to come up with a solution that pulls the .txt file properties to get date and time of initial creation. Tried adding in BuiltinDocumentProperties("Creation Date") But it's bugging out with "Run-time error '-2147467259 (80004005)': Automation error Unspecified error"
Sub LoopAllFiles()
Dim sPath As String, sDir As String
Dim sLoc As String
Dim creationDate As Date
sPath = "C:\Users\MyUserName\OneDrive\DailySnapshots\"
sLoc = "C:\Users\MyUser\OneDrive\Repository\"
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
sDir = Dir$(sPath & "*.txt", vbNormal)
Do Until Len(sDir) = 0
Workbooks.Open (sPath & sDir)
With ActiveWorkbook
Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="|", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
creationDate = ActiveWorkbook.BuiltinDocumentProperties("Creation Date")
.SaveAs Filename:=sLoc & Left(.Name, 2) & creationDate & "xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
.Close
End With
sDir = Dir$
Loop
End Sub
and then using creationDate in the file name but it's just throwing constant errors.