我想要达到的目标如下:
我在one“全局”文件夹中有几千个文件。示例:
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
*
*
*下面是我认为需要执行的操作:
我找到了一个类似的剧本,但在我的情况下有些东西不管用.
$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脚本实现这一点?
感谢你在这方面的帮助!提前谢谢。
发布于 2021-11-26 13:07:22
假设您的结构,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://stackoverflow.com/questions/70123174
复制相似问题