【问题标题】:powershell using AlphaFS to capture path names longer than 259 characterspowershell 使用 AlphaFS 捕获超过 259 个字符的路径名
【发布时间】:2016-08-30 12:28:56
【问题描述】:

我的脚本在遇到长路径名(>= 260 个字符)时退出。我如何更改我的 sn-p 以在我的 tif 到 pdf 转换中捕获长路径名。还是简单地将所有文件

SNIPPET:(只要路径名

$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 blog+posts,看来我需要使用语法

[Alphaleonis.Win32.Filesystem.File]::<some AlphaFS function here?>

但我需要一些与我的申请相关的进一步指导或示例。

我在使用 Powershell v 4 的 Windows Server 2012 R 上安装了 AlphaFS 库。我正在使用 imagemagick 将 PDF 的第一页转换为 TIF。

【问题讨论】:

    标签: .net powershell imagemagick imagemagick-convert


    【解决方案1】:

    抱歉,我没有使用 AlphaFS dll 文件,从长远来看,这可能更容易实现,但这里有一个可能的解决方法,使用 robocopy 组合获取长文件名和 subst 临时缩短路径

    这利用了这里的函数来进行 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
    

    【讨论】:

    • 谢谢。我的结果似乎有些不对劲 - 当我将 Get-Files 通过管道传输到目录列表时,如下所示:“(Get-Files -Path $source -Filter Elko*.tif).fullname | Export-Csv .\dirlist.csv -NoTypeInformation”,我得到一个文件:“长度”“103”“144”“161”“147”“154”“200”“149”“152”“163”“144”“149”“144” “151”“144”“143”
    • 我尝试使用以下答案之一将您的 Get-Files 函数转换为模块:stackoverflow.com/questions/6016436/…
    • 如果该功能不起作用,您可以查看它的 cmets 以了解它如何使用 robocopy,然后只需将 get-files 行替换为 robocopy 命令和一些正则表达式。 *编辑:尝试将 -filter 更改为 -include
    猜你喜欢
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2017-08-10
    • 2018-11-28
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多