Search code examples
powershellargumentslibtiff

Powershell - Argument with a space errors with both single and double quotes


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
    }
}

Solution

  • 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!