Ben
Ben

Reputation: 1011

Print a directory listing with full filename and path?

I am trying to create a simple txt file as follows with two columns - the first is a date of the last modified date and the next is the full filename (including path):

/dir > list.txt almost does the job but it groups all the files by directory and I am trying to get the directory on the same line as the filename and last modified date.

Do I need to use a batch script? or CMD line? I'm using Windows XP.

YYYY/MM/DD   C:/Directory1/Directory2/Directory3/filename.txt
YYYY/MM/DD   C:/Directory1/Directory2/Directory3/filename.txt
YYYY/MM/DD   C:/Directory1/Directory2/Directory3/filename.txt
YYYY/MM/DD   C:/Directory1/Directory2/Directory3/filename.txt
YYYY/MM/DD   C:/Directory1/Directory2/Directory3/filename.txt

This almost works too but I can't get the last modified date:

dir /s /b > list.txt

Note - I will place this cmd or .bat file in the parent directory AND I can't install any programs or software (i.e. python, perl, etc.) solution must be simple CMD line or .bat file.

Upvotes: 1

Views: 4810

Answers (3)

Kent Kruckeberg
Kent Kruckeberg

Reputation: 371

I needed the same thing. I wanted to get the last modified date for all files in a directory and all subdirectories and output it to a file as did the OP. Magoo has listed the guts of it but what is missing is the saving to a file. So, this is what worked nicely from the command prompt:

for /f "delims=" %a in ('dir/s/b/a-d') do echo %~ta %a >>c:\temp\filelist.txt

Upvotes: 0

Magoo
Magoo

Reputation: 79947

for /f "delims=" %%a in ('dir/s/b/a-d') do echo %%~ta %%a

as a batch line, reduce %% throughout to % if you are running from the prompt.

(well - that gives you the date and time, then the filename - do you really want just the date?)

@ECHO OFF
SETLOCAL
for /f "delims=" %%a in ('dir/s/b/a-d') do (
 FOR /f "tokens=1-3delims=/: " %%q IN ("%%~ta") DO echo %%s/%%r/%%q %%a
)

GOTO :EOF

yields the yyyy/mm/dd format you've specified on my system but I use a date format of dd/mm/yyyy - you haven't specified yours, so details may differ.

Upvotes: 3

r3ap3r
r3ap3r

Reputation: 2815

Since you are using Windows XP, it would be easier to get the required file properties using Powershell when compared to CMDLine/Batch Script. It is a bit cumbersome to retrieve last modified date of a file using CMD.

Try this :

$files=Get-ChildItem -recurse "full_path_to_test_directory"
for($i=0;$i -lt $files.Count;$i++) {
$f=$files.Get($i)
$fName=$f.FullName
$fModified=Get-Date $f.LastWriteTime -Format "yyyy\\MM\\dd"
$out="$($fModified)    $($fName)"
Add-Content -Path "full_path_to_destination_file" -Value $out

}

Save this as getFileProperties.ps1 and run it from the Powershell console.

You can also run the above powershell script from Command Line as follows:

powershell -File "full_path_to_above_powershell_script"

Upvotes: 1

Related Questions