Directory Listing includes Date Modified, File Size into text file/excel file
Solution 1
powershell ls -r -fo Z:\ ^|?{!$_.PSIsContainer}^|Select DirectoryName,Name,BaseName,Extension,Length,CreationTime,LastAccessTime,LastWriteTime ^|epcsv Z:\excel.csv -En UTF8 -NoType -Delim ';'
^|
-^
- mask transporter/pipe symbol in cmd,|
- pipe object$_
- variable for the current object in the pipe line; sample:powershell 'a','B','c','d','F' ^|%{if($_.toLower() -gt 'b'){write $_}}
?
=where
- check is not directory?{!$_.PSIsContainer}
cycle{}
ls -r
- get all file in/and all subdirectory and current directory-fo
=-force
- add to list hidden, system and read-only attribyte file'Z:\' - directory path, if use 'Z:' - set current directory at Z: cd.
select
- select properties atls
pipe objectepcsv
=Export-Csv
- Export a PowerShell object to separated values (CSV) file.-En
=-Encoding
- Encoding string The encoding for the exported CSV file. Valid values are: Unicode, UTF7, UTF8, ASCII, UTF32, BigEndian unicode, Default, and OEM. The default is ASCII.-NoType
=-NoTypeInformation
- Omit the type information from the CSV file.-Delim ';'
--Delimiter
char A delimiter to separate the property values. The default is a comma (,). Enter a character, such as a colon (:). To specify a semicolon (;), enclose it in quotation marks.
result:
Solution 2
The above has issues with spaces in directory names I found, this basic powershell command works better in my experience:
Get-ChildItem -r "Z:\" | select DirectoryName,Name,Extension,CreationTime,AccessTime,LastWriteTime | Export-Csv -Append -Path "Z:\export.csv" -En UTF8 -NoType -Delim ','
In addition using the Delimiter ";" is confusing to Excel and changing this to "," means excel can open the exported CSV without any further manipulation of the data.
Related videos on Youtube
Kurisuchin
Student | Intern | Newbie Programmer Bear with my questions/answers don't make any sense. In time, I'll be able to change this "about me" stuff to something very reliable and sensible.
Updated on September 18, 2022Comments
-
Kurisuchin almost 2 years
I want to list an entire drive's (Z:) directories, subdirectories, and files in a single text file with all the dates and the file sizes. I can then open the file inside Excel.
I am currently using the following command:
dir z:\ /s /o:gne >text.txt
Is there any way that I can get an output similar to what you usually get with the tree command, with all the files and subdirectories stacked in one and not listed separately?
What do I have to do or to input if I wanted to remove other unnecessary information like the time?
-
Kurisuchin over 9 yearsOh it worked. I have to check.
-
STTR over 9 years@CharisseDaitol Excellent! Can change the output by adding and removing properties in the
select
. -
Kurisuchin over 9 yearsyep, I've tried it. Since I don't necessarily need to show every property.Thank you again.