【问题标题】:Move families of files into their own folders using a text file formatted for the purpose使用为此目的格式化的文本文件将文件族移动到自己的文件夹中
【发布时间】:2021-02-02 08:01:00
【问题描述】:

我有这样的文件名的档案

first_name.part1.rar
first_name.part1.rev
first_name.part2.rar
first_name.part2.rev
second_name.part01.rar
second_name.part01.rev
second_name.part02.rar
second_name.part02.rev

在同一个文件夹中,我有一个以这种方式格式化的 text.txt 文件

Citadel: Forged with Fire;first_name.part1.rar;link
Citadel: Forged with Fire;first_name.part1.rev;link
Citadel: Forged with Fire;first_name.part2.rar;link
Citadel: Forged with Fire;first_name.part2.rev;link

Eurotrack Simulator;second_name.part01.rar;link
Eurotrack Simulator;second_name.part01.rev;link
Eurotrack Simulator;second_name.part02.rar;link
Eurotrack Simulator;second_name.part02.rev;link

我尝试这样做:

1。使用文本文件中的第一个字段创建文件夹
2。将同族的文件移动到各自的文件夹中

例如,要从 txt 文本文件的每一行中提取年份并创建相关文件夹,我使用此代码

$movies = @()
(get-content C:\Path\text.txt) | foreach($_){
    $properties = @{
        date = $_.substring($_.IndexOf("(")+1,4)
        name = $_.substring(0,$_.IndexOf("("))
    }
    $movies += New-Object PSObject -Property $properties
}
$movies

foreach($movie in $movies){
    $movie.date
    $datePath = "C:\Path\$($movie.date)"
    if(-not(test-path $datePath)) {
        new-item $datePath -ItemType "directory"
    }
    $words = $movie.name -split '\s'
    $words
    #this is as far as I got
}

例如,如果我想在 powershell 脚本中实现一个正则表达式来生成文件夹并在相关文件夹中移动文件,我可以使用它

我使用类似的脚本来生成文件并将其移动到文件夹中。

$ToFolder = "$env:USERPROFILE\Desktop\to"
$FromFolder = "$env:USERPROFILE\Desktop\From"

#Create the sample folder on your desktop
#This line can be commented out if your ToFolder exists
New-Item $ToFolder -ItemType directory -Force

GCI -Path $FromFolder *.torrent | % {
    if ($_.Name -match "(19|20)\d{2}") {

        #Check to see if year folder already exists at the destination
        #If not then create a folder based on this year
        if (!(Test-Path "$ToFolder\$($Matches[0])")) {
            New-Item -Path "$ToFolder\$($Matches[0])" -ItemType directory
        }

        #Transfer the matching file to its new folder
        #Can be changed to Move-Item if happy with the results
        Copy-Item -Path $_.FullName -Destination "$ToFolder\$($Matches[0])" -Force
    }
}

我正在尝试拟合和链接这 2 个代码,但目的不同(如图所示,不是提取年份),但我不知道该怎么做。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您的输入文件text.txt 可以被视为以分号 (;) 分隔的 CSV 文件,因此我们可以在其上使用 Import-Csv cmdlet 来简化操作:

    $sourcePath = 'D:\Test'
    $inputFile  = Join-Path -Path $sourcePath -ChildPath 'text.txt'
    # because the title may contain invalid characters for a folder name like ':'
    # create a regex to remove those if applicable
    $invalid = "[{0}]" -f [RegEx]::Escape(([IO.Path]::GetInvalidFileNameChars() -join ''))
    
    Import-Csv -Path $inputFile -Delimiter ';' -Header Title,FileName,Link | Group-Object Title | ForEach-Object {
        # combine the Title with the source path. Remove invalid characters from the Title
        $targetFolder = Join-Path -Path $sourcePath -ChildPath ($_.Name -replace $invalid)
        # if the destination folder does not already exist, create it
        if (!(Test-Path -Path $targetFolder -PathType Container)) {
            $null = New-Item -Path $targetFolder -ItemType Directory
        }
        foreach ($fileName in $_.Group.FileName) {
            $targetFile = Join-Path -Path $sourcePath -ChildPath $fileName
            if (Test-Path -Path $targetFile -PathType Leaf) {
                Move-Item -Path $targetFile -Destination $targetFolder -Force
            }
            else {
                Write-Warning "File '$targetFile' not found!"
            }
        }
    }
    

    之前:

    D:\TEST
        first_name.part1.rar
        first_name.part1.rev
        first_name.part2.rar
        first_name.part2.rev
        second_name.part01.rar
        second_name.part01.rev
        second_name.part02.rar
        second_name.part02.rev
        text.txt
    

    之后:

    D:\TEST
    |   text.txt
    |
    +---Citadel Forged with Fire
    |       first_name.part1.rar
    |       first_name.part1.rev
    |       first_name.part2.rar
    |       first_name.part2.rev
    |
    \---Eurotrack Simulator
            second_name.part01.rar
            second_name.part01.rev
            second_name.part02.rar
            second_name.part02.rev
    

    【讨论】:

      猜你喜欢
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      相关资源
      最近更新 更多