0

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
  • 2
    I suggest you to use the _standard array notation_ enclosing the _index_ or _subscript_ in square braquets: `Set "Printer[Shelf-01]=NPIBBF846 (HP Color LaserJet CP2025dn)"` or `Set "Printer[Shelf-07]=NPIBBF846 (HP Color LaserJet CP2025dn)"`, etc. You may process _array elements_ via `Delayed Expansion` this way: `Set "PrinterName=!Printer[%filename%]!"`. See [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). – Aacini Nov 15 '18 at 17:59
  • @Aacini, you are correct. This probably should be marked as a duplicate question. Or I should have the delayed expansion example in my answer as well. Just felt using the CALL was easier to explain then delayed expansion. It always seems to confuse new users. – Squashman Nov 15 '18 at 18:19

1 Answers1

3

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.

Squashman
  • 13,649
  • 5
  • 27
  • 36