0

Windows, Command Prompt, need to generate a .txt file output containing of all files from a big and complex dir tree with one (1) line for each files as:

CreationDateYYYYMMDD-HHMMSS, LastModifiedYYYYMMDD-HHMMSS, filesize[no K commas], filename.ext

for example:

20100101-174503, 20120202-191536, 1589567, myfile.ext

The list should not contain lines of dir name entries, etc., only filenames, even if the same file is present in more than once. Time in 24 hours format.

dir /s/t:c/t:w/-c > filelist.txt

command does not exactly works this way.

user3026965
  • 673
  • 4
  • 8
  • 22

2 Answers2

0
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=c:\program files"
FOR /f "delims=" %%a IN (
  'dir /s /b /a-d "%sourcedir%\*" '
  ) DO (
  FOR %%d IN (timewritten timecreated) DO SET "%%d="
  FOR %%k IN (-d s h) DO (
   IF NOT DEFINED timewritten FOR /f "tokens=1,2 delims= " %%d IN ('dir /tw %%~k "%%a" 2^>nul ^|find "%%~nxa"') DO SET "timewritten=%%d %%e"
   IF NOT DEFINED timecreated FOR /f "tokens=1,2 delims= " %%d IN ('dir /tc %%~k "%%a" 2^>nul ^|find "%%~nxa"') DO SET "timecreated=%%d %%e"
  )
  ECHO !timecreated! !timewritten! %%~za %%~nxa
  )
)
GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

Interesting problem. This code processes it by

First, applying the standard directory-list for filenames on the tree from the relative root (%sourcedir%) to %%a

Using the full filename in %%a, set timewritten and timecreated from an ordinary dir list targeting the file in question.

It appeared that %%~ta didn't play nicely to extract the timestamp for hidden and system files, so I decided to build them from the ordinary dir listing with the appropriate t setting, specifically listing with /a-d, /as and /ah and filtering for the line which matched the filename, which seemed to extract the data appropriately.

I left the date/time in raw format. It should be an easy task to extract the various elements and construct the report in the format you want.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 1.) How can I output the results into a text.txt file? 2.) Any way to configure the script so the .bat file can stay unchanged in one fixed directory (C:\script\script.bat, for example) and the actual directory path for which I need to generate the filelist can be pasted (need to do often and for long paths) into the command line (vs. editing the script every time to insert the path)? – user3026965 Feb 04 '16 at 10:26
  • SET "sourcedir=%cd%" This seems to make it to apply for the current directory. – user3026965 Feb 04 '16 at 10:48
  • "script.bat > results.txt" written in the command line will print to text file. – user3026965 Feb 04 '16 at 11:00
  • if *thisbatch*.bat is located in any directory in the `path` (enter `path` at the prompt for semicolon-separated list) then typing *thisbatch* will run this code. You could replace the setting od `sourcedir` with `set /p "sourcedir=a prompt "` to set it from the keyboard, or you could use `set "sourcedir=%~1"` to set it from the first parameter supplied to the batch, so *thisbatch* `"c:\my directory\wherever"` would assign the supplied directoryname as the first parameter (`%1`) The `~` removes any `"` around the string, which can be supplied without quotes if it doesn't contain spaces. – Magoo Feb 04 '16 at 11:29
0

This question is a dupe of the SO post cmd dir /b/s plus date, but posting what worked for me:

@echo off
REM list files with timestamp
REM Filename first then timestamp
for /R %I in (*.*) do @echo %~dpnxI %~tI

@echo off
REM list files with timestamp
REM Timestamp first then name
for /R %I in (*.*) do @echo %~tI %~dpnxI

The above are the versions that you would directly paste into a command prompt. If you want to use these in a batch file and log the output, you could do something like:

rem: Place the following in a batch file such as DirectoriesBareWithTS.cmd.
rem: As the first step in the batch file, net use to the directory or share you want the listing of
rem: Change to your target directory 
Y:
for /R %%I in (*.mp4) do @echo %%~tI %%~dpnxI 

Then you can pipe the output to a log file when you execute:

DirectoriesBareWithTS.cmd > C:\temp\GiantLongDirListing.log

You could then import that log into Excel.

Charlesdwm
  • 71
  • 5