2016-05-04 31 views
1

長いパス名(> = 260文字)に当たると、スクリプトが終了します。 tifからpdfへの変換で長いパス名を取得するためにスニペットを変更するにはどうすればよいですか。または、単純にすべてのファイル<を260文字に分解し、それらにスクリプトを適用する方が簡単です(残りのロングネームファイルにどのように対処するかはまだ分かりません)。AlphaFSを使って259文字を超えるパス名を取得するpowershell

SNIPPET:(これは限りpathnameは< 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に変換しています。

答えて

1

申し訳ありませんが、私はAlphaFS dllファイルを使用していないため、長期的に実装する方が簡単かもしれませんが、ロボファイルの組み合わせを使用して一時的にパスを短縮する

これは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 
+0

ありがとうございます。何かが私の終わりのように思われません - 私がGet-Filesをdirlistにパイプすると、次のようになります: "(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」 – val

+0

私はここでの回答のいずれかを使用してモジュールにそれを回すことによってあなたのGet-ファイル機能を追加しようとしました:のhttp:/ /stackoverflow.com/questions/6016436/in-powershell-how-do-i-define-a-funcファイルからの呼び出しとそれからの呼び出し – val

+0

機能が動作していない場合は、そのコメントを見て、robocopyをどのように使用しているかを確認し、get-files行を置き換えることができますrobocopyコマンドと正規表現を使用しています。 *編集:-filterを-includeに変更してみる –

関連する問題