首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PowerShell脚本根据txt文件中的文件/文件夹结构移动全部位于一个文件夹中的文件

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

Stack Overflow用户
提问于 2021-11-26 10:44:54
回答 1查看 110关注 0票数 0

我想要达到的目标如下:

我在one“全局”文件夹中有几千个文件。示例:

代码语言:javascript
复制
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含量

代码语言:javascript
复制
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
*
*
*

下面是我认为需要执行的操作:

  1. 为txt中的每一行读取Structure.txt文件
  2. ,检查文件夹/子文件夹是否存在,在不存在
  3. 的情况下创建该文件,将文件从“全局”文件夹移动到在中引用的文件夹

我找到了一个类似的剧本,但在我的情况下有些东西不管用.

代码语言:javascript
复制
$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脚本实现这一点?

感谢你在这方面的帮助!提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-26 13:07:22

假设您的结构,txt文件可能包含不同的目的地路径,您可以在下面移动这些文件:

代码语言:javascript
复制
$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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70123174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档