我正在尝试根据文件名将文件复制到特定文件夹。
例如:
当前文件夹 - C:\Stuff\Old Files\
文件- 206.Little Rock.map.pdf
目标文件夹 - D:\Cleanup\206\Repository
所以基本上文件上的前导数字(206)是子文件夹的一部分。“\Repository”将保持不变。只有领先的数字会改变。
如果文件是 207.Little Rock.map.pdf 那么目标文件夹是
D:\清理\207\存储库
我从从这里获得的代码开始,但我不确定如何解释数字的变化以及如果文件夹不存在如何使其创建文件夹。所以 206\Repository 可能已经存在,但如果不存在,我需要脚本来创建文件夹。
$SourceFolder = "C:\Stuff\Old Files\"
$targetFolder = "D:\Cleanup\"
$numFiles = (Get-ChildItem -Path $SourceFolder -Filter *.pdf).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'
Get-ChildItem -Path $SourceFolder -Filter *.PDF | %{
[System.IO.FileInfo]$destination = (Join-Path -Path $targetFolder -ChildPath $Name.Repository(".*","\"))
if(!(Test-Path -Path $destination.Directory )){
New-item -Path $destination.Directory.FullName -ItemType Directory
}
[int]$percent = $i / $numFiles * 100
copy-item -Path $_.FullName -Destination $Destination.FullName
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;