I need to create a Windows batch file that generates a .csv file with three fields for all the files in a directory (minus the batch file itself!).
Fields:
- File name (this can include commas!)
- Creation date
- Modified date
Note that the batch file will be run from the directory, and should know to generate the .csv file for the files in the current directory, but should not list the batch file or the generated .csv file.
Powershell, vbscript, and the like are not feasible in my situation, so it has to be a Windows batch file.
EDIT:
Here are the two main approaches that I have tried.
First attempt: I've tried separating the bare file names into "temp1.tmp", the file list with the created timestamps into "temp2.tmp", and the file list with the modified timestamps into "temp3.tmp".
DIR /B /A:-D-H | FIND /V "Print Files.bat" | FIND /V "File List" | FIND /V "temp" > "temp1.tmp"
DIR /A:-D-H /T:C | FIND /V "Print Files.bat" | FIND /V "File List" | FIND /V "temp" > "temp2.tmp"
DIR /A:-D-H /T:W | FIND /V "Print Files.bat" | FIND /V "File List" | FIND /V "temp" > "temp3.tmp"
MORE +5 "temp2.tmp" > "temp4.tmp"
COPY /Y "temp4.tmp" "temp2.tmp"
MORE +5 "temp3.tmp" > "temp4.tmp"
COPY /Y "temp4.tmp" "temp3.tmp"
DEL /F/Q "temp4.tmp"
But the problem with this approach is that I cannot figure out how to read lines from more than one file at a time. Here is the closest that I have found, but it is only from one file:
FOR /F "delims=" %%i IN (temp1.tmp) DO (
ECHO."%%i" >> "File List.csv"
)
Second attempt: I've tried avoiding using temporary files, but to no avail.
FOR %%i IN (*.*) DO @ECHO "%%~ni%%~xi",%%~ti >> "File List.csv"
The problem with this second approach is that it doesn't exclude the batch file name, and it doesn't provide the file creation timestamp.
Is there a solution to make either of these work for me, or an approach that I haven't tried yet?