0

OK... It's me again. Same project. I'm trying to loop through files in a directory and set the page size on all the PDFs. I'm having difficulty getting the file names in the parameters I need to pass them through ghostscript. I gave up on changing the parameters IN the .run shell command and decided to create the parameters in a variable (as you can see). My problem seems to be in the strParam variable that I am passing. It looks correct when I check it in the msgbox but when the script runs I don't get any files created. If I hardcode the 2 parameters I need to change ("-sOUTPUTFILE=" and file name) it works as expected.

Dim FSO, FLD, FIL, X
Dim strInput, strFolder, strParam

strInput = InputBox ("1 - Landscape" & chr(13) & "2 - Portrait", "Original Image   Orientation")
strFolder = InputBox ("Copy and paste the folder location" & chr(13) & "to your scans",  "File Location")
Set objShell = WScript.CreateObject("WScript.Shell")

If strInput=1 then  
    Call LandScape 
ElseIf strInput=2 Then
    Call Portrait
Else
    MsgBox "Your entry is invalid.  Click OK to exit"
End If


Sub LandScape
ObjShell.CurrentDirectory= strFolder
Wscript.Echo objShell.CurrentDirectory
    MsgBox "Your images are Landscape"
    MsgBox "Your files are located at: " & strFolder
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set FLD = FSO.GetFolder(strFolder)

    For Each FIL In FLD.Files
        strParam = chr(34) & " -dQUIET -dNOPAUSE dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=new-" & FIL.Name & " " & FIL.Name & chr(34) & chr(34) & chr(34)
        MsgBox strParam
        objShell.Run """c:\Program Files\gs\gs9.04\bin\gswin64c.exe" & strParam
    Next

End Sub
Matthew
  • 25
  • 1
  • 10
  • This is the hardcoded line that works: objShell.Run """c:\Program Files\gs\gs9.04\bin\gswin64c.exe"" -dQUIET -dNOPAUSE -dBATCH -sPAPERSIZE=archD -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf INPUT.pdf""" – Matthew Aug 02 '14 at 05:52
  • It looks like you took _part_ of my answer from your [other question](http://stackoverflow.com/questions/25089507/pass-parameters-to-an-application) but you're adding `Chr(34)` around the parameter list instead of the exe path. Put `Chr(34)` around the path to the exe, not your parameter list, like I showed in your other question. If your input/output file params have spaces, do the same for those. – Bond Aug 02 '14 at 14:23

2 Answers2

1

As your problem is caused by the parameter to the .Run method, you should build a strCmd variable and display that (instead of just strParam) to rule out that the final concatenation messes things up.

If you do that, you can copy & paste the command and test it from a command line instead of doing a check with a different command (e.g. no -dDEVICEWIDTHPOINTS=2592 in your hard-coded command).

As I can't test you command(s), all I can do is point out that the 3 quotes at the end of

"c:\Program Files\gs\gs9.04\bin\gswin64c.exe" -dQUIET -dNOPAUSE dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHT
POINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=new-04.vbs 04.vbs"""

look fishy. Do you want to quote both file names?

You should build your (complex) command line in a structured way. For background see here and here (if you aren't confused easily).

Update wrt @Bond's answer:

There can't be any doubt that Bond's way of 'building a command line in a structured way' is sound. It will work for many people and many comparable tasks just fine. But people are wired differently, so it may pay to know another way to skin those cats:

Option Explicit
Function qq(s) : qq = """" & s & """" : End Function
Dim sFiNa : sFiNa = "justafile.ext"
Dim sCmd  : sCmd  = Join(Array( _
     qq("c:\Program Files\gs\gs9.04\bin\gswin64c.exe") _
   , "-dQUIET -dNOPAUSE dBATCH" _
   , "-dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728" _
   , "-dFIXEDMEDIA -sDEVICE=pdfwrite" _
   , "-sOutputFile=" & qq(sFiNa) _
   , qq(sFiNa) _
))
WScript.Echo sCmd

output:

"c:\Program Files\gs\gs9.04\bin\gswin64c.exe" -dQUIET -dNOPAUSE dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile="justafile.ext" "justafile.ext"
  1. The quoting is implemented once; no need to check 6 occurrences of Chr(34) to guard against one typo Chr(43).
  2. The Join provides the spaces between arguments automagically; no need to check each & for "Need I a space here too?".
  3. Because the full command line is build, it can be tested; in contrast, the correctness of strExe & " " & strParms (is there really a space between the quotes or some funny letter that displays like a space in my editor?; did I misspell strParams (while not using "Option Explicit")?) may still be in doubt.

(4) The test may well throw an error like "input and output must be different". Such a problem is easier to catch if you don't have extra noise (and the temptation to program by copy & paste) as in

strParams = strParams & Chr(34) & FIL.Name & Chr(34)
strParams = strParams & " "
!-- noise -----------------------!        !--------!
strParams = strParams & Chr(34) & FIL.Name & Chr(34)

vs:

                 !----!     !
, "-sOutputFile=" & qq(sFiNa) _

In addition, the meaning of the parameter isn't hidden in columns 123ff in a statement 3 lines before, and it can't messed up by adding the "-dNoErrorsPlease" argument later. A possible fix

, "-sOutputFile=" & qq("new-" & sFiNa) _

looks complicated but (for a VBScripter) obviously means: "output to file with added prefix 'new-', quoted to avoid problems with spaces in file names".

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • All these suggestions worked. I will post my (now) final code once I clean it up. I do have a few questions: Is it necessary for me to use objShell.CurrenDirectory? Or does 'code' Set FLD = FSo.GetFolder(strFolder) 'code' do the same thing? Also do you usually leave your troubleshooting code commented in your final code or do you completely remove it? – Matthew Aug 04 '14 at 21:09
  • OK. I figured I didn't need to CurrentDirectory but without it the script only converted the first file in the directory. Our of curiosity can anyone explain that? – Matthew Aug 04 '14 at 22:10
1

Like @Ekkehard mentioned, you need to build this up in a structured way. Look at your original command line (outside of VBScript) and wherever you see a " use Chr(34) instead. Then just use normal string concatenation in VBScript to bring everything together. It's best to not mix double-quote-escaping ("") with Chr(34). Just use one or the other. For clarity, Chr(34) is usually the best choice.

strParams = "-dQUIET -dNOPAUSE dBATCH -dDEVICEWIDTHPOINTS=2592 -dDEVICEHEIGHTPOINTS=1728 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=new-"
strParams = strParams & Chr(34) & FIL.Name & Chr(34)
strParams = strParams & " "
strParams = strParams & Chr(34) & FIL.Name & Chr(34)

strExe = Chr(34) & "c:\Program Files\gs\gs9.04\bin\gswin64c.exe" & Chr(34)

objShell.Run strExe & " " & strParams
Bond
  • 16,071
  • 6
  • 30
  • 53