简体   繁体   中英

How can I list and print files in a directory?

I'm trying this but it doesn't print anything:

Dir -Recurse "C:\temp" | Select Fullname

Looks like this command just selects file names. I want to see them in console.

看看Get-Childitem

Dir -Recurse c:\path\ | Get-Childitem

Concerning your code in the question.

Your command should have worked as is. You are, in fact, already calling Get-ChildItem . If you check Get-Alias you will see what I'm trying to tell you.

PS C:\users\Cameron\Downloads> Get-Alias dir

CommandType     Name                                               ModuleName                                                                                               
-----------     ----                                               ----------                                                                                               
Alias           dir -> Get-ChildItem  

You code translates to

Get-ChildItem -Recurse "C:\temp" | Select Fullname

Again, I'm not sure why your code does not generate output since that is perfectly fine on a folder that contains files or directories. Might be an issue with the positional parameter maybe? What is your PowerShell version? ( Use Get-Host ).

The code you have would send all file paths to console. Did you want that output somewhere else?

About the accepted answer

Pretty sure this code will double up output if you have folders in the path since directory will output to the second Get-ChildItem

Dir -Recurse c:\path\ | Get-Childitem

Consider the following folder tree

C:\TEMP\TEST
│   File1.txt
│   File2.txt
│
└───Folder1
        File3.txt

Consider the two command run against that folder tree.

PS C:\users\Cameron\Downloads> Dir -Recurse c:\temp\test | Select Fullname

FullName                                                                                                                                                                    
--------                                                                                                                                                                    
C:\temp\test\Folder1                                                                                                                                                        
C:\temp\test\File1.txt                                                                                                                                                      
C:\temp\test\File2.txt                                                                                                                                                      
C:\temp\test\Folder1\File3.txt                                                                                                                                              



PS C:\users\Cameron\Downloads> Dir -Recurse c:\temp\test | Get-Childitem | Select Fullname

FullName                                                                                                                                                                    
--------                                                                                                                                                                    
C:\temp\test\Folder1\File3.txt                                                                                                                                              
C:\temp\test\File1.txt                                                                                                                                                      
C:\temp\test\File2.txt                                                                                                                                                      
C:\temp\test\Folder1\File3.txt   

The second command shows two files called File3.txt when in reality there is only one.

get-childitem | format-list > filename.txt 

This will give you a text file with name, size, last modified, etc.

if you want specific parameters from the item... such as name of the file only the command is

get-childitem | format-list name > filename.txt

this is give you the same text file, but with just the name of the files listed.

可能还值得一提的是查看隐藏物品所需的 -force 开关。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM