Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to list file path and file name in a csv

Tags:

batch-file

I am trying to make a batch file that lists all files in in a folder and sub directors and exports to a csv with file size, I curently have this:

@ECHO OFF &SETLOCAL

(FOR /f "delims=|" %%a  IN ('dir /s /b  /a-d') DO (
    FOR /f "tokens=1-9*" %%x IN ('dir /b  /a-d /tc  "%%~a"^| C:\Windows\System32\findstr "^[0-9]"') DO (

        ECHO %%a, %%z
    )
))>DIR.csv
TYPE DIR.csv

But what I need is the File directory and File path as separate records

like image 498
Robbo Avatar asked Oct 04 '13 14:10

Robbo


Video Answer


1 Answers

This can be accomplished with a simple one liner directly from the command line - no batch required.

File names can contain comma, so they should be quoted in your CSV. The following will create a csv with file path, file name, file size on each line.

(for /r %F in (*) do @echo "%~dpF","%~nxF",%~zF) >dir.csv

Double up the percents if used within a batch script.

Type HELP FOR or FOR /? from the command line for documentation on the FOR command. At the bottom is a description of all of the modifiers that can be used when expanding a FOR variable value.

like image 71
dbenham Avatar answered Oct 13 '22 01:10

dbenham