0

I'm having trouble with a batch script that worked a few days ago, but now doesnt work, even though no changes has been made! I believe something has changed in the system without my knowledge.

The expected link is:

order.htm?order=12345

But it becomes like this: (notice the question mark becomes %3F)

order.htm%3Forder=12345

The code is as follows:

@echo off
echo.

set "drive=%~d0"
set "runningDir=%~dp0"

:start
ClS
Echo.
Set /P Job=Enter number:^>

@echo off

if exist c:\"Program Files (x86)"\Google\Chrome\Application\chrome.exe goto program_files_x86

:program_files_x86
start c:\"Program Files (x86)"\Google\Chrome\Application\chrome.exe --disable-print-preview --ignore-certificate-errors --disable-web-security --user-data-dir --allow-file-access-from-files %runningDir%\order.htm?order=%job%
goto end

:end
goto start

Any suggestions?

Best Regards Niclas

Niclas
  • 135
  • 3
  • 13

1 Answers1

2

Double quotes should be usually used around entire folder/file string and not just parts of it.

Command START interprets first double quoted string as title for the new command process. Therefore on starting a GUI application an empty title string specified with "" should be used on START command line to avoid interpreting the double quoted file name with path of the application to execute as title string.

The batch file path referenced with %~dp0 always ends with a backslash. Therefore don't specify an extra backspace after this string or an environment variable like runningDir with path of batch file. By the way: The current directory on running a batch file can be different to directory of batch file. For that reason the name runningDir is not good as misleading. A better name for the environment variable is BatchPath.

It is possible to use start as label in a batch file. But it is not advisable to do that because of command START which makes it difficult to search for label respectively search for command. It is better to use a label like Begin.

In an url the directory separator is / and therefore each backslash (directory separator on Windows) in batch file path should be substituted by a slash.

The url should start with the protocol like http:// (Hypertext Transfer Protocol) and should be enclosed completely in double quotes.

And last echo/ or echo( is better than echo. for printing a blank line, see Difference between Echo[Special Character] for details.

The rewritten batch code:

@echo off
echo/

set "BatchPath=%~dp0"
set "BatchPath=%BatchPath:\=/%"

:Begin
clS
echo/
set /P "Job=Enter number: "

if exist "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" goto program_files_x86

:program_files_x86
start "" "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" --disable-print-preview --ignore-certificate-errors --disable-web-security --user-data-dir --allow-file-access-from-files "http://%BatchPath%order.htm?order=%Job%"
goto end

:end
goto Begin

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cls /?
  • echo /?
  • goto /?
  • if /?
  • set /?
  • start /?
Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143