我想要實作的是以下內容:
我在一個“全域”檔案夾中有幾千個檔案。例子:
PS D:\Backup\Global> dir
Directory: D:\Backup\Global
Mode LastWriteTime Length Name
---- ------------- ------ ----
------ 19.8.2016. 08:25 282208 CBR.docx
------ 17.9.2018. 13:37 254803 CMZ.docx
------ 6.6.2017. 07:46 191928 Ginekologija.docx
------ 6.12.2019. 08:16 192412 HES.docx
------ 19.2.2021. 11:56 192925 Hitna medicinska pomo?.docx
*
*
*
我需要根據Structure.txt檔案將它們移動到檔案夾/子檔案夾(尚不存在)。
Structure.txt 的內容:
D:\Backup\Structured\1 Word files\1 Планови 2018\Prijedlog godi?njeg plana obuke\CBR.docx
D:\Backup\Structured\1 Word files\1 Планови 2018\Prijedlog godi?njeg plana obuke\CMZ.docx
D:\Backup\Structured\1 Word files\1 Планови 2018\Prijedlog godi?njeg plana obuke\Ginekologija.docx
D:\Backup\Structured\1 Word files\1 Планови 2018\Prijedlog godi?njeg plana obuke\HES.docx
D:\Backup\Structured\1 Word files\1 Планови 2018\Prijedlog godi?njeg plana obuke\Hitna medicinska pomo?.docx
*
*
*
以下是我認為我需要執行的操作:
- 閱讀 Structure.txt 檔案
- 對于 txt 中的每一行,檢查檔案夾/子檔案夾是否存在,不存在時創建
- 將檔案從“全域”檔案夾移動到“Structure.txt”中參考的檔案夾
我找到了一個類似的腳本,但在我的情況下有些東西不起作用......
$des = "$ENV:D:\Backup\Structured"
$safe = Get-Content "$ENV:D:\Backup\Global\Structure.txt"
$safe | ForEach-Object {
#find drive-delimeter
$first = $_.IndexOf(":\");
if ($first -eq 1) {
#stripe it
$newdes = Join-Path -Path $des -ChildPath @($_.Substring(0, 1) $_.Substring(2))[0]
}
else {
$newdes = Join-Path -Path $des -ChildPath $_
}
$folder = Split-Path -Path $newdes -Parent
$err = 0
#check if folder exists"
$void = Get-Item $folder -ErrorVariable err -ErrorAction SilentlyContinue
if ($err.Count -ne 0) {
#create when it doesn't
$void = New-Item -Path $folder -ItemType Directory -Force -Verbose
}
$void = Move-Item -Path $_ -destination $newdes -Force
}
如何使用 powershell 腳本實作此目的?
感謝這方面的幫助!提前致謝。
uj5u.com熱心網友回復:
假設您的結構,txt 檔案可能包含目標的不同路徑,您可以執行以下操作來移動檔案:
$sourceFolder = 'D:\Backup\Global'
# read the structure.txt file and loop through the entries
Get-Content -Path 'D:\Test\structure.txt' -Encoding utf8 | ForEach-Object {
# create the full file path and name to be found in the source folder
$file = Join-Path -Path $sourceFolder -ChildPath ([System.IO.Path]::GetFileName($_))
if (Test-Path -Path $file -PathType Leaf) {
# file found; split the path from the filename as found in structure.txt
$targetFolder = [System.IO.Path]::GetDirectoryName($_)
# create the path if it did not already exist
$null = New-Item -Path $targetFolder -ItemType Directory -Force
# move the file
Move-Item -Path $file -Destination $targetFolder
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/368907.html
標籤:电源外壳
