Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/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,我需要知道如何显示完全通过替换的文件列表,在它显示文件夹和子文件夹中的所有文件时,此输出不提供信息 param ( [Parameter(Mandatory = $true)][string] $environment ) Get-ChildItem -Recurse -include "*.*" -Exclude "*.ps1", "*.yml" | foreach-object { $Item = $_.fulln

我需要知道如何显示完全通过替换的文件列表,在它显示文件夹和子文件夹中的所有文件时,此输出不提供信息

param (
    [Parameter(Mandatory = $true)][string] $environment
)

Get-ChildItem -Recurse -include "*.*" -Exclude "*.ps1", "*.yml" | foreach-object {
    $Item = $_.fullname; ( get-content $Item ) | foreach-object {
        $_ -replace "{env}", "$environment" 
    }  
    | set-content $Item
}
#-gt
Get-ChildItem -Recurse -include "*.*" -Exclude "*.ps1", "*.yml" | Where-Object { $_.LastAccessTime -gt (get-date).AddSeconds(-15) -and $_.LastWriteTime -gt (get-date).AddSeconds(-15) } | Select-Object LastWriteTime , Name
答复:

LastWriteTime       Name
-------------       ----
01.06.2021 12:16:46 test-number-1F.txt
01.06.2021 12:16:46 test-number-2F.txt
01.06.2021 12:16:46 test-number-3F.txt
01.06.2021 12:16:46 test-number-4F.txt
01.06.2021 12:16:46 text.json
但是,例如text.json文件总是空的,它不应该出现在输出中

另外,我正在尝试使用LastWriteTime,因为我不知道如何实现这一点

没有“简单”的方法来查看文件是否被修改过,因此我们需要找到某种“解决方法”。 顺便说一句,由于您正在设置每个文件的内容,而不管是否执行替换操作-
LastWriteTime
将始终被修改

我的文件中的
-include
-exclude
开关有一些问题,所以我重构了您的代码

我创建了两个列表。第一个由已更改的文件组成,第二个用于存储文件的内容(无论行是否已更改)。为什么是列表而不是数组?添加到数组会删除“旧”数组,并创建一个包含新元素的新数组。对于大文件,这可能是一个性能问题

param (
    [Parameter(Mandatory)]
    [string] $environment
)
$modifiedFilesList = New-Object System.Collections.Generic.List[string];
$files = Get-ChildItem -File | Where-Object {$_.Name -notlike "*.yml" -and $_.Name -notlike "*.ps1"};

foreach ($file in $files.FullName) {
    #create a new list to save content
    $newContent = New-Object System.Collections.Generic.List[string];

    foreach ($line in (Get-Content $file)) {
        #save line for comparison
        $original = $line; 
        $modified = $line -replace "{env}", "$environment";
        if ($original -ne $modified) {
            #line has changed (skip the case sensitive search, cause we are changing a token here. Case sensitive switch is '-cne')
            $line = $modified;
            $modifiedFilesList.Add($file); #save the file as modified
        }
        $newContent.Add($line); #add content to list that will be saved to file
    }
    $newContent | Set-Content $file; #set content of file
}

if ($modifiedFilesList.Count -gt 0) {
    Write-Output "Files changed: `n$([string]::Join("`n", $modifiedFilesList))";
}
else {
    Write-Output "No files changed";
}