[英]Move files that are all in one folder based on file/folder structure in txt file using PowerShell script
What I'm trying to achieve is following:我想要达到的目标如下:
I have a few thousand files in one "Global" folder .我在一个“全局”文件夹中有几千个文件。 Example:例子:
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
*
*
*
And I need to move them to folders/subfolders (that don't exist yet) according to Structure.txt file.我需要根据Structure.txt文件将它们移动到文件夹/子文件夹(尚不存在)。
Content of 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
*
*
*
Below are the actions that I suppose I need to perform:以下是我想我需要执行的操作:
I have found a similar script but something is not working in my case...我找到了一个类似的脚本,但在我的情况下有些东西不起作用......
$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
}
How can I achieve this using powershell script?如何使用 powershell 脚本实现此目的?
Appreciate help on this.感谢您对此的帮助。 Thanks in advance.提前致谢。
Assuming your structure,txt file may contain different paths for the destinations, you could do below to move the files:假设您的结构,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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.