歐萊娜 -
我有 300,000 多個包含子檔案夾和檔案的檔案夾。
我正在嘗試展平每個目錄,以便洗掉子檔案夾并將所有檔案帶到各自的父目錄。
不幸的是,Get-ChildItem cmdlet 在 .ps1 檔案的位置運行,而不是在 .txt 檔案中指定的位置。
我一直在嘗試解決這個問題幾個小時,任何幫助將不勝感激。
程序 -
首先,我運行一個 .ps1 檔案,該檔案從文本檔案中檢索父檔案夾位置并呼叫自定義模塊:
[System.IO.File]::ReadLines("C:\Users\ccugnet\Desktop\test.txt") | ForEach-Object {
Import-Module MyFunctions
fcopy -SourceDir $line -DestinationDir $line
Remove-Module MyFunctions
}
其次,自定義模塊將子專案移動到父檔案夾,在重復檔案的檔案名末尾附加一個遞增數字:
function fcopy ($SourceDir,$DestinationDir)
{
Get-ChildItem $SourceDir -Recurse | Where-Object { $_.PSIsContainer -eq $false } | ForEach-Object {
$SourceFile = $_.FullName
$DestinationFile = $DestinationDir $_
if (Test-Path $DestinationFile) {
$i = 0
while (Test-Path $DestinationFile) {
$i = 1
$DestinationFile = $DestinationDir $_.basename $i $_.extension
}
} else {
Move-Item -Path $SourceFile -Destination $DestinationFile -Verbose -Force -WhatIf
}
Move-Item -Path $SourceFile -Destination $DestinationFile -Verbose -Force -WhatIf
}
}
文本檔案內容:
"C:\Users\ccugnet\Desktop\Dir_Flatten\Fox, Hound & Hunter"
"C:\Users\ccugnet\Desktop\Dir_Flatten\Cat, Hat"
"C:\Users\ccugnet\Desktop\Dir_Flatten\Alice"
"C:\Users\ccugnet\Desktop\Dir_Flatten\Beetle | Juice"
uj5u.com熱心網友回復:
你的 $line 是空的
PS P:\> [System.IO.File]::ReadLines("C:\Users\ccugnet\Desktop\test.txt") | ForEach-Object {
$line
}
試試 $_
PS P:\> [System.IO.File]::ReadLines("C:\Users\ccugnet\Desktop\test.txt") | ForEach-Object {
$_
}
"C:\Users\ccugnet\Desktop\Dir_Flatten\Fox, Hound & Hunter"
"C:\Users\ccugnet\Desktop\Dir_Flatten\Cat, Hat"
"C:\Users\ccugnet\Desktop\Dir_Flatten\Alice"
"C:\Users\ccugnet\Desktop\Dir_Flatten\Beetle | Juice"
如果有用請采納我的回答
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444143.html
上一篇:從powershell生成pdf
