从具有相同名称但不同扩展名的文件(*.pdf 和 *.rtf),如果 *.rtf 是较新的,则在控制台上输出消息

桑杰

我有包含两个不同扩展名的文件的文件夹和子文件夹。我需要检查两个相应文件的上次写入时间。
例如

file1.rtf
file1.pdf
file2.rtf
file2.pdf
file3.rtf
file3.pdf

... 等等

我需要确保 RTF 文件在 PDF 文件之前运行。如果 PDF 早于 RTF,则意味着这是一个错误。我需要在 powershell 控制台上输出这些文件名,并显示一条指示此错误的消息。任何帮助,将不胜感激。我试图找到并运行我在网上找到的不同代码,但没有任何效果。谢谢你。代码代码

$path = Read-Host "Enter Path"
$files = Get-ChildItem -Path $path -Filter "*.txt"            

$files | %{ 
    $rtf=$($_.BaseName+".rtf")
    If (Test-Path $rtf){
    If ($_.LastWritetime -lt (Get-Item $rtf).LastWriteTime){

    $file = $_.OpenText();            
    $lineNum = 1;            
    Write-Host "Checking file"$_.Name -f Yellow  "$_ is older than $rtf file"   ;            
    while($file.EndOfStream -ne $true)            
    {   
        $line = $file.ReadLine();                    
        if($line -ne $null)            
        {  
            if($line.ToLower().Contains("error") -or $line.ToLower().Contains("exception"))            
            {  
                Write-Host "Line: $lineNum " -NoNewline -ForegroundColor Green;            
                Write-Host $line -f Red;            
            }            
        }             

        $lineNum++;                   
    }            
}            
}
} 
用户6811411
Set-Location C:\Path\To\Files\
Get-ChildItem -Filter *.rtf |ForEach-Object {
    $pdf=$($_.BaseName+".pdf")
    If (Test-Path $pdf){
        If ($_.LastWritetime -gt (Get-Item $pdf).LastWriteTime){
            Write-Warning "$_ is newer than $pdf file"
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档