I can print directory contents (Windows 10) with Mode, LastWriteTime etc included but I want to use something like dir > test.csv to only print the Name of each file. Is there such a way to do this?
3 Answers
Try like this:
(dir "PATH" -file | select name) >file.csv
Basename only returns the file name of the item. To filter only files add the -File switch with dir and to get recursively add -Recurse. You can add other properties comma separated, too.
5 Comments
Adrift
So this works but I need file extensions and don't need mode nor LastWriteTime.
avarice
Modified as per your need
Adrift
Thank you but now I'm getting a blank CSV file? I used:
(dir "C:\Users\NAME.OFF\desktop\New Folder" -file | select name) > file.csvLee_Dailey
@adrift - confirm that your target dir has files in it. the code you show SHOULD work if there is at least one file in the target dir ... [grin]
GuidoG
I am also getting a blank csv file, when I execute it without the
> file.csv then it shows me all filenames correct, but with the > file.csv iti produces a blank file. Did you ever resolved this ?
(Ls "$Env:UserProfile\Desktop\New Folder" -File -Fo).Name > file.csv. Please note that your CSV will probably be encoded as UTF-16 with the little-endian byte order. If you want a different encoding type, UTF8 or ASCII for instance, do it like this instead,Get-ChildItem -Path "$Env:UserProfile\Desktop\New Folder" -File -Force | Select-Object -ExpandProperty Name | Set-Content -Encoding UTF8 -Path ".\file.csv", or shortenedLs "$Env:UserProfile\Desktop\New Folder" -File -Fo|Select -Exp Name|SC -En ASCII file.csv