How to allow input of any character

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
thebrenda
Posts: 7
Joined: 25 Jun 2015 13:21

How to allow input of any character

#1 Post by thebrenda » 28 Jun 2015 15:05

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%

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to allow input of any character

#2 Post by aGerman » 28 Jun 2015 17:35

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.

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

thebrenda
Posts: 7
Joined: 25 Jun 2015 13:21

Re: How to allow input of any character

#3 Post by thebrenda » 28 Jun 2015 17:43

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.

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


aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to allow input of any character

#4 Post by aGerman » 28 Jun 2015 17:56

You have the half of the assignment already in your file. If it was ...

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!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to allow input of any character

#5 Post by foxidrive » 28 Jun 2015 22:32

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.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to allow input of any character

#6 Post by penpen » 29 Jun 2015 02:51

You might do something like this (using "findstr" to locate the password line, reading it using "set /P", and avoiding "call"):

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
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":

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.

thebrenda
Posts: 7
Joined: 25 Jun 2015 13:21

Re: How to allow input of any character

#7 Post by thebrenda » 29 Jun 2015 12:59

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 \".

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to allow input of any character

#8 Post by Squashman » 29 Jun 2015 13:35

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=\"%>

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to allow input of any character

#9 Post by aGerman » 29 Jun 2015 14:33

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!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to allow input of any character

#10 Post by penpen » 30 Jun 2015 16:28

aGerman wrote:If the backslash is used as escape character I wonder if you have to escape (double) it in the password as well.
Yes, you need to do this (i've corrected this flaw in my above post, too).

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

Post Reply