batch script to store the filename,date modified and the file size of a specific directory into a .csv is resulting in a syntax error

Viewed 574

I need to loop a set of files along a particular directory and store their filename,date modified and filesize into a .csv file.the below code is giving me syntax errors. Kindly help on the below:

setlocal enabledelayedexpansion enableextensions
set baseDIR=%1 
cd %baseDIR% 
FOR /R %%a IN (*) DO 
    "%%a","%%~ta","%%za" >>test.csv
exit /b %errorlevel%
4 Answers

Refining earlier answers here, below is a working script. Save the following to a file named CSVDir.cmd:

@ECHO OFF
SETLOCAL
IF /I "%1" == "/R" SET RECURSE=/R
IF /I "%1" == "/R" SHIFT
IF NOT "%1" == "" cd /D "%1"
( 
    ECHO filename,dateModified,filesize
    FOR %RECURSE% %%a IN (*) DO ECHO "%%a","%%~ta",%%~za
)

Usage examples:

CSVDir -- non-recursive CSV listing of current directory

CSVDir /r -- recursive CSV listing of current directory

CSVDir C:\Some\Folder\Path -- non-recursive CSV listing of C:\Some\Folder\Path

CSVDir /R C:\Some\Folder\Path -- recursive CSV listing of C:\Some\Folder\Path

NOTE that without the /R flag, it will list "bare" filenames, without their full path. Otherwise all filenames are listed with their full path.

Related