简体   繁体   English

如何使用Powershell根据文件名将文件移动到“文件夹和子文件夹”?

[英]How to move files to Folder and Sub folder based on file name using Powershell?

Powershell beginner here. Powershell初学者在这里。 I tried coding this using Powershell, but without getting anywhere. 我尝试使用Powershell对此进行编码,但没有成功。

I have a list of Wav files that I need to move automatically into a folder based on the file name. 我有一个Wav文件列表,我需要根据文件名自动将其移动到文件夹中。

20190822091227_202123545.wav

20190822080957_202123545.wav

The file name determines the folder structure - Example 文件名决定文件夹结构-示例

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

So the folder structure will be 因此文件夹结构将是

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

and the wav files will move to that folder - In this case its 22 wav文件将移动到该文件夹​​-在这种情况下,其22

I have tried this code that I found here on Stackoverflow and added my own, but its giving me errors. 我已经尝试过在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;

The files are not moving to the folder and I am getting error in the file names 文件未移至文件夹,文件名出现错误

Example

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

$files is named appropriately plural as a variable that could contain many files. $files被适当地命名为可以包含许多文件的变量。 But when you go to retrieve the date parts, you use call it once, on the whole collection of file s , instead of on each individual $file (which is the variable you have inside the loop which iterates over the collection of files). 但是,当您去检索日期部分时,可以在整个文件s上使用一次,而不是在每个单独的$file上调用一次(这是您在循环内循环访问文件集合的变量)。

So you want to set the date variables inside the loop, on every iteration, so that it's related to the current 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
}

Also the pattern you laid out in your question doesn't seem to be what you're setting, so I'm a little confused how your output looks like it does. 同样,您在问题中列出的模式似乎也不是您要设置的模式,因此我有点困惑您的输出看起来如何。

What you have here: 您在这里拥有什么:

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

Should produce: C:\\Archive\\201908\\22 even though you want C:\\Archive\\2019\\201908\\22 . 即使您想要C:\\Archive\\2019\\201908\\22应生成: C:\\Archive\\201908\\22 C:\\Archive\\2019\\201908\\22

To produce what you wanted the line should be like this: 要产生您想要的内容,该行应如下所示:

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

By the way you can use variables directly in double-quoted strings: 顺便说一下,您可以直接在双引号字符串中使用变量:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Powershell根据文件名将文档移动到文件夹 - Using Powershell to move documents to a folder based on file name 如何使用 PowerShell 搜索特定文件名然后将该文件之后的所有文件移动到另一个文件夹 - How to search for a specific file name then move all files after that file to another folder using PowerShell 使用 PowerShell 脚本根据 txt 文件中的文件/文件夹结构移动所有位于一个文件夹中的文件 - Move files that are all in one folder based on file/folder structure in txt file using PowerShell script 如何根据名称和基名在文件夹之间移动文件 - How to move files from folder to folder based on name and Basename Powershell:提取文件名的一部分以创建文件夹并将文件移动到文件夹中 - Powershell: Extracting parts of file name to create folders and move files into the folder 如何将文件移动到文件夹中,然后在Powershell中将文件夹压缩 - How to move files into a folder and then zip the folder in powershell Powershell:根据创建日期将文件移动到文件夹 - Powershell: Move files to folder based on Date Created PowerShell脚本将日期作为名称的文件移动到文件夹 - PowerShell script to move files with date as name in it to a folder 使用powershell中的regex基于文件名命名文件夹 - Name folder based on file name using regex in powershell 如何基于Powershell中的用户输入将文件移动到其他文件夹 - How to move a file to a different folder based on user input in powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM