Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
Search PowerShell搜索和替换_Search_Powershell - Fatal编程技术网

Search PowerShell搜索和替换

Search PowerShell搜索和替换,search,powershell,Search,Powershell,我在一个没有扩展名的.txt文件中有大约500个文件名。我有另一个.txt文件,其完整文件名和扩展名总计超过1000 我需要遍历较小的.txt文件,并在较大的.txt文件中搜索正在读取的当前行。如果找到了,则将名称复制到一个新文件中,found.txt,如果没有找到,则转到较小的.txt文件中的下一行 我不熟悉脚本编写,不知道从这里开始 Get-childitem -path "C:\Users\U0146121\Desktop\Example" -recurse -name | out-fil

我在一个没有扩展名的.txt文件中有大约500个文件名。我有另一个.txt文件,其完整文件名和扩展名总计超过1000

我需要遍历较小的.txt文件,并在较大的.txt文件中搜索正在读取的当前行。如果找到了,则将名称复制到一个新文件中,
found.txt
,如果没有找到,则转到较小的.txt文件中的下一行

我不熟悉脚本编写,不知道从这里开始

Get-childitem -path "C:\Users\U0146121\Desktop\Example" -recurse -name | out-file C:\Users\U0146121\Desktop\Output.txt  #send filenames to text file
(Get-Content C:\Users\U0146121\Desktop\Output.txt) |
ForEach-Object {$_  1

您的示例显示通过递归桌面上的文件夹来创建文本文件。你不需要一个文本文件来循环;您可以直接使用它,但假设您确实生成了一个短名称的文本文件,就像您所说的那样

$short_file_names = Get-Content C:\Path\To\500_Short_File_Names_No_Extensions.txt
现在,您可以通过两种方式循环该阵列:

使用
foreach
关键字:

foreach ($file_name in $short_file_names) {
    # ...
}
或者使用
ForEach对象
cmdlet:

$short_file_names | ForEach-Object {
    # ...
}
最大的区别在于,当前项在第一个中是命名变量
$file\u name
,在第二个中是非命名内置
$\u
变量

假设您使用第一个。您需要查看第二个文件中是否有
$file\u name
,如果是,请记录您找到的文件。可以这样做。我在代码中添加了注释,解释了每个部分

# Read the 1000 names into an array variable
$full_file_names = Get-Content C:\Path\To\1000_Full_File_Names.txt

# Loop through the short file names and test each
foreach ($file_name in $short_file_names) {

    # Use the -match operator to check if the array contains the string
    # The -contains operator won't work since its a partial string match due to the extension
    # Need to escape the file name since the -match operator uses regular expressions

    if ($full_file_names -match [regex]::Escape($file_name)) {

        # Record the discovered item
        $file_name | Out-File C:\Path\To\Found.txt -Encoding ASCII -Append
    }
}

您能添加一些示例输入和您想要的输出吗?这将有助于我们更好地理解问题。请后退一步,描述您试图解决的实际问题,而不是您认为的解决方案。你想通过这样做实现什么?