Get filenames & timestamps batch file [Closed]

- - Latest reply:  doesntmatter - Dec 11, 2011 at 05:35 AM
Hello,
Can anyone help me create a batch file that will display all the files, subfolders and files under subfolders of folder with both date created and date modified then output the result to .txt or .xls file.

Here's what I've tried but I cannot display both date created and date modified.

dir /t:c /t:w /s /q "C:\Documents and Settings\administrator\Desktop\fileinfo_test" >> "C:\Documents and Settings\administrator\Desktop\dir.txt"

Please help me with this.
Thank you in advance..

mhel
See more 

8 replies

Best answer
Posts
1022
Registration date
Saturday May 23, 2009
Status
Contributor
Last seen
October 20, 2010
214
20
Thank you
this will do what you want in an excel sheet. add it as a macro to your sheet, when run it will create a sheet with all the information that you require.

---------------------------------------------------------------------------------------------------
Public X()
Public I As Long
Public objShell, objFolder, objFolderItem
Public FSO, oFolder, Fil

Sub MainExtractData()

Dim NewSht As Worksheet
Dim MainFolderName As String
Dim TimeLimit As Long, StartTime As Double

ReDim X(1 To 65536, 1 To 11)

Set objShell = CreateObject("Shell.Application")
TimeLimit = Application.InputBox("Please enter the maximum time that you wish this code to run for in minutes" & vbNewLine & vbNewLine & _
"Leave this at zero for unlimited runtime", "Time Check box", 0)
StartTime = Timer

' This is the part I need to change to create a Table with these field names.

Application.ScreenUpdating = False
MainFolderName = BrowseForFolder()
Set NewSht = ThisWorkbook.Sheets.Add

X(1, 1) = "Path"
X(1, 2) = "File Name"
X(1, 3) = "Last Accessed"
X(1, 4) = "Last Modified"
X(1, 5) = "Created"
X(1, 6) = "Type"
X(1, 7) = "Size"
X(1, 8) = "Owner"
X(1, 9) = "Author"
X(1, 10) = "Title"
X(1, 11) = "Comments"

' I is defined Publicly as a Long Integer
I = 1

Set FSO = CreateObject("scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(MainFolderName)
'error handling to stop the obscure error that occurs at time when retrieving DateLastAccessed
On Error Resume Next
For Each Fil In oFolder.Files
Set objFolder = objShell.Namespace(oFolder.Path)
Set objFolderItem = objFolder.ParseName(Fil.Name)
I = I + 1
If I Mod 20 = 0 And TimeLimit <> 0 And Timer > (TimeLimit * 60 + StartTime) Then
GoTo FastExit
End If
If I Mod 50 = 0 Then
Application.StatusBar = "Processing File " & i
DoEvents
End If
X(i, 1) = oFolder.Path
X(i, 2) = Fil.Name
X(i, 3) = Fil.DateLastAccessed
X(i, 4) = Fil.DateLastModified
X(i, 5) = Fil.DateCreated
X(i, 6) = Fil.Type
X(i, 7) = Fil.Size
X(i, 8) = objFolder.GetDetailsOf(objFolderItem, 8)
X(i, 9) = objFolder.GetDetailsOf(objFolderItem, 9)
X(i, 10) = objFolder.GetDetailsOf(objFolderItem, 10)
X(i, 11) = objFolder.GetDetailsOf(objFolderItem, 14)
Next

'Get subdirectories
If TimeLimit = 0 Then
Call RecursiveFolder(oFolder, 0)
Else
If Timer < (TimeLimit * 60 + StartTime) Then Call RecursiveFolder(oFolder, TimeLimit * 60 + StartTime)
End If

FastExit:
Range("A:K") = X
If I < 65535 Then Range(Cells(i + 1, "A"), Cells(65536, "A")).EntireRow.Delete
Range("A:K").WrapText = False
Range("A:K").EntireColumn.AutoFit
Range("1:1").Font.Bold = True
Rows("2:2").Select
ActiveWindow.FreezePanes = True
Range("a1").Activate

Set FSO = Nothing
Set objShell = Nothing
Set oFolder = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing
Set Fil = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub

Sub RecursiveFolder(xFolder, TimeTest As Long)
Dim SubFld
For Each SubFld In xFolder.SubFolders
Set oFolder = FSO.GetFolder(SubFld)
Set objFolder = objShell.Namespace(SubFld.Path)
For Each Fil In SubFld.Files
Set objFolder = objShell.Namespace(oFolder.Path)
'Problem with objFolder at times
If Not objFolder Is Nothing Then
Set objFolderItem = objFolder.ParseName(Fil.Name)
I = I + 1
If I Mod 20 = 0 And TimeTest <> 0 And Timer > TimeTest Then
Exit Sub
End If
If I Mod 50 = 0 Then
Application.StatusBar = "Processing File " & i
DoEvents
End If
X(i, 1) = SubFld.Path
X(i, 2) = Fil.Name
X(i, 3) = Fil.DateLastAccessed
X(i, 4) = Fil.DateLastModified
X(i, 5) = Fil.DateCreated
X(i, 6) = Fil.Type
X(i, 7) = Fil.Size
X(i, 8) = objFolder.GetDetailsOf(objFolderItem, 8)
X(i, 9) = objFolder.GetDetailsOf(objFolderItem, 9)
X(i, 10) = objFolder.GetDetailsOf(objFolderItem, 10)
X(i, 11) = objFolder.GetDetailsOf(objFolderItem, 14)
Else
Debug.Print Fil.Path & " " & Fil.Name
End If
Next
Call RecursiveFolder(SubFld, TimeTest)
Next
End Sub

Function BrowseForFolder(Optional OpenAt As Variant) As Variant
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that directory
'NOTE: If invalid, it will open at the Desktop level

Dim ShellApp As Object

'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0

'Destroy the Shell Application
Set ShellApp = Nothing

'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select

Exit Function

Invalid:
'If it was determined that the selection was invalid, set to False
BrowseForFolder = False

End Function

-------------------------------------------------------------------------------------------------

i know you wanted a dos script, but you're limited in what it can do.

hope this helps

Say "Thank you" 20

A few words of thanks would be greatly appreciated. Add comment

CCM 5638 users have said thank you to us this month

-
THANK YOU... By Far you are a lifesaver!, this script is easy as 1, 2, 3... Create the Macro.. Cut & Paste, execute and set 1 variable to 0 and browse to the directory... BULLETPROOF
Posts
1022
Registration date
Saturday May 23, 2009
Status
Contributor
Last seen
October 20, 2010
214
9
Thank you
ok, you were nearly there
================================================================

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] /C [/D] [/L] [/N]
[/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] /4


[drive:][path][filename]
Specifies drive, directory, and/or files to list.

/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files - Prefix meaning not
/B Uses bare format (no heading information or summary).
/C Display the thousand separator in file sizes. This is the
default. Use /-C to disable display of separator.
/D Same as wide but files are list sorted by column.
/L Uses lowercase.
/N New long list format where filenames are on the far right.
/O List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
G Group directories first - Prefix to reverse order
/P Pauses after each screenful of information.
/Q Display the owner of the file.
/S Displays files in specified directory and all subdirectories.
/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last Written
/W Uses wide list format.
/X This displays the short names generated for non-8dot3 file
names. The format is that of /N with the short name inserted
before the long name. If no short name is present, blanks are
displayed in its place.
/4 Displays four-digit years

Switches may be preset in the DIRCMD environment variable. Override
preset switches by prefixing any switch with - (hyphen)--for example, /-W.

==================================================================

dir "D:\Documents and Settings\dave\Desktop\"/T:C /T:W /S /Q >> "d:\Documents and

Settings\dave\Desktop\dir.txt"

me thinks will produce the desired result. (you need to put the switches after the source details.

hope that helps. give me a nod if it does.
Posts
1022
Registration date
Saturday May 23, 2009
Status
Contributor
Last seen
October 20, 2010
214
2
Thank you
I think you're limited in what dos can do in respect of what you are trying to do.

you can have date/time created or date/time accessed, but not both.
Posts
25
Registration date
Saturday November 7, 2009
Status
Member
Last seen
July 18, 2012
14
2
Thank you

I like sharpman's solutions. Here is an alternate solution for those who don't know programming well.


mhel_0414 asked
Can anyone help me create a batch file that will display all the files, subfolders and files under subfolders of folder with both date created and date modified then output the result to .txt or .xls file.


The following command in biterscripting will do just that.

lf -r -n -c -m "*" "C:/your/directory" > output.txt ; system start output.txt


Or

lf -r -n -c -m "*" "C:/your/directory" > output.xls ; system start output.xls



lf = list files, -r = recursive, -n = name, -c = creation time, -m = modification time

jamr_xx asked
How can I adjust this to check in multiple directories and only output files having a timestamp created today?

lf -r -n -c -m "*" "C:/your/directory" ($fctime>="20091207") > output.txt


Or

lf -r -n -c -m "*" "C:/your/directory" ($fctime>="20091207") > output.xls



20091207 is today's date. We compare the $fctime (file creation time) with it.

You will need biterscripting, it is free. Just google it up.
0
Thank you
I a little bit changed sharpman's script to additionally fetch files' attributes. To run it in Excel 2002 go to Service -> Macros -> Visual Basic Editor, right click on Sheet1 -> Insert -> Module, past bellow code under General section. Than go to Run -> Run Sub/UserForm (F5), input MainExtractData into Macro Name field and press Run button.

Public X()
Public I As Long
Public objShell, objFolder, objFolderItem
Public FSO, oFolder, Fil

Sub MainExtractData()

Dim NewSht As Worksheet
Dim MainFolderName As String
Dim TimeLimit, j, k As Long, StartTime As Double
Dim Y()

ReDim X(1 To 65536, 1 To 6)

Set objShell = CreateObject("Shell.Application")
TimeLimit = Application.InputBox("Please enter the maximum time that you wish this code to run for in minutes" & vbNewLine & vbNewLine & _
"Leave this at zero for unlimited runtime", "Time Check box", 0)
StartTime = Timer

' This is the part I need to change to create a Table with these field names.

Application.ScreenUpdating = False
MainFolderName = BrowseForFolder()
Set NewSht = ThisWorkbook.Sheets.Add

X(1, 1) = "Attributes"
X(1, 2) = "File Name"
X(1, 3) = "Last Accessed"
X(1, 4) = "Last Modified"
X(1, 5) = "Created"
X(1, 6) = "Size"
X(2, 1) = "Attributes: v - Disk drive volume label; l - Alias (link or shortcut); c - Compressed"
' I is defined Publicly as a Long Integer
i = 2

Set FSO = CreateObject("scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(MainFolderName)
'error handling to stop the obscure error that occurs at time when retrieving DateLastAccessed
On Error Resume Next
i = I + 1
Call GetAttr(oFolder)
For Each Fil In oFolder.Files
Set objFolder = objShell.Namespace(oFolder.Path)
Set objFolderItem = objFolder.ParseName(Fil.Name)
i = I + 1
If I Mod 20 = 0 And TimeLimit <> 0 And Timer > (TimeLimit * 60 + StartTime) Then
GoTo FastExit
End If
If I Mod 50 = 0 Then
Application.StatusBar = "Processing File " & i
DoEvents
End If
Call GetAttr(Fil)
Next

'Get subdirectories
If TimeLimit = 0 Then
Call RecursiveFolder(oFolder, 0)
Else
If Timer < (TimeLimit * 60 + StartTime) Then Call RecursiveFolder(oFolder, TimeLimit * 60 + StartTime)
End If

FastExit:
'To prevent 65536 rows in a sheet
Application.StatusBar = "Data copying"
DoEvents
ReDim Y(1 To i, 1 To 6)
For j = 1 To i
For k = 1 To 6
Y(j, k) = X(j, k)
Next
Next
Erase X
Application.StatusBar = "Transferring data to a sheet"
DoEvents
Range("A:F").NumberFormat = "@" 'Text
Range("A2:F2").Merge
Range("A1:F" & i) = Y
Erase Y
Range("A:F").WrapText = False
Range("A:F").EntireColumn.AutoFit
Range("1:1").Font.Bold = True
Rows("2:2").Select
ActiveWindow.FreezePanes = True
Range("a1").Activate

Set FSO = Nothing
Set objShell = Nothing
Set oFolder = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing
Set Fil = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub

Sub RecursiveFolder(xFolder, TimeTest As Long)
Dim SubFld
For Each SubFld In xFolder.SubFolders
i = I + 1
Call GetAttr(SubFld)
Set oFolder = FSO.GetFolder(SubFld)
Set objFolder = objShell.Namespace(SubFld.Path)
For Each Fil In SubFld.Files
Set objFolder = objShell.Namespace(oFolder.Path)
'Problem with objFolder at times
If Not objFolder Is Nothing Then
Set objFolderItem = objFolder.ParseName(Fil.Name)
i = I + 1
If I Mod 20 = 0 And TimeTest <> 0 And Timer > TimeTest Then
Exit Sub
End If
If I Mod 50 = 0 Then
Application.StatusBar = "Processing File " & i
DoEvents
End If
Call GetAttr(Fil)
Else
Debug.Print Fil.Path & " " & Fil.Name
End If
Next
Call RecursiveFolder(SubFld, TimeTest)
Next
End Sub

Function BrowseForFolder(Optional OpenAt As Variant) As Variant
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that directory
'NOTE: If invalid, it will open at the Desktop level

Dim ShellApp As Object

'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0

'Destroy the Shell Application
Set ShellApp = Nothing

'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select

Exit Function

Invalid:
'If it was determined that the selection was invalid, set to False
BrowseForFolder = False

End Function

Sub GetAttr(Fil)
Dim a
'1 - Read-only file; 2 - Hidden file; 4 - System file;
'8 - Disk drive volume label; 16 - Folder or directory;
'32 - Archive file (file has changed since last backup);
'64 - Alias (link or shortcut); 128 - Compressed file;
a = IIf(Fil.Attributes And 1, "r", "-") & _
IIf(Fil.Attributes And 2, "h", "-") & _
IIf(Fil.Attributes And 4, "s", "-") & _
IIf(Fil.Attributes And 8, "v", "-") & _
IIf(Fil.Attributes And 16, "d", "-") & _
IIf(Fil.Attributes And 32, "a", "-") & _
IIf(Fil.Attributes And 64, "l", "-") & _
IIf(Fil.Attributes And 128, "c", "-")

X(i, 1) = a
X(i, 2) = Fil.Path
X(i, 3) = Fil.DateLastAccessed
X(i, 4) = Fil.DateLastModified
X(i, 5) = Fil.DateCreated
X(i, 6) = Fil.Size
End Sub
Posts
3
Registration date
Friday September 11, 2009
Status
Member
Last seen
September 19, 2009
-1
Thank you
hi sharpman,

Thank you very much for your help. It works perfectly as expected.
Thank you thank you thank you...
I really got limited knowledge in what dos commands can do but learning and studying right now.
Thanks again.
sharpman
Posts
1022
Registration date
Saturday May 23, 2009
Status
Contributor
Last seen
October 20, 2010
214 -
your welcome mhel_0414
> sharpman
Posts
1022
Registration date
Saturday May 23, 2009
Status
Contributor
Last seen
October 20, 2010
-
Hi Sharpman,
How can I adjust this to check in multiple directories and only output files having a timestamp created today?

Thanks,
Jamr_xx
Posts
3
Registration date
Friday September 11, 2009
Status
Member
Last seen
September 19, 2009
-2
Thank you
thank you for the help sharpman but still, it doesn't show both the date created and date modified of the files..=(
-2
Thank you
Hi williams,

try this below code.This will give you all the filenames under the given directory.
please modify the "strcpy(command,"dir /S /B ");" based on your
input.

Code:
int ExtractAllFilesFromDir(char *argv[])
{
/*Variable declaration*/
char command[1024];
char buffer[_MAX_PATH];

printf("\n*************EXE USAGE*************:\n");
//printf("utility_no Directory_PathName InputFile_Extension(*.*,*.txt,*.c etc)\n");
//printf("Eg: 20 \"D:\\Source_Code\" *.*\n");

/*Creating the file list for given command in the DOS window - /b(display only file names) /o(sorted order) */
//strcpy(command,"dir /S /B *.* >>FileList.txt");
strcpy(command,"dir /S /B ");
strcat(command,argv[3]);
strcat(command," >>FileList.txt");

/* Get the current working directory: */
if( _getcwd( buffer, _MAX_PATH ) == NULL )
{
perror( "_getcwd error" );
return -1;
}

/*Changing the directory for the giving path*/
if( _chdir( argv[2] ) )
{
printf( "Unable to locate the directory: %s\n", argv[2] );
return -1;
}
else
{
system("del filelist.txt");
system( command);
}

/*Create the output filename path*/
strcpy(command,"\"");
strcat(command,argv[2]);
strcat(command,"\\");
strcat(command,"FileList.txt");
strcat(command,"\" ");

printf("\nExtracting FileName Process Ends..\n");

/*Open the output file*/
printf("\nOutput File is copied in \n%s ",command);
system(command);
return 0;
}

with regards,
sakthi narayanan.D