【问题标题】:How to move files to Folder and Sub folder based on file name using Powershell?如何使用 Powershell 根据文件名将文件移动到文件夹和子文件夹?
【发布时间】:2019-12-28 07:58:56
【问题描述】:

Powershell 初学者在这里。我尝试使用 Powershell 对此进行编码,但没有得到任何结果。

我有一个Wav 文件列表,我需要根据文件名将这些文件自动移动到文件夹中。

20190822091227_202123545.wav

20190822080957_202123545.wav

文件名决定文件夹结构——示例

2019(This is the Year)08(This is the Month)22(This is the Day)_202123545.wav

所以文件夹结构将是

C:\Archive\2019(Year)\201908(YearMonth)\22(Day)
C:\Archive\2019\201908\22 

wav 文件将移至该文件夹 - 在本例中为 22

我已经尝试过我在 Stackoverflow 上找到的代码并添加了我自己的代码,但它给了我错误。

$SourceFolder = Set-Location "C:\CR\Files"
$targetFolder = "C:\Archive\CR_Dev\Test_Folder"
$numFiles = (Get-ChildItem -Path $SourceFolder -Filter *.WAV).Count
$i=0

clear-host;
Write-Host 'This script will copy ' $numFiles ' files from ' $SourceFolder ' to ' $targetFolder
Read-host -prompt 'Press enter to start copying the files'

$files = Get-ChildItem $SourceFolder | where {$_.extension -in ".wav"} | select -expand basename

 # Out FileName, year and month
    $Year = $files.Substring(0,4)
    $Month = $files.Substring(4,2)
    $Day = $files.Substring(6,2)

foreach ($file in $files)

{

    # Set Directory Path
    $Directory = $targetFolder + "\" + $Year+$Month + "\" + $Day

    # Create directory if it doesn't exsist
    if (!(Test-Path $Directory))
    {
    New-Item $Directory -type Directory
    }

    [int]$percent = $i / $numFiles * 100

    # Move File to new location
    $file | Copy-Item -Destination $Directory

    Write-Progress -Activity "Copying ... ($percent %)" -status $_  -PercentComplete $percent -verbose
    $i++
}

Write-Host 'Total number of files read from directory '$SourceFolder ' is ' $numFiles
Write-Host 'Total number of files that was copied to '$targetFolder ' is ' $i
Read-host -prompt "Press enter to complete..."
clear-host;

文件没有移动到文件夹中,文件名出现错误

例子

C:\Archive\2019 2019\2019 08 2019 08\22 22

【问题讨论】:

    标签: powershell directory subdirectory


    【解决方案1】:

    $files 被适当地命名为复数,作为可以包含许多文件的变量。 但是当你去检索日期部分时,你使用调用它一次,在整个文件集合s上,而不是在每个单独的$file(这是你在循环中的变量遍历文件集合)。

    因此,您希望在每次迭代时在循环内设置日期变量,使其与当前文件相关。

    $files = Get-ChildItem $SourceFolder | where {$_.extension -in ".wav"} | select -expand basename
    
    foreach ($file in $files)
    {
        $Year = $file.Substring(0,4)
        $Month = $file.Substring(4,2)
        $Day = $file.Substring(6,2)
    
       $Directory = $targetFolder + "\" + $Year+$Month + "\" + $Day
    
    # etc
    }
    

    此外,您在问题中列出的模式似乎不是您设置的,所以我有点困惑您的输出看起来如何。

    你有什么:

    $Directory = $targetFolder + "\" + $Year+$Month + "\" + $Day
    

    应该产生:C:\Archive\201908\22,即使你想要C:\Archive\2019\201908\22

    要产生你想要的线应该是这样的:

    $Directory = $targetFolder + "\" + $Year + "\" + $Year+$Month + "\" + $Day
    

    顺便说一句,你可以在双引号字符串中直接使用变量:

    $Directory = "$targetFolder\$Year\$Year$Month\$Day"
    

    【讨论】:

    • 对于$directory变量,可以使用join-path。
    • 谢谢布赖恩蒂斯特!谢谢!我可以看到 $file 出了什么问题。我也更新了 $Directory 代码。再次感谢您指出这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多