2

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:

  1. File name (this can include commas!)
  2. Creation date
  3. 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?

4
  • SO is not a "write code for me free" type of site. What have you tried so far? Commented Aug 6, 2013 at 19:10
  • @AnsgarWiechers & Ken White: You are both correct. I was in too much of a hurry when I wrote the question and forgot to describe my attempts at solving the problem. Commented Aug 6, 2013 at 21:02
  • Please provide your local time format, European or AM/PM? Commented Aug 6, 2013 at 21:19
  • @Endoro: AM/PM - The DIR command gives the timestamp as mm/dd/yyyy hh:mm AM (or PM) Commented Aug 6, 2013 at 21:27

5 Answers 5

5

try this:

@ECHO OFF &SETLOCAL
(FOR /f "delims=" %%a IN ('dir /b /a-d') DO (
    FOR /f "tokens=1-3*" %%x IN ('dir /a-d /tc "%%~a"^|findstr "^[0-9]"') DO (
        ECHO "%%a",%%~ta,%%x %%y %%z
    )
))>DIR.csv
TYPE DIR.csv
Sign up to request clarification or add additional context in comments.

1 Comment

This also does the job, and is a bit easier to understand.
2

Something like this might work:

@echo off

setlocal EnableDelayedExpansion

(
  echo "Name","Modification Time","Creation Time"
  for %%f in (*) do (
    set "name=%%~nxf"
    if not "!name!"=="%~nx0" (
      set "mtime=%%~tf"
      for /f "tokens=1-3" %%d in (
        'dir /t:c "!name!" ^| find /i "!name!"'
      ) do set "ctime=%%~d %%~e %%~f"
      echo "!name!","!mtime!","!ctime!"
    )
  )
) > output.csv

3 Comments

Thanks! Although the names of batch file and the csv file aren't excluded, I'm sure that I can figure that part out. I would upvote the answer, but I don't have enough reputation.
The name of the batch file is excluded. The name of the CSV isn't, though.
You are correct. I was looking at a different file on the list.
1

Here is a command line one liner that does not include the newly create list file in the list:

(for /f "tokens=1-4*" %A in ('dir /a-d /tc^|findstr "^[0-9]"') do @if "%E" neq "FileList.csv" echo "%E",%A %B %C,%~tE)>"FileList.csv"

Here is a batch script that does not include itself or the newly created list file:

@echo off
>"FileList.csv" (
  for /f "tokens=1-4*" %%A in (
    'dir /a-d /tc^|findstr "^[0-9]"'
  ) do if "%~f0" neq "%%~fE" if "%%E" neq "FileList.csv" echo "%%E",%%A %%B %%C,%%~tE
)

Comments

0

A one-line PowerShell command to write directory listing to CSV

Comments

0

I had a requirement to read the number of lines in two files and output to a csv file.

@ECHO OFF
cd "D:\CSVOutputPath\"
echo Process started, please wait...
echo FILENAME,FILECOUNT> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Salary.txt"') do set Count=%%C
echo Salary,%Count%>> SUMMARY.csv
for /f %%C in ('Find /V /C "" ^< "D:\Trial\Tax.txt"') do set Count=%%C
echo Tax,%Count%>> SUMMARY.csv

The > will overwrite the existing content of the file and the >> will append the new data to existing data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.