Nick Thompson
Nick Thompson

Reputation: 3

Passing a parameter into a parameter

Help!!

So I have a batch file that looks at the name of a file and then prints to sumatrapdf.exe to a printer name based on the original file name. The printer name is stored in a parameter. When I echo the printer name parameter it is correct. When I pass the printer name parameter to the print program, it errors, assuming its because it cannot see the printer name parameter. (thinking I somehow need to pass across another set of "%"'s so that batch file knows its a parameter.

FOR %%F IN (%C:\Users\nick\Desktop\Test1%\Shelf*.csv) DO (
 set filename=%%~nF
 Set filename1=%%F
 goto tests
)
:tests
echo "%filename1%"
echo "%filename%"

Set "Shelf-01Printer=NPIBBF846 (HP Color LaserJet CP2025dn)"
Set "Shelf-07Printer=NPIBBF846 (HP Color LaserJet CP2025dn)"
Set "Shelf-97Printer=NPIBBF846 (HP Color LaserJet CP2025dn)"
Set "Hook-09Printer=HP LaserJet P2050 Series PCL6"
Set "PrinterName=%filename%Printer"
Echo %PrinterName%

Start SumatraPDF.exe -print-to %PrinterName% "C:\Users\nick\Desktop\Test1\% filename%".pdf
Timeout /t 15

Upvotes: 0

Views: 35

Answers (1)

Squashman
Squashman

Reputation: 14320

You have a space in your variable expansion: % filename%. Remove the space. But that doesn't fix your logic. Essentially you are trying to do double variable expansion. You can accomplish this using a neat trick with the CALL command.

CALL Set "PrinterName=%%%filename%Printer%%"

Using the CALL command gives you two phases of variable expansion. When the CALL command executes the line of code becomes:

CALL Set "PrinterName=%Shelf-01Printer%"

Then the SET command executes and sets the variable to the appropriate printer name.

Upvotes: 3

Related Questions