Question

This code takes a directory with tiffs, uses the tiflib tiffcp.exe and inserts another tiff at page one that is a copyright notice. The part that continually errors is when I call the compression. & $tool -c lzw

The error it gives is

& $tool -c lzw ` + ~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (TIFFReadDirecto...4) encountered.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError

Now, I know that this is because there is a space in the argument. I have tried putting it in double quotes, single quotes, putting it in a parameter, putting it in two parameters, and a bunch of other things. No matter what I do, the LZW part seems throw an error. I should say that it still works for some reason. It still inserts the page and moves the old copy. Thanks for any help that can be given.

$tool = 'c:\tiff-3.8.2-1-bin\bin\tiffcp.exe'
$tifs = get-childitem . | where {$_.Extension -match "tif"}

foreach($tif in $tifs)
{
    if ($tif -like '*copyright*')
        {
        "File already has copyright notice " + $tif
        }
    else        
        {   
    'Processing ' + $tif.Name        
    $output = "copyright-$tif"
    & $tool "-c lzw" `
    "c:\copyright pages\copyright.tif"  $tif.FullName $output

     Move-Item $tif C:\backup
    }
}
Was it helpful?

Solution 3

I believe I have fallen victim to a bug/choice in Poweshell itself. I think I am encountering this exact problem.

Ignoring an errorlevel != 0 in Windows Powershell

and

https://superuser.com/questions/213848/using-powershell-call-native-command-line-app-and-capture-stderr

I will take the answers given here to heart as ways to clean up my code though. Thanks for the help!

OTHER TIPS

Try it with an array of arguments instead.

$Args = @("-c lzw","c:\copyright pages\copyright.tif",$tif.FullName,$output)
& $tool $Args

Or if you are uncomfortable with that you could assign variables to everything and just throw the variables at the call operator like this:

$arg1 = "-c lzw"
$arg2 = "c:\copyright pages\copyright.tif"
$arg3 = $tif.FullName
& $tool $arg1 $arg2 $arg3 $output

This code:

$output = "copyright-$tif"
& $tool "-c lzw" "c:\copyright pages\copyright.tif"  $tif.FullName $output

results in a command a bit like this:

tiffcp.exe -c lzw "1.tif" "c:\2.tif" "copyright-c:\2.tif"

The problem is that "copyright-$tif" is putting the full file path in, and that makes the output a nonsense filename.

Try:

$tool = 'c:\tiff-3.8.2-1-bin\bin\tiffcp.exe'
$tifs = Get-ChildItem .\*.tif

foreach ($tif in $tifs)
{
    if ($tif -like '*copyright*')
    {

        write "File already has copyright notice $tif"

    } else {

        write "Processing $tif"

        $output = "copyright-$($tif.Name)"
        & $tool -c lzw "c:\copyright pages\copyright.tif" $tif.FullName $output

        Move-Item $tif C:\backup
    }
}

Comments:

  • Get-ChildItem can find *.tif directly, without getting all files and using where, so I changed that.

  • Used string interpolation for `"Processing $tif" and similar

  • I think if ($tif -match 'copyright') or if ($tif.Name.Contains("copyright")) feel nicer, but why not filter those out earlier, when you get-childitem?

EDIT example code filtering out the already copyrighted ones:

e.g.

$tool = 'c:\tiff-3.8.2-1-bin\bin\tiffcp.exe'
$tifs = (gci .\*.tif | ?{ $_.Name -notmatch "copyright" })

foreach ($tif in $tifs) {

    write "Processing $tif"

    & $tool -c lzw "c:\copyright pages\copyright.tif" "$tif" "copyright-$($tif.Name)"

    Move-Item $tif C:\backup
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top