您的示例显示您通过递归通过文件夹桌面创建文本文件,您不需要文本文件循环,您可以使用该文件,但可以说,您生成短名称的文本文件像你说的那样。
$short_file_names = Get-Content C:\Path\To\500_Short_File_Names_No_Extensions.txt
现在,你可以通过该数组有两种方式循环:
使用foreach
关键字:
foreach ($file_name in $short_file_names) {
# ...
}
或者使用ForEach-Object
的cmdlet:
$short_file_names | ForEach-Object {
# ...
}
最大的区别是当前项目将是一个命名变量$file_name
中的第一个和第二个中的非命名内置变量$_
。
假设你使用第一个。您需要查看$file_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
}
}
你可以添加一些示例输入和你想要的输出?这将有助于我们更好地理解这个问题。 – Eris
请退后一步,描述您尝试解决的实际问题,而不是您认为的解决方案。你想通过这样做来达到什么目的? –