问题描述
我跑过这个code在另一篇文章,几乎做什么,我需要的,但不能想出如何修改它来寻找特定的文件类型,如* .bak的, .TXT等我使用PowerShell工具[System.IO.DirectoryInfo]因为像其他人所说,使用Get-ChildItem是在网络速度太慢。我认为这将只是类似$ fileEntries = [IO.Directory] ::的GetFiles(C:\,的.bak的),但它仍然会返回在每一个目录中的所有文件。 --PS / .NET新手
尝试
{
$ fileEntries = [IO.Directory] ::的GetFiles(C:\)
[System.IO.DirectoryInfo] $ directInf =新对象IO.DirectoryInfo(C:\)
$文件夹= $ directInf.GetDirectories()
}
抓[例外]
{
$ _。Exception.Message
$文件夹= @()
}
的foreach($ filename在$ fileEntries)
{
#[控制台]:的WriteLine($文件名);
}
删除变量-ErrorAction SilentlyContinue -Name fileEntries
的foreach(在$文件夹$文件夹)
{
递归(C:\+ $文件夹+\)
}
这将通过每个扩展搜索根和子目录中的所有文件循环。请确保您有所有目录的正确权限,特别是当你从C运行:\作为根
$扩展= @(比克,CSV,TXT)
的foreach($扩展在$扩展)
{
[System.IO.Directory] :: EnumerateFiles(C:\,* $扩展,AllDirectories)
}
此方法只使用PowerShell运行.NET 4.0或更高的工作。要检查和更新的.Net版本:
$ PSVersionTable
名称值
---- -----
CLRVERSION 2.0.50727.4971
BuildVersion 6.1.7600.16385
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0,2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
该CLRVERSION值是.NET版本。
更新配置文件如下:
$配置= @
< XML版本=1.0&GT?;
<结构>
<启动useLegacyV2RuntimeActivationPolicy =真正的>
< supportedRuntime版本=v4.0.30319/>
< supportedRuntime版本=V2.0.50727/>
< /启动>
< /结构>
@
$配置> $ PSHome的\ Powershell.exe.config
重新启动PowerShell会话并验证在$ PSVersionTable变量CLRVERSION值。
I ran across this code in another post that almost does what I need, but can't figure out how to modify it to look for specific file types, i.e. *.bak, .txt, etc. I'm using Powershell with [System.IO.DirectoryInfo] because, like others have stated, using Get-ChildItem is too slow across the network. I thought it would just be something like $fileEntries = [IO.Directory]::GetFiles("C:\", ".bak"), but it still returns every file in every directory. --PS/.NET newbie
try
{
$fileEntries = [IO.Directory]::GetFiles("C:\")
[System.IO.DirectoryInfo]$directInf = New-Object IO.DirectoryInfo("C:\")
$folders = $directInf.GetDirectories()
}
catch [Exception]
{
$_.Exception.Message
$folders = @()
}
foreach($fileName in $fileEntries)
{
#[Console]::WriteLine($fileName);
}
Remove-Variable -ErrorAction SilentlyContinue -Name fileEntries
foreach($folder in $folders)
{
recurse("C:\" + $folder + "\")
}
This will loop through each extension searching for all files in the root and sub-directories. Ensure you have the correct privileges on all the directories especially when you're running from C:\ as the root.
$Extensions = @(".bak",".csv",".txt")
Foreach ( $Extension in $Extensions )
{
[System.IO.Directory]::EnumerateFiles("C:\","*$Extension","AllDirectories")
}
This method will only work with Powershell running .Net 4.0 or higher.To check and update the version of .Net:
$PSVersionTable
Name Value
---- -----
CLRVersion 2.0.50727.4971
BuildVersion 6.1.7600.16385
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
The CLRVersion value is the .net version.
Update the config file as follows:
$Config = @"
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
"@
$Config > $PSHOME\Powershell.exe.config
Restart the Powershell session and verify the CLRVersion value in the $PSVersionTable variable.
这篇关于PowerShell的 - IO.Directory - 查找所有子目录中的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!