Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows PowerShell-在有限的行数内移动包含文本内容的所有文件_Windows_Powershell_Find - Fatal编程技术网

Windows PowerShell-在有限的行数内移动包含文本内容的所有文件

Windows PowerShell-在有限的行数内移动包含文本内容的所有文件,windows,powershell,find,Windows,Powershell,Find,我正在PowerShell中寻找用于查找和移动包含特定字符串的文件的命令 我有一个包含数千个XML文件的文件夹。这些XML文件具有相同的结构,每个文件包含1000多行。因此,Select String命令将遍历所有文件内容,这是不必要的,因为我要查找的字符串位于文件的前10行 因此,我想了解一些如何帮助PowerShell更快地获得结果。(需要递归搜索) 因此,我想找到这些文件(int folderfile\u source),并将它们移动到另一个名为destination的文件夹中。搜索模式为

我正在PowerShell中寻找用于查找和移动包含特定字符串的文件的命令

我有一个包含数千个XML文件的文件夹。这些XML文件具有相同的结构,每个文件包含1000多行。因此,Select String命令将遍历所有文件内容,这是不必要的,因为我要查找的字符串位于文件的前10行

因此,我想了解一些如何帮助PowerShell更快地获得结果。(需要递归搜索)

因此,我想找到这些文件(int folderfile\u source),并将它们移动到另一个名为destination的文件夹中。搜索模式为“\s*A73”(不带引号),我已使用此命令:

Get-ChildItem -path ./file_source -recurse | Select-String -list -pattern "<type>\s*A73" | move -dest ./destination
Get ChildItem-path./file_source-recurse | Select String-list-pattern“\s*A73”| move-dest./destination

谢谢。

您还没有提供任何您尝试执行的代码示例。这就留下了一些有待解释的问题。话虽如此,您可以执行以下操作:

$RootDirectoryToCheck = 'some directory path'
$DestinationDirectory = 'some directory path'
$TextToFind = 'some text'
Get-ChildItem -Path $RootDirectoryToCheck -Filter '*.xml' -File -Recurse |
    where {(Get-Content $_.FullName -TotalCount 10) -match $TextToFind} |
        Move-Item -Destination $DestinationDirectory
说明:

包含从
-Path
开始递归搜索的
-Recurse
参数<代码>-文件确保输出仅包含文件

的参数
-TotalCount
告诉PowerShell仅读取文件的前10行
-match
是一个正则表达式匹配运算符,如果比较单个字符串,它将返回
True
False
。比较字符串集合时,如果匹配成功,它将返回匹配的字符串;如果匹配失败,它将返回
null


然后,匹配的文件可以通过管道传输到。
-Destination
参数可用于指示将文件移动到何处。

您尚未提供任何您尝试执行的代码示例。这就留下了一些有待解释的问题。话虽如此,您可以执行以下操作:

$RootDirectoryToCheck = 'some directory path'
$DestinationDirectory = 'some directory path'
$TextToFind = 'some text'
Get-ChildItem -Path $RootDirectoryToCheck -Filter '*.xml' -File -Recurse |
    where {(Get-Content $_.FullName -TotalCount 10) -match $TextToFind} |
        Move-Item -Destination $DestinationDirectory
说明:

包含从
-Path
开始递归搜索的
-Recurse
参数<代码>-文件确保输出仅包含文件

的参数
-TotalCount
告诉PowerShell仅读取文件的前10行
-match
是一个正则表达式匹配运算符,如果比较单个字符串,它将返回
True
False
。比较字符串集合时,如果匹配成功,它将返回匹配的字符串;如果匹配失败,它将返回
null


然后,匹配的文件可以通过管道传输到。
-Destination
参数可用于指示文件的移动位置。

我怀疑这比读取前10行更快:

(dir <SourcePath> -Recurse -File | Select-String -Pattern <SearchTerm> -List).Path | Move-Item -Destination <DestinationPath>
(dir-Recurse-File | Select String-Pattern-List)。Path | Move Item-Destination

但是见鬼,因为我刚刚花了很多时间才意识到,Select字符串本身不能进行递归…

我怀疑这比阅读前10行更快:

(dir <SourcePath> -Recurse -File | Select-String -Pattern <SearchTerm> -List).Path | Move-Item -Destination <DestinationPath>
(dir-Recurse-File | Select String-Pattern-List)。Path | Move Item-Destination

但是见鬼,因为我刚刚花了很多时间才意识到选择字符串本身不能递归…

你考虑过/test
Select String
?@Olaf吗,问题是性能,而
Select String
通常是搜索文件内容的正确工具,您不能将其限制为仅搜索前N行。是的,这正是@mklement0正在编写的内容您是否考虑过/test
Select String
?@Olaf,问题在于性能,而
Select String
通常是搜索文件内容的正确工具,你不能限制它只搜索前N行。是的,这正是@mklement0所写的