-1

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
    }
}
extispex
  • 51
  • 1
  • 5

3 Answers3

0

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
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Neither one of those solutions work. It actually doesn't pass any of the variables. It essentially just runs tiffcp.exe without anything. The output gives me a list of all of the options. It gives this error: `tiffcp.exe : LIBTIFF, Version 3.8.2 At c:\2-Insert_Copyright_into_TIFFs.ps1:15 char:5 + & $tool $Args + ~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (LIBTIFF, Version 3.8.2:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError` – extispex Mar 24 '14 at 14:56
  • Here is the echoargs: `Arg 0 is Arg 1 is <-c lzw> Arg 2 is Arg 3 is Arg 4 is ` – extispex Mar 24 '14 at 15:21
  • If you just type it all out in PS does it work? `C:\tiff-3.8.2-1-bin\bin\tiffcp.exe -c lzw c:\copyright pages\copyright.tif c:\test.tif c:\copyright-test.tif` doesn't kick back errors does it? – TheMadTechnician Mar 24 '14 at 16:03
  • Yes. Gives the same error. That is why I am pretty sure it is the powershell bug. – extispex Mar 24 '14 at 18:11
0

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
}
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • There never seemed to be a problem with the naming. Like I said, the script worked, and it named all of the files correctly. But anyway, both of your examples throw the same error. ` & $tool -c lzw "c:\copyright pages\copyright.tif" $tif.FullName $ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (TIFFReadDirecto...4) encountered.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError` – extispex Mar 24 '14 at 15:05
0

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!

Community
  • 1
  • 1
extispex
  • 51
  • 1
  • 5
  • That is interesting! And it answers another post I saw recently about that error in PowerShell too. I still don't get how your filenames were not corrupting, but oh well. – TessellatingHeckler Mar 24 '14 at 22:44