将结果输出到文本文件

时间:2019-09-18 08:27:25

标签: powershell

感谢@Theo昨天,我的脚本可以正常工作,并且可以完成我想要的所有事情。所以现在有了一个可爱的菜单系统,选项等。

但是我已经决定-就像我想做的一点魅力一样!

因此,正在发生的部分事情是查找文件并重命名它们。西奥的例子是最好的:

D:\The Old Man and his dog.mp4   → D:\Old Man and his dog (The).mp4
D:\A Nightmare on Elm Street.mp4 → D:\Nightmare on Elm Street (A).mp4
D:\I, Robot.mkv                  → D:\Robot (I).mkv

我想要做的是从输出更改为筛选结果。对于所做的更改,最好在每次脚本运行时更新文件并添加或制作新的日志文件。万一我需要查找某些东西或检查发生了什么,以防万一-我有一些值得参考的东西!

下面是我使用过但不满意显示结果的代码。

Start-Sleep -s 5
cls
Write-Output "

好吧,$filter$rename现在已被搜索和更改。

总共

(Get-ChildItem | ? {$_.CreationTime -ge (Get-Date).Addminutes(-10)}).Count
Write-Output "files being changed.


"
pause
cls}

我想获得更好的显示效果,即

  

xx文件已成功更新
  xx个文件失败。
  请查看日志以获取详细信息。

然后查看日志文件,看到类似以下内容的

Log1.txt

  

D:\ The Old Man and his dog.mp4成功更改为D:\ Old Man and his dog(The).mp4
  总共1/1个文件已成功更改

我正在考虑Compare-ObjectOut-File,所以也许

Compare-Object $(Get-Content c:\user\documents\List1.txt) $(Get-Content c:\user\documents\List2.txt) > c:\user\documents\diff_output.txt

但是由于某些原因,我无法理解这个想法并弄清楚从哪里开始。

1 个答案:

答案 0 :(得分:0)

如果我正确理解了需求,并扩展了先前的代码,则可以执行以下操作:

$rootFolder = 'D:\'                                                   #'# the root folder where the files and subfolders are
$logFile    = 'D:\renamed_{0:yyyy-MM-dd HH_mm_ss}.txt' -f (Get-Date)  #'# the file to log to, including the current date

# create an array of words that start the file name and should be moved towards the back
$words = 'The', 'i', 'a'

# create a regex pattern where all possible words are separated by the regex OR (|) sign
$re = '^({0})[ ,]+' -f ($words -join '|')

# create two integer counter variables
[int]$success, [int]$failed = 0
Get-ChildItem -Path $rootFolder | Where-Object { $_.Name -match $re } | ForEach-Object {
    $newName = '{0} ({1}){2}' -f ($_.BaseName -replace $re), $matches[1], $_.Extension
    try {
        $_ | Rename-Item -NewName $newName -ErrorAction Stop -WhatIf
        Add-Content -Path $logFile -Value "Successfully renamed '$($_.FullName)' to '$newName'"
        $success++
    }
    catch {
        Add-Content -Path $logFile -Value "Rename failed for file '$($_.FullName)'"
        $failed++
    }

}
# write summary
$total   = $success + $failed
$summary = "Total of $success / $total files successfully changed`r`nTotal of $failed / $total files failed"
# output summary to the log file
Add-Content -Path $logFile -Value "`r`n==========================================================="
Add-Content -Path $logFile -Value $summary

# output summary on screen
Write-Host "`r`n$summary`r`nPlease see '$logFile' for details." -ForegroundColor Yellow

使用相同的三个示例将在屏幕上输出:

Total of 3 / 3 files successfully changed
Total of 0 / 3 files failed
Please see 'D:\renamed_2019-09-18 12_07_01.txt' for details.

,日志文件将包含:

Successfully renamed 'D:\A Nightmare on Elm Street.mp4' to 'Nightmare on Elm Street (A).mp4'
Successfully renamed 'D:\I, Robot.mkv' to 'Robot (I).mkv'
Successfully renamed 'D:\The Old Man and his dog.mp4' to 'Old Man and his dog (The).mp4'

===========================================================
Total of 3 / 3 files successfully changed
Total of 0 / 3 files failed