我一直试图找到一个脚本,递归打印这样的目录中的反斜杠用于指示目录中的所有文件和文件夹:PowerShell脚本列出目录中的所有文件和文件夹
Source code\
Source code\Base\
Source code\Base\main.c
Source code\Base\print.c
List.txt
我使用的PowerShell 3.0和我发现的大多数其他脚本不起作用(虽然他们没有像我问的东西)。
此外:我需要它是递归的。
我一直试图找到一个脚本,递归打印这样的目录中的反斜杠用于指示目录中的所有文件和文件夹:PowerShell脚本列出目录中的所有文件和文件夹
Source code\
Source code\Base\
Source code\Base\main.c
Source code\Base\print.c
List.txt
我使用的PowerShell 3.0和我发现的大多数其他脚本不起作用(虽然他们没有像我问的东西)。
此外:我需要它是递归的。
这可能是这样的:
$path = "c:\Source code"
DIR $path -Recurse | % {
$_.fullname -replace [regex]::escape($path), (split-path $path -leaf)
}
继@Goyuix想法:
$path = "c:\source code"
DIR $path -Recurse | % {
$d = "\"
$o = $_.fullname -replace [regex]::escape($path), (split-path $path -leaf)
if (-not $_.psiscontainer) {
$d = [string]::Empty
}
"$o$d"
}
什么你很可能在寻找的东西,以帮助区分从文件夹中的文件。幸运的是,有一个属性叫PSIsContainer
,对于文件夹是真实的,对于文件是虚假的。
dir -r | % { if ($_.PsIsContainer) { $_.FullName + "\" } else { $_.FullName } }
C:\Source code\Base\
C:\Source code\List.txt
C:\Source code\Base\main.c
C:\Source code\Base\print.c
如果前面的路径信息是不可取的,你可以很轻松地使用-replace
其删除:
dir | % { $_.FullName -replace "C:\\","" }
希望这可以让你在正确的方向前进了。
用于查看'\'作为文件夹标记的+1;) – 2013-03-01 20:20:15
@Guvante'''必须在正则表达式中逃脱! '-replace'的第一个参数是一个正则表达式! – 2013-03-01 20:28:28
感叹号使它更加直接。 http://knowyourmeme.com/memes/the-1-phenomenon – EBGreen 2013-03-01 20:43:08
dir | % {
$p= (split-path -noqualifier $_.fullname).substring(1)
if($_.psiscontainer) {$p+'\'} else {$p}
}
(ls $path -r).FullName | % {if((get-item "$_").psiscontainer){"$_\"}else{$_}}
只有在PS 3.0使用
这一个显示完整路径,如一些其他的答案的事,但更短:
ls -r | % { $_.FullName + $(if($_.PsIsContainer){'\'}) }
然而,OP相信询问相对路径(即相对于当前目录),只有@ CB的回答解决了这一点。因此,只需添加一个substring
我们有这个:
ls -r | % { $_.FullName.substring($pwd.Path.length+1) + $(if($_.PsIsContainer){'\'}) }
不PowerShell的,但你可以使用命令提示符中的以下递归列出文件到一个文本文件:
dir *.* /s /b /a:-d > filelist.txt
的PowerShell命令指南清单到TXT文件:
完全路径目录列表(文件夹&文件)以文本文件:
ls -r | % { $_.FullName + $(if($_.PsIsContainer){'\'}) } > filelist.txt
相对路径目录列表(文件夹&文件)以文本文件:
ls -r | % { $_.FullName.substring($pwd.Path.length+1) + $(if($_.PsIsContainer){'\'}) } > filelist.txt
@Melab参数添加到您的脚本传递路径,在所有的答案在这里的路径是硬代码为方便...阅读有关如何创建与参数PowerShell脚本... – 2013-03-02 21:00:10
我该怎么做?我怀疑它会以正确的方式命令他们。看到我对Goyux的回答的评论。 – Melab 2013-03-03 15:45:22
@Melab我认为是时候尝试自己做点什么了...... – 2013-03-03 15:59:10