Expanding variables in a Windows batch file

2019-08-31 08:53发布

问题:

I run the following command in a Windows batch file:

start "" "C:\PDF Viewer\PDFXCview.exe" /A "page=1&zoom=33.3" "G:\my pdfs\file 1.pdf" /A "page=4&zoom=55.5" "G:\my pdfs\file 2.pdf"

The works perfectly fine and opens both PDF files using their respective parameters. However, to make the process cleaner, I would like to start using variables in place of the PDF files (and even the PDF viewer executable). However, when I use variables, only the first PDF file opens:

set PDF1="G:\my pdfs\file 1.pdf"
set PDF2="G:\my pdfs\file 2.pdf"
start "" "C:\PDF Viewer\PDFXCview.exe" /A "page=1&zoom=33.3" %PDF1% /A "page=4&zoom=55.5" %PDF2%

I should mention that I don't have this issue if I remove the /A command and the subsequent parameters for each file.

回答1:

Wrong quotation in set commands. Use set "variable=value" syntax as follows:

set "PDF1=G:\my pdfs\file 1.pdf"
set "PDF2=G:\my pdfs\file 2.pdf"
start "" "C:\PDF Viewer\PDFXCview.exe" /A "page=1&zoom=33.3" "%PDF1%" /A "page=4&zoom=55.5" "%PDF2%"