Problems using parameters in a STARTed Batch file
Posted: 05 Jul 2024 07:25
I discovered a couple problems when I want to use special and extended characters in parameters in a Batch file started with START command. This is the test.bat Batch file:
... that works perfectly when it is used this way:
... but I want to use special characters in the parameters, so I need to enclose they in quotes:
Ok. Now, I need to enclose the Batch file name in quotes because its path will contain spaces. Lets start with a test with no special characters first:
... that works OK. However, if I enclose the parameters in quotes (because they will contain special characters) the problems begins. This command:
... show this output in the started cmd.exe window:
After several tests, the only way I found to fix this point is include a CMD /K command after the START command AND insert an additional pair of quotes at the very beginning and end of the whole command, that is:
... that correctly works again.
However, if I include the special characters in the parameters:
... the problems returns...
After several tests I discovered that the only way to include special characters in the parameters in this way is escaping the special characters:
... that is not a convenient solution.
First question: is there any better way to include special characters in the parameters of a Batch file started this way?
Ok. If the problem is the characters included in the parameters, then just eliminate the parameters. I changed the test.bat file for this line:
... and executed it this way:
... that correctly show "First >" "Second |" in the started cmd.exe window.
===================================================================================
Ok. Lets pass now to the second problem. I want that the parameters include extended (beyond ASCII 127) characters:
This test correctly show the extended characters in the started cmd.exe window. However, this test was executed from the keyboard. Of course, I want to use the test.bat file from another Batch file, so I prepared it:
When I executed this second.bat file the started cmd.exe window show characters, but the characters are different:
I thought at first that I found the problem: both test.bat and second.bat were saved with the default Windows Notepad encoding: UTF-8. This point had caused me several problems in the past...
So I saved both files with ANSI encoding and checked that the characters in second.bat file be correct. Anyway, the characters shown ARE NOT THE SAME!
What happens here? How can I show the same extended characters of the calling Batch file in the called one?
Antonio
Code: Select all
@echo %*
Code: Select all
start "Title" test.bat First Second
Code: Select all
start "Title" test.bat "First >" "Second |"
Code: Select all
start "Title" "test.bat" First Second
Code: Select all
start "Title" "test.bat" "First" "Second"
Code: Select all
"test.bat" "First" "Second" is not recognized as an external or internal command, etc...
Code: Select all
start "Title" cmd /K ""test.bat" "First" "Second""
However, if I include the special characters in the parameters:
Code: Select all
start "Title" cmd /K ""test.bat" "First >" "Second |""
After several tests I discovered that the only way to include special characters in the parameters in this way is escaping the special characters:
Code: Select all
start "Title" cmd /K ""test.bat" "First ^>" "Second ^|""
First question: is there any better way to include special characters in the parameters of a Batch file started this way?
Ok. If the problem is the characters included in the parameters, then just eliminate the parameters. I changed the test.bat file for this line:
Code: Select all
@echo %parameters%
Code: Select all
set parameters="First >" "Second |"
start "Title" cmd /K "test.bat"
===================================================================================
Ok. Lets pass now to the second problem. I want that the parameters include extended (beyond ASCII 127) characters:
Code: Select all
set parameters="ÇüéâäàåçêëèïîìÄÅ" "áíóúñÑ¿¡"
start "Title" cmd /K "test.bat"
Code: Select all
@echo off
set parameters="ÇüéâäàåçêëèïîìÄÅ" "áíóúñÑ¿¡"
start "Title" cmd /K "test.bat"
Code: Select all
"├ç├╝├®├ó├ñ├á├Ñ├º├¬├½├¿├»├«├¼├ä├à" "├í├¡├│├║├▒├æ┬┐┬í"
So I saved both files with ANSI encoding and checked that the characters in second.bat file be correct. Anyway, the characters shown ARE NOT THE SAME!
Code: Select all
"óÚÔõÓÕþÛÙÞ´¯ý─┼" "ßݾ·±Ð┐í"
Antonio