[英]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.