How to allow input of any character
Moderator: DosItHelp
How to allow input of any character
I have a .bat file that calls a java program with a password parameter. Obviously the password can contain any combination of special characters. Currently the operator is editing the .bat file and entering the password with a set statement and then running. it is not working so well. what options do i have (want to minimize operator entry) so that the password will be passed to the java program exactly how it is entered, regardless of the combination of special characters?
set password=~!@#$%&*()_+{}|:^"<>?`1-=[]\;',./
call java program %password%
set password=~!@#$%&*()_+{}|:^"<>?`1-=[]\;',./
call java program %password%
Re: How to allow input of any character
Recently Carlos published his revised Password Input function. It workes with any input and also hides the input using asterisks.
More complicated could be passing the string to another application. First of all you should use delayed expansion to avoid misinterpretations.
Nevertheless I'm not certain if you could run into trouble with unbalanced quotation marks and/or default token separators like commas, semicolons, etc.
Regards
aGerman
More complicated could be passing the string to another application. First of all you should use delayed expansion to avoid misinterpretations.
Code: Select all
setlocal EnableDelayedExpansion
call "java program" !password!
Nevertheless I'm not certain if you could run into trouble with unbalanced quotation marks and/or default token separators like commas, semicolons, etc.
Regards
aGerman
Re: How to allow input of any character
OK, there is so much that I do not know about DOS batch (not even sure what to call it). But I have come up with the following but it is terribly flawed
ParameterInput.txt file. Again the idea is that the operators do not have to be concerned with escaping special characters. They would edit this file only.
password=~!@#$%^&*()_+{}|:"<>?`[]\;'./"%"
userid=brenda
Then working on something like this, that would read the ParameterInput.txt file line by line, but cannot figure out how to know which line I am on, so that it can be set to the correct variable, and also the "%JAVA_HOME%" and %CLASSPATH% are defined outside the FOR block.
ParameterInput.txt file. Again the idea is that the operators do not have to be concerned with escaping special characters. They would edit this file only.
password=~!@#$%^&*()_+{}|:"<>?`[]\;'./"%"
userid=brenda
Then working on something like this, that would read the ParameterInput.txt file line by line, but cannot figure out how to know which line I am on, so that it can be set to the correct variable, and also the "%JAVA_HOME%" and %CLASSPATH% are defined outside the FOR block.
Code: Select all
@echo off
setlocal DisableDelayedExpansion enableextensions
FOR /F "tokens=* delims=" %%a IN (.\InputParameter.txt) DO (
set "myVar=%%a"
setlocal enabledelayedexpansion
echo myVar is !myVar!
rem want to set password only if 1st line or !myvar:~0,8% == password
set password=!myvar!
rem want to set userid only if 2st line or !myvar:~0,6% == userid
set userid=!myvar!
rem want to execute this only if 2st line or !myvar:~0,6% == userid (done with input file)
rem "%JAVA_HOME%" -classpath %CLASSPATH% Datasource !password! !userid!
rem want to execute this if
echo password is !password!
echo userid is !userid!
endlocal
)
pause
Re: How to allow input of any character
You have the half of the assignment already in your file. If it was ...
... you can use the whole line together with the SET statement.
Code: Select all
password=~!@#$%^&*()_+{}|:"<>?`[]\;'./"%"
userid=brenda
... you can use the whole line together with the SET statement.
Code: Select all
setlocal DisableDelayedExpansion
for /f "usebackq delims=" %%i in ("InputParameter.txt") do set "%%i"
:: Display what we have:
setlocal EnableDelayedExpansion
echo !password!
echo !userid!
Re: How to allow input of any character
thebrenda wrote:what options do i have (want to minimize operator entry) so that the password will be passed to the java program exactly how it is entered, regardless of the combination of special characters?
One plan may be to use a VBS script wrapped inside a batch file, with the VBS script
getting input and launching the java widget.
Re: How to allow input of any character
You might do something like this (using "findstr" to locate the password line, reading it using "set /P", and avoiding "call"):
Notes:
Java should support parameters enclosed in doublequotes, with an internal escape of doublequotes using "".
If you don't use delayed expansion and call you only have to double '%' and '"' characters.
If reading from file, you only need to double '"' characters.
Java sample "Start.java":
Result:
penpen
Edit: Now doubles '\'.
Code: Select all
@echo off
:: syntax to set password: ::password="<password>"
::password="()[]{}^=;!'+,`~ %""
setlocal enableExtensions disableDelayedExpansion
for /F "delims=:" %%a in ('findstr /N "^::password=" "%~f0"') do (
<"%~f0" (
for /L %%b in (1, 1, %%~a) do (
set "password="
set /P "password="
)
)
)
setlocal enableExtensions enableDelayedExpansion
set "password=!password:~12,-1!"
set ^"password=!password:"=""!"
set "password=!password:\=\\!"
setlocal enableExtensions disableDelayedExpansion
java Start "%password%"
endlocal
endlocal
endlocal
goto :eof
Java should support parameters enclosed in doublequotes, with an internal escape of doublequotes using "".
If you don't use delayed expansion and call you only have to double '%' and '"' characters.
If reading from file, you only need to double '"' characters.
Java sample "Start.java":
Code: Select all
public class Start {
public static void main(String[] args) {
if (args != null) for (int i = 0; i < args.length; ++i) {
System.out.println(args[i]);
}
}
}
Result:
Code: Select all
Z:\>test.bat
()[]{}^=;!'+,`~ %"
penpen
Edit: Now doubles '\'.
Last edited by penpen on 30 Jun 2015 16:26, edited 1 time in total.
Re: How to allow input of any character
aGerman - your suggestion is working like a charm. The only issue that I am having now is if the input parameters have embedded double quotes. the dos batch handles them fine, but java wants them escaped with a /. So how can i Find and replace in dos batch? I want to search my varaibles for an embedded " and replace with \".
Re: How to allow input of any character
This is the general syntax for string substitution. But as was mentioned earlier you probably want to use delayed expansion.
Code: Select all
C:\Users\Squash>set "quotes="quotes""
C:\Users\Squash>echo %quotes%
"quotes"
C:\Users\Squash>set "escaped=%quotes:"=\"%"
C:\Users\Squash>echo %escaped%
%quotes=\"%>
Re: How to allow input of any character
As Squashman already wrote you have to use string substitution. If the backslash is used as escape character I wonder if you have to escape (double) it in the password as well. In that case just un-comment the third line of the below snippet.
Code: Select all
setlocal DisableDelayedExpansion
for /f "usebackq delims=" %%i in ("InputParameter.txt") do set "%%i"
:: set "password=%password:\=\\%"
set "password=%password:"=\"%"
:: Display what we have:
setlocal EnableDelayedExpansion
echo !password!
echo !userid!
Re: How to allow input of any character
Yes, you need to do this (i've corrected this flaw in my above post, too).aGerman wrote:If the backslash is used as escape character I wonder if you have to escape (double) it in the password as well.
I also prefer, to use doubled doublequotes, and encapsulating the (java) parameter within doublequotes, so you don't need to worry about most dos special characters (but that's a matter of taste).
penpen