ash*_*h g 2 directory powershell file exists
我有一个根目录,包含许多文件夹和子文件夹.我需要检查文件夹或子文件夹中是否存在*.sln或*.designer.vb等特定文件,并将结果输出到文本文件中.
对于Eg:
$root = "C:\Root\"
$FileType = ".sln",".designer.vb"
Run Code Online (Sandbox Code Playgroud)
文本文件的结果有点如下:
.sln ---> 2 files
.sln files path ---->
c:\Root\Application1\subfolder1\Test.sln
c:\Root\Application2\subfolder1\Test2.sln
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感谢!
此致,Ashish
试试这个:
function Get-ExtensionCount {
param(
$Root = "C:\Root\",
$FileType = @(".sln", ".designer.vb"),
$Outfile = "C:\Root\rootext.txt"
)
$output = @()
Foreach ($type in $FileType) {
$files = Get-ChildItem $Root -Filter *$type -Recurse | ? { !$_.PSIsContainer }
$output += "$type ---> $($files.Count) files"
foreach ($file in $files) {
$output += $file.FullName
}
}
$output | Set-Content $Outfile
}
Run Code Online (Sandbox Code Playgroud)
我把它变成了一个函数,你的值作为默认参数值.通过使用来调用它
Get-ExtensionCount #for default values
Run Code Online (Sandbox Code Playgroud)
要么
Get-ExtensionCount -Root "d:\test" -FileType ".txt", ".bmp" -Outfile "D:\output.txt"
Run Code Online (Sandbox Code Playgroud)
输出保存到文件ex:
.txt ---> 3 files
D:\Test\as.txt
D:\Test\ddddd.txt
D:\Test\sss.txt
.bmp ---> 2 files
D:\Test\dsadsa.bmp
D:\Test\New Bitmap Image.bmp
Run Code Online (Sandbox Code Playgroud)
要在开始时获取所有文件计数,请尝试:
function Get-ExtensionCount {
param(
$Root = "C:\Root\",
$FileType = @(".sln", ".designer.vb"),
$Outfile = "C:\Root\rootext.txt"
)
#Filecount per type
$header = @()
#All the filepaths
$filelist = @()
Foreach ($type in $FileType) {
$files = Get-ChildItem $Root -Filter *$type -Recurse | ? { !$_.PSIsContainer }
$header += "$type ---> $($files.Count) files"
foreach ($file in $files) {
$filelist += $file.FullName
}
}
#Collect to single output
$output = @($header, $filelist)
$output | Set-Content $Outfile
}
Run Code Online (Sandbox Code Playgroud)
以下一行代码用于确定目录 $OutputPath 中是否至少存在一个扩展名为 .txt 或 .ps1 的文件:
(Get-ChildItem -Path $OutputPath -force | Where-Object Extension -in ('.txt','.ps1') | Measure-Object).Count
Run Code Online (Sandbox Code Playgroud)
说明:该命令告诉您指定目录中与任何列出的扩展名匹配的文件数。您可以附加-ne 0
到末尾,这会返回 true 或 false 以在if块中使用。
归档时间: |
|
查看次数: |
8547 次 |
最近记录: |