【发布时间】:2017-06-14 14:11:00
【问题描述】:
我编写了以下脚本来根据杂志标题(第一个连字符之前的所有内容)将文件批量处理到文件夹中:
magazine title - year-month.pdf 例如National Geographic - 2017-07.pdf
运行脚本后,杂志从父文件夹移动到新的子文件夹,在本例中为“国家地理杂志”。
三个相关问题:
即使没有“孤儿”,也会创建“_Orphans”文件夹(第 38 行) 归档到它以供以后手动处理。如何制作文件夹 创建条件?
重复文件在处理过程中会产生错误消息。脚本继续运行没什么大不了的,但我想以与处理“孤儿”相同的方式处理重复项,使用新的“_Duplicates”文件夹/移动。
如何注释多行而每行开头不带 # 行(例如,在脚本的顶部)。一定有更优雅的 处理 cmets/文档的方法...?
额外问题:
如果您真的厌倦了等待多 TB 文件副本 您正在像沙漏一样观察进度,任何人都可以提供代码帮助 对于第 10 行所示的分隔符数组(可能是错误的术语/名称)? ID 希望能够指定的不仅仅是我在我的 正则表达式匹配(第 26 行,我花了一天的大部分时间才开始工作)。
$OrigFolder = ".\"
$NewFolder = ".\_Sorted to Move"
# Orphans folder, where files that return null in the regex match will be moved
# Example: file "- title.pdf"
# will be moved to ".\_Orphans" folder
$Orphans = '_Orphans' # Use the underscore to sort the folder to the top of the window
#### How to use an array of values for the delimiters in the regex instead of literals
#### My proposed code, but I am missing how to use the delims in the regex match
#### $delims = "\s-\s" ",\s"\s and\s"
# First count the number of files in the $OrigFolder directory
$numFiles = (Get-ChildItem -Path $OrigFolder).Count
$i=0
# Tell the user what will happen
clear-host;
Write-Host 'This script will copy ' $numFiles ' files from ' $OrigFolder ' to _Sorted to Move'
# Ask user to confirm the copy operation
Read-host -prompt 'Press enter to start copying the files'
# Regex to match filenames
$Regex = [regex]"(?:(.*?)\s-)|(?:(.*?),\s)|(?:(.*?)\sand\s)"
# Loop through the $OrigFolder directory, skipping folders
Get-ChildItem -LiteralPath $OrigFolder | Where-Object {!$_.PsIsContainer} |
ForEach-Object {
if($_.BaseName -match $Regex){
$ChildPath = $_.BaseName -replace $Regex
#Caluclate copy operation progress as a percentage
[int]$percent = $i / $numFiles * 100
# If first part of the file name is empty, move it to the '_Orphans' folder
if(!$Matches[1]){
$ChildPath = $Orphans}
else {
$ChildPath = $Matches[1]
}
# Generate new folder name
$FolderName = Join-Path -Path $NewFolder -ChildPath ($ChildPath + ' Magazine')
# Create folder if it doesn't exist
if(!(Test-Path -LiteralPath $FolderName -PathType Container)){
$null = New-Item -Path $FolderName -ItemType Directory}
# Log progress to the screen
Write-Host "$($_.FullName) -> $FolderName"
# Move the file to the folder
Move-Item -LiteralPath $_.FullName -Destination $FolderName
# Tell the user how much has been moved
Write-Progress -Activity "Copying ... ($percent %)" -status $_ -PercentComplete $percent -verbose
$i++
}
}
Write-Host 'Total number of files in '$OrigFolder ' is ' $numFiles
Write-Host 'Total number of files copied to '$NewFolder ' is ' $i
Read-host -prompt "Press enter to complete..."
clear-host;
【问题讨论】:
-
多行cmets可以用
<##>这样包裹<# your multi line comment here #>
标签: powershell