我的脚本在遇到长路径名(&gt; = 260个字符)时退出。如何在我的tif到pdf转换中更改我的代码片段以捕获长路径名称。或者更简单/更好的方法是简单地将所有文件转换为<260个字符并单独应用脚本(我仍然不知道如何处理剩余的长名称文件)?
SNIPPET:(只要路径名为&lt; 260个字符,这就有效)
$tool = 'C:\Program Files\ImageMagick-7.0.1-Q16\magick.exe'
$source = "\\my\path"
Get-ChildItem -Path $source -filter longnames*.tif | %{ & $tool convert "$($_.FullName -Replace ".tif+$", ".tif[0]")" "C:\Data\$($_.Name -Replace ".tif+$", ".pdf")" }
从阅读this博客+帖子开始,我似乎需要使用语法
[Alphaleonis.Win32.Filesystem.File]::<some AlphaFS function here?>
但我需要一些与我的申请相关的进一步指导或示例。
我在使用Powershell v 4的Windows Server 2012 R上,我已经安装了AlphaFS库。我正在使用imagemagick将PDF的第一页转换为TIF。
答案 0 :(得分:1)
这里使用了robocopy的功能 https://github.com/gangstanthony/PowerShell/blob/master/Get-Files.ps1
$tool = 'C:\Program Files\ImageMagick-7.0.1-Q16\magick.exe'
$source = "\\my\path"
# this will load the Get-Files function which uses robocopy
# feel free to check it out before running the script
try{ iex (iwr https://raw.githubusercontent.com/gangstanthony/PowerShell/master/Get-Files.ps1).rawcontent -ea 0 }catch{}
$files = (Get-Files -Path $source -Include longnames*.tif).fullname
# get available drives in case dealing with long file names
# we can map a drive to a long file path so it is short enough for powershell to handle
$drives = [io.driveinfo]::getdrives() | % {$_.name[0]}
$alpha = [char[]](65..90)
$avail = diff $drives $alpha | select -ExpandProperty inputobject
$drive = $avail[0] + ':'
# prepare for write-progress
$index = 0
$total = $files.Count
$starttime = $lasttime = Get-Date
$result = foreach ($file in $files) {
# this is just the write-progress section
$index++
$currtime = (Get-Date) - $starttime
$avg = $currtime.TotalSeconds / $index
$last = ((Get-Date) - $lasttime).TotalSeconds
$left = $total - $index
$WrPrgParam = @{
Activity = (
"imagemagick.exe $(Get-Date -f s)",
"Total: $($currtime -replace '\..*')",
"Avg: $('{0:N2}' -f $avg)",
"Last: $('{0:N2}' -f $last)",
"ETA: $('{0:N2}' -f (($avg * $left) / 60))",
"min ($([string](Get-Date).AddSeconds($avg*$left) -replace '^.* '))"
) -join ' '
Status = "$index of $total ($left left) [$('{0:N2}' -f (($index/$total)*100))%]"
CurrentOperation = "FILE: $file"
PercentComplete = ($index/$total)*100
}
Write-Progress @WrPrgParam
$lasttime = Get-Date
# if filename is longer than 240 characters,
# map a drive to the current path to shorten the filename
$null = subst $drive /d
$path, $newfile = ''
if ($file.length -gt 240) {
$path = Split-Path $file
subst $drive $path
$newfile = Join-Path $drive $(Split-Path $file -Leaf)
}
if ($newfile) {
& $tool convert "$($newfile -Replace '.tif+$', '.tif[0]')" "C:\Data\$($(Split-Path $newfile -Leaf) -Replace '.tif+$', '.pdf')"
} else {
& $tool convert "$($file -Replace '.tif+$', '.tif[0]')" "C:\Data\$($(Split-Path $file -Leaf) -Replace '.tif+$', '.pdf')"
}
# un-map the drive (whether we mapped it or not, just to be sure)
$null = subst $drive /d
}
$result