bat script with parameters and personalized message

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: bat script with parameters and personalized message

#16 Post by Compo » 24 Jun 2016 09:02

balubeto wrote:Does anyone know how to solve my problem in a more suitable form?

Thanks

Bye
balubeto wrote:Hi

I created a bat file with parameters that must be entered from the command line.

I would like this script is stopped with a personalized message if it will run without any parameters or with the wrong number of parameters.

So, how do I do this?

Thanks

Bye
Compo wrote:I would use something similar to this:

Code: Select all

If %1' Equ ' (Echo=You didn't enter any parameters.
   GoTo :Qend)

If %5' NEq ' (Echo=Too many parameters entered.
   GoTo :Qend)

If %4' Equ ' (Echo=Insufficient parameters entered.
   GoTo :Qend)

Rem Other code below here



:Qend
Echo=Press any key to exit. . .
Timeout -1 1>Nul
Exit/B


Just bear in mind that his only checks the number, not the correctness, of parameters.


I already did!

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: bat script with parameters and personalized message

#17 Post by balubeto » 24 Jun 2016 09:42

Compo wrote:
balubeto wrote:Does anyone know how to solve my problem in a more suitable form?

Thanks

Bye
balubeto wrote:Hi

I created a bat file with parameters that must be entered from the command line.

I would like this script is stopped with a personalized message if it will run without any parameters or with the wrong number of parameters.

So, how do I do this?

Thanks

Bye
Compo wrote:I would use something similar to this:

Code: Select all

If %1' Equ ' (Echo=You didn't enter any parameters.
   GoTo :Qend)

If %5' NEq ' (Echo=Too many parameters entered.
   GoTo :Qend)

If %4' Equ ' (Echo=Insufficient parameters entered.
   GoTo :Qend)

Rem Other code below here



:Qend
Echo=Press any key to exit. . .
Timeout -1 1>Nul
Exit/B


Just bear in mind that his only checks the number, not the correctness, of parameters.


I already did!


I do not understand:

When I press only the Enter key when the script displays an entry, it is possible to use a For loop or other structure to redisplay the entry?

Thanks

Bye

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: bat script with parameters and personalized message

#18 Post by Compo » 24 Jun 2016 09:52

balubeto wrote:I do not understand:

When I press only the Enter key when the script displays an entry, it is possible to use a For loop or other structure to redisplay the entry?

Thanks

Bye
Put all of your code from the script you posted underneath the line which says Rem Other code below here
save it, run it and tell us what you want to change about it's behaviour.

The only other thing I can guess from your last post is if you replace it with this

Code: Select all

@Echo Off
:loop
If %1' Equ ' (Echo=You didn't enter any parameters.
   GoTo :loop)

If %5' NEq ' (Echo=Too many parameters entered.
   GoTo :loop)

If %4' Equ ' (Echo=Insufficient parameters entered.
   GoTo :loop)

Rem Other code below here
…although that would mean you could not reenter anything on the command line!

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: bat script with parameters and personalized message

#19 Post by balubeto » 24 Jun 2016 10:15

foxidrive wrote:You had some typos:
= instead of ==
exit instead of exist

I'm not sure if this is what you are attempting to do:

Code: Select all

@echo off
:again
set "ESD_directory="
set /p "ESD_directory=Enter the directory where the esd unencrypted file will be inserted: "
if not defined ESD_directory goto :again
If not exist "%ESD_directory%\" md "%ESD_directory%"


In practice, I want to find an alternative solution to the one mentioned above without using the goto command to create the cycle.

Thanks

Bye

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: bat script with parameters and personalized message

#20 Post by Compo » 24 Jun 2016 10:21

balubeto wrote:I want to find an alternative solution to the one mentioned above without using the goto command to create the cycle.

Are you wanting to
  • still have the cycle but without using goto
  • not have the cycle

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: bat script with parameters and personalized message

#21 Post by balubeto » 24 Jun 2016 10:39

Compo wrote:
balubeto wrote:I want to find an alternative solution to the one mentioned above without using the goto command to create the cycle.

Are you wanting to
  • still have the cycle but without using goto
  • not have the cycle


Right.

So, I wait for a solution.

Thanks

Bye

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: bat script with parameters and personalized message

#22 Post by Compo » 24 Jun 2016 10:42

balubeto wrote:Right.

So, I wait for a solution.
You will be waiting a long time because you haven't told me which of the two options you require a solution for!

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: bat script with parameters and personalized message

#23 Post by sambul35 » 24 Jun 2016 11:04

balubeto wrote:I want to find an alternative solution to the one mentioned above without using the goto command to create the cycle.

To be fare, foxidrive gave you the solution sketch that addresses your problem. You need to add some meat to it, or at least post an ALGORITHM (not words) showing how do you envision user input and its validation in your batch, acting like a software architect. :D

There're several means for a user input, such as CHOICE, SET /P commands, or batch arguments. Once you get the input, it must be validated, and if not fit the user asked to re-enter. It does require iteration in the code, which can be done by using GOTO, or CALL to an outside validation function, or SET var & IF !var! combo triggering the code to move forward or do some extras like CALL again. Start with the above code, post your console output instead of forum post quotes, and let us know, what exactly doesn't work. :wink:

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: bat script with parameters and personalized message

#24 Post by balubeto » 24 Jun 2016 11:18

Compo wrote:
balubeto wrote:Right.

So, I wait for a solution.
You will be waiting a long time because you haven't told me which of the two options you require a solution for!


The solution that I want is one in which if I press only the Enter key when the script displays an entry, it continues to display it until I insert an absolute path of a directory.

Thanks

Bye

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: bat script with parameters and personalized message

#25 Post by sambul35 » 24 Jun 2016 11:24

That's exactly what this script does. What error are you getting? :D

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: bat script with parameters and personalized message

#26 Post by balubeto » 25 Jun 2016 10:34

Excuse me, but this script

Code: Select all

@echo off
setlocal EnableDelayedExpansion
echo/ & set ArgCount=0 & for %%i in (%*) do (set /a ArgCount+=1)
if %ArgCount% neq 4 (echo The arguments number is incorrect.) & goto end

:: put your code here

:end
echo/ & echo Exiting program...
timeout /t 10 /nobreak >nul
exit /b


assumes that its arguments are insert directly from the command line.

Now, I would like to create a script like this

Code: Select all

@echo off
set /p Windows_Files_Path="Enter the directory in which put the content of the ^"Windows Setup Media^" volume image:"
set /p iso_Path="Enter the directory in which put the iso image file created:"
set /p esd_File_Path="Enter the directory in which put the esd unencrypted file:"
set /p esd_File="Enter the file to be converted which should be put in the %esd_File_Path% directory:"

:: put your code here

:end
echo/ & echo Exiting program...
timeout /t 10 /nobreak >nul
exit /b


that when it will be run, ask to enter the arguments. How do I check whether the entered values are valid? If they are not valid, the script will have to immediately repeat the relative entry without using the goto command to create a loop.

Thanks

Bye

douglas.swehla
Posts: 75
Joined: 01 Jun 2016 09:25

Re: bat script with parameters and personalized message

#27 Post by douglas.swehla » 25 Jun 2016 12:01

balubeto wrote:Excuse me, but this script

Code: Select all

echo/ & set ArgCount=0 & for %%i in (%*) do (set /a ArgCount+=1)
if %ArgCount% neq 4 (echo The arguments number is incorrect.) & goto end
:end


assumes that its arguments are insert directly from the command line. Now, I would like to create a script like this

Code: Select all

set /p Windows_Files_Path="Enter the directory in which put the content of the ^"Windows Setup Media^" volume image:"
set /p iso_Path="Enter the directory in which put the iso image file created:"
set /p esd_File_Path="Enter the directory in which put the esd unencrypted file:"
set /p esd_File="Enter the file to be converted which should be put in the %esd_File_Path% directory:"


that when it will be run, ask to enter the arguments.

How do I check whether the entered values are valid? If they are not valid, the script will have to immediately repeat the relative entry without using the goto command to create a loop.


Here's what I'm hearing:
  1. You want to be able to accept arguments from the command line.
  2. You want to be able to prompt for missing argument values.
  3. You want to validate the values given, regardless of the source.
  4. If an argument value is invalid, you want to require the user to enter a new value, and repeat until a valid argument is entered.

Each of these things is very easy, but you are adding in some complications for no apparent reason.

To validate file and folder paths, it is usually enough to confirm that they exist, and if not, create them. You are asking to require absolute paths instead of relative, but aren't saying why. While this can be done, it's a bit of a pain, so we want to either understand what your use case is, so we can handle any other related weirdness, or explain to you why you don't need that, and keep the code simple and robust.

To do a repetitive task, you use a loop. If there are a fixed number of iterations, you use the FOR command. If there are an undefined, potentially infinite number of iterations like you are asking for, you use the GOTO command. That's what it's for. Some very competent people have suggested this already, and you are rejecting it without saying why. Before anyone invests time in creating and testing exotic solutions to avoid GOTO, you need to explain why it won't work for you.

So, to get the help you are asking for, please answer these questions:
  • Why do you need absolute instead of relative paths?
  • What are ALL of the conditions that arguments need to meet to be valid?
  • Why do you object to using the GOTO command?

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: bat script with parameters and personalized message

#28 Post by balubeto » 26 Jun 2016 02:29

douglas.swehla wrote:
balubeto wrote:Excuse me, but this script

Code: Select all

echo/ & set ArgCount=0 & for %%i in (%*) do (set /a ArgCount+=1)
if %ArgCount% neq 4 (echo The arguments number is incorrect.) & goto end
:end


assumes that its arguments are insert directly from the command line. Now, I would like to create a script like this

Code: Select all

set /p Windows_Files_Path="Enter the directory in which put the content of the ^"Windows Setup Media^" volume image:"
set /p iso_Path="Enter the directory in which put the iso image file created:"
set /p esd_File_Path="Enter the directory in which put the esd unencrypted file:"
set /p esd_File="Enter the file to be converted which should be put in the %esd_File_Path% directory:"


that when it will be run, ask to enter the arguments.

How do I check whether the entered values are valid? If they are not valid, the script will have to immediately repeat the relative entry without using the goto command to create a loop.


Here's what I'm hearing:
  1. You want to be able to accept arguments from the command line.
  2. You want to be able to prompt for missing argument values.
  3. You want to validate the values given, regardless of the source.
  4. If an argument value is invalid, you want to require the user to enter a new value, and repeat until a valid argument is entered.

Each of these things is very easy, but you are adding in some complications for no apparent reason.

To validate file and folder paths, it is usually enough to confirm that they exist, and if not, create them. You are asking to require absolute paths instead of relative, but aren't saying why. While this can be done, it's a bit of a pain, so we want to either understand what your use case is, so we can handle any other related weirdness, or explain to you why you don't need that, and keep the code simple and robust.

To do a repetitive task, you use a loop. If there are a fixed number of iterations, you use the FOR command. If there are an undefined, potentially infinite number of iterations like you are asking for, you use the GOTO command. That's what it's for. Some very competent people have suggested this already, and you are rejecting it without saying why. Before anyone invests time in creating and testing exotic solutions to avoid GOTO, you need to explain why it won't work for you.

So, to get the help you are asking for, please answer these questions:
  • Why do you need absolute instead of relative paths?
  • What are ALL of the conditions that arguments need to meet to be valid?
  • Why do you object to using the GOTO command?


You understood what I would do.

The user will have to write the absolute path of the directory because this script will have to be placed in a different directory than of work.

The validity of the arguments will have to be compatible with that of symbols allowed by Windows 7 (or higher) for the creation of the file and directory names.

Unfortunately, I realized that the only command, that can create potentially infinite iterative cycles, is the goto command. Right?

So, could you help solve the first two points?

Thanks

Bye

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: bat script with parameters and personalized message

#29 Post by sambul35 » 03 Jul 2016 11:25

balubeto wrote:
The user will have to write the absolute path of the directory because this script will have to be placed in a different directory than of work.

The validity of the arguments will have to be compatible with that of symbols allowed by Windows 7 (or higher) for the creation of the file and directory names.

Thanks

Bye


Check this one out to see if it works for your task. Try making it simpler if needed, and post your code or questions here, after checking command usage rules & examples at http://ss64.com/nt/ . :)

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

Re: bat script with parameters and personalized message

#30 Post by foxidrive » 04 Jul 2016 11:11

balubeto wrote:
foxidrive wrote:You had some typos:
= instead of ==
exit instead of exist

I'm not sure if this is what you are attempting to do:

Code: Select all

@echo off
:again
set "ESD_directory="
set /p "ESD_directory=Enter the directory where the esd unencrypted file will be inserted: "
if not defined ESD_directory goto :again
If not exist "%ESD_directory%\" md "%ESD_directory%"


Does anyone know how to solve my problem in a more suitable form?

Thanks

Bye



I deleted my annoying posts here balubeto.

Please be considerate to any person who spends time to answer one of your questions, even if it doesn't solve your entire problem.

Locked