如何重命名檔案夾中的多個檔案以包括:
- MM_YYYY
- XX中的檔案1
- 檔案中的行數
例子 :
當前檔案名:
- SRA_0400041325.TXT
- SRA_0400027561.TXT
- SRA_0400013497.TXT
后 :
- SRA_0400041325_03_2022_1-3_720.TXT
- SRA_0400027561_03_2022_2-3_433.TXT
- SRA_0400013497_03_2022_3-3_1720.TXT
如何在 PowerShell 中做到這一點?
謝謝。
編輯 1:
我試過這個:
$Month = (Get-Date).ToString("MM")
$Year = (Get-Date).ToString("yyyy")
$MonthYear = $Month "-" $Year
$NumberOfFiles = (Get-ChildItem | Measure-Object).Count
$NumberOfLines = Get-Content SRA_0400041325.TXT | Measure-Object -line | Select-Object -ExpandProperty Lines
$PartToAdd = $MonthYear "_" $NumberOfFiles "_" $NumberOfLines
Write-Host $PartToAdd
但我不知道如何包含 ForEach
uj5u.com熱心網友回復:
正如您所發現的,您可以使用Get-ChildItem來發現檔案并Get-Content列舉內容并計算行數。
現在您只需要一個變數來跟蹤您當前正在處理的數字檔案:
# Prepare the date parts
$Month = (Get-Date).ToString("MM")
$Year = (Get-Date).ToString("yyyy")
# Discover the files
$Files = @(Get-ChildItem -File)
$NumberOfFiles = $Files.Count
# We'll use this to keep track of which number file we're operating on
$FileCounter = 1
foreach($file in $files){
$NumberOfLines = ($file |Get-Content |Measure-Object -Line).Lines
# Construct the file-count part
$FileCountString = $FileCounter,$NumberOfFiles -join '-'
# Remember to update the file counter
$FileCounter
# Construct the new file name
$newName = $file.BaseName,$Month,$Year,$FileCountString,$NumberOfLines -join '_'
# Rename the file
$file |Rename-Item -NewName $newName
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437597.html
標籤:电源外壳
上一篇:將CSV資料轉換為日期時間格式
