简体   繁体   English

使用 PowerShell 脚本根据 txt 文件中的文件/文件夹结构移动所有位于一个文件夹中的文件

[英]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:以下是我想我需要执行的操作:

  1. Read the Structure.txt file阅读 Structure.txt 文件
  2. For every line in txt check if folders/subfolders exist, create when it doesn't对于 txt 中的每一行,检查文件夹/子文件夹是否存在,不存在时创建
  3. Move file from "Global" folder to folder referenced in "Structure.txt"将文件从“Global”文件夹移动到“Structure.txt”中引用的文件夹

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.

相关问题 读取文件,然后根据文件内容名称,使用 Powershell 脚本将文件从一个文件夹移动到另一个文件夹 - Read a file and then based on file content names, move the file from one folder to another using Powershell script 如何使用Powershell根据文件名将文件移动到“文件夹和子文件夹”? - How to move files to Folder and Sub folder based on file name using Powershell? 如何使用 PowerShell 搜索特定文件名然后将该文件之后的所有文件移动到另一个文件夹 - How to search for a specific file name then move all files after that file to another folder using PowerShell 如何使用 powershell 将一个 window 文件夹中的所有文件重命名为另一个文件夹中每个文件的名称? - how to rename all files in one window folder to names of each file in another folder using powershell? 根据文件内容重命名文件夹中的所有txt文件 - Rename all txt files in a folder based on file content 如何使用Powershell获取文件夹中所有文件夹名称而不是子文件夹中文件/子文件夹的.txt文件? - How to get a .txt file of all folders names inside a folder but not the files/subfolders inside the child folders using powershell? 使用Powershell根据文件名将文档移动到文件夹 - Using Powershell to move documents to a folder based on file name 将所有文件移动到一个文件夹中的脚本 - script to move all files into one folder 读取文件,然后使用 Powershell 脚本将内容从一个文件夹移动到另一个文件夹 - Read a file and then move the content from one folder to another using Powershell script 下面是文件夹和文件结构,在其中移动了所有文件,并保持其子文件夹结构(不包括几个文件和子文件夹) - Below is the folder and file structure, in which I have move all the files with keeping its sub folder structure excluding few files and sub folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM