【发布时间】: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