Batch file to output filename to csv file

March 2017


I'm looking to write a windows batch file that will look for a fixed file in a fixed folder (i.e. c:folderfile1.csv) and use the dir command to output the filename and last written date to a second csv file (i.e. c:folderfile2.csv). The output must be in two columns and will look something like this when you open file2.csv:

file1.csv 10/14/2008 15:38

This seems like a fairly straightforward batch, but i'm struggling to limit my output to just these two attributes and place them in the first two columns and first row of file2.csv

Any help would be greatly appreciated. Thanks!!!


The script you need is as follows:

____________________Begin Code__________________

Dim Filepath 
Dim Filename 
Dim OutputFile 

Filepath = "c:test\" 'Edit this to specify the filepath 
Filename = "file1.csv" 'Name of file that last edited date/time is needed 
OutputFile = "file2.csv" 'File to output results to 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
If not objFSO.FileExists(FilePath & OutputFile) then 
Set objFile = objFSO.CreateTextFile(Filepath & OutputFile) 
Wscript.sleep(2000) 'Pause while file is being created 
End If 

If objFSO.FileExists(Filepath & Filename) then 
Set objFile = objFSO.GetFile(Filepath & Filename) 
Lastmodifieddate = objFile.DateLastModified 

Const ForWriting = 2 
Set outputFile = objFSO.OpenTextFile(Filepath & "file2.csv", ForWriting) ' Open file for writing 
OutputFile.Write Filename & "," & objFile.DateLastModified 'Write filename and last modified date to output file 
End If 
set objFSO = Nothing 

____________________End Code__________________

The three variables, filepath, filename and output file that are set up on lines 5, 6 and 7 can be modified to pick up any path and filename that you specify and will create the file on line 7 on the same path.

If you save that code as test.vbs file, then in your batch file, you will need to enter the line "Call test.vbs". Obviously if you change the name of the vbs file, you will need to change the reference to it in your batch file.


Thanks to Swarve for this tip on the forum.


Published by aakai1056.
This document, titled "Batch file to output filename to csv file," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (