0

I need either a native command or a lightweight simple 3rd party utility that I can use to set an environment variable in a batch file, but limit it to a certain number of characters.

For example:

Enter message here (28 characters max):

Then of course the user can only put in 28 characters.

Any ideas?

Thanks!

3
  • This is just one of those keep it simple moments. Just substring the variable after the user enters their input to truncate it. set input=%input:~0,28%
    – Squashman
    Jan 30, 2018 at 21:53
  • Unfortunately, it's more to help the user to keep it to that. Basically, I'm using it to watermark an image and need to limit the character count so they don't enter something that will run off the image :)
    – N. Johnson
    Jan 31, 2018 at 22:39
  • So do as Squashman said and in your batch file set input=%input:~0,28% to parse it and only keep the first 28 characters of the file name that way. Read about it more here: dostips.com/DtTipsStringManipulation.php Jan 31, 2018 at 22:46

1 Answer 1

1

After searching around, xcopy seemed to be the most solid choice.

set "key="
for /f "delims=" %%A in ('xcopy /w "%comspec%" "%comspec%" 2^>nul') do if not defined key set "key=%%A"
set "key=!key:~-1!"

fetches a key, with wich you can build the input.

if "!input:~-%length%!" == "!input!"

allows for checks on length (in your case 28).


You can also the input.bat script I wrote. By using a macro you can fetch the output of the script. First you declare the macro:

set input=^
for %%. in (1 2) do if %%.==2 (^
    for /F "delims=" %%I in ('input.bat !args!') do ^
        set "result=%%I"^
) else set args=

then you use it like this:

setlocal EnableDelayedExpansion
%input% "prompt=Password: " "length=28"

You can then use the result variable to get your input.

IMPORTANT

For defining the macro delayed expansion needs to be DISABLED. While using it however, delayed expansion needs to be ENABLED.


Resources

xcopy input - DosTips.com

batch macros - DosTips.com

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .