Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/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
Powershell 如果列中的文本与特定路径或文件名匹配,则删除整行_Powershell - Fatal编程技术网

Powershell 如果列中的文本与特定路径或文件名匹配,则删除整行

Powershell 如果列中的文本与特定路径或文件名匹配,则删除整行,powershell,Powershell,我是Powershell的新手,如果可以的话,请尝试解释一下。我正在尝试将目录的内容与CSV中的一些其他信息一起导出 CSV文件包含有关文件的信息,但是,我只需要匹配文件名列(其中包含完整路径)。如果匹配,我需要删除整行 $folder1 = OldFiles $folder2 = Log Files\January $file1 = _updatehistory.txt $file2 = websites.config 在CSV文件中,如果其中任何一个匹配,则必须删除整行。CSV文件以以下方

我是Powershell的新手,如果可以的话,请尝试解释一下。我正在尝试将目录的内容与CSV中的一些其他信息一起导出

CSV文件包含有关文件的信息,但是,我只需要匹配文件名列(其中包含完整路径)。如果匹配,我需要删除整行

$folder1 = OldFiles
$folder2 = Log Files\January
$file1 = _updatehistory.txt
$file2 = websites.config
在CSV文件中,如果其中任何一个匹配,则必须删除整行。CSV文件以以下方式包含文件名:

**FileName**
C:\Installation\New Applications\Root
我试过这样做:

Import-csv -Path "C:\CSV\Recursion.csv" | Where-Object { $_.FileName -ne $folder2} | Export-csv -Path "C:\CSV\RecursionUpdated.csv" -NoTypeInformation

但它不起作用。我非常感谢您的帮助。

看起来您只想匹配完整路径的一部分,所以您应该使用运算符(或其求反的变体),它们可以进行非精确匹配:

$excludes = '*\OldFiles', '*\Log Files\January', '*\_updatehistory.txt', '*\websites.config'

Import-csv -Path "C:\CSV\Recursion.csv" | 
    Where-Object { 

        # $matchesExclude Will be $true if at least one exclude pattern matches
        # against FileName. Otherwise it will be $null.
        $matchesExclude = foreach( $exclude in $excludes ) {
            # Output $true if pattern matches, which will be captured in $matchesExclude.
            if( $_.FileName -like $exclude ) { $true; break }
        }

        # This outputs $true if the filename is not excluded, thus Where-Object
        # passes the row along the pipeline.
        -not $matchesExclude  

    } | Export-csv -Path "C:\CSV\RecursionUpdated.csv" -NoTypeInformation

此代码大量使用PowerShell的隐式输出行为。例如。
foreach
循环体中的文本
$true
是隐式输出,将在
$matchesExclude
中自动捕获。如果不是分配
$matchesExclude=foreach…
,则该值将被写入控制台(如果不是在调用堆栈中的其他位置捕获的话)。

文件名是否完全匹配?如果它只是其中的一部分,请使用:$\文件名-notlike“$folder2*”@guiwhats确保文件名完全匹配。包含完整路径“部分”的变量将从其他位置加载(实际上是JSON文件)。因此,我无法输入要精确匹配的文件名,因为我需要首先加载变量。是否有一种方法可以像您使用
$excludes
那样存储这些变量?@Scripter9700应该是可能的。如何做到这一点,取决于JSON的结构。您可以将此问题作为第二个问题发布。目前的问题已经得到了回答。