bat script with parameters and personalized message

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

bat script with parameters and personalized message

#1 Post by balubeto » 21 Jun 2016 10:43

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
Posts: 599
Joined: 21 Mar 2014 08:50

Re: bat script with parameters and personalized message

#2 Post by Compo » 21 Jun 2016 10:52

Post the bat you created; with the example parameters and the method you use to enter those parameters by example.

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

Re: bat script with parameters and personalized message

#3 Post by balubeto » 21 Jun 2016 11:25

Compo wrote:Post the bat you created; with the example parameters and the method you use to enter those parameters by example.


For example, this script

Code: Select all

@echo off

md "C:\Users\Public\Documents\Windows_DVD_creation\Windows_files"

md "C:\Users\Public\Documents\Windows_DVD_creation\Modified_iso"

md "C:\Users\Public\Documents\Windows_DVD_creation\Esd_files"

echo Put the esd unencrypted files in the C:\Users\Public\Documents\Windows_DVD_creation\Esd_files directory.

pause

Dism /Apply-Image /ImageFile:"C:\Users\Public\Documents\Windows_DVD_creation\Esd_files\%1" /Index:1 /ApplyDir:"C:\Users\Public\Documents\Windows_DVD_creation\Windows_files" /Verify /CheckIntegrity

Dism /Export-image /SourceImageFile:"C:\Users\Public\Documents\Windows_DVD_creation\Esd_files\%1" /SourceIndex:2 /DestinationImageFile:"C:\Users\Public\Documents\Windows_DVD_creation\Windows_files\sources\boot.wim" /Compress:max /Bootable /CheckIntegrity

Dism /Export-image /SourceImageFile:"C:\Users\Public\Documents\Windows_DVD_creation\Esd_files\%1" /SourceIndex:3 /DestinationImageFile:"C:\Users\Public\Documents\Windows_DVD_creation\Windows_files\sources\boot.wim" /Compress:max /Bootable /CheckIntegrity

Dism /Export-image /SourceImageFile:"C:\Users\Public\Documents\Windows_DVD_creation\Esd_files\%1" /SourceIndex:4 /DestinationImageFile:"C:\Users\Public\Documents\Windows_DVD_creation\Windows_files\sources\install.esd" /Compress:Recovery /CheckIntegrity

rem del "C:\Users\Public\Documents\Windows_DVD_creation\Esd_files\%1"

oscdimg -o -u2 -udfver102 -l"%2" -t%3,00:00:00 -bootdata:2#p0,e,b"C:\Users\Public\Documents\Windows_DVD_creation\Windows_files\boot\etfsboot.com"#pEF,e,b"C:\Users\Public\Documents\Windows_DVD_creation\Windows_files\efi\microsoft\boot\efisys.bin" "C:\Users\Public\Documents\Windows_DVD_creation\Windows_files" "C:\Users\Public\Documents\Windows_DVD_creation\Modified_iso\%4"

pushd "C:\Users\Public\Documents\Windows_DVD_creation\Windows_files" && ( rd /S /Q "C:\Users\Public\Documents\Windows_DVD_creation\Windows_files" 2>nul & popd )

rd "C:\Users\Public\Documents\Windows_DVD_creation\Windows_files"


must be executed typing on the command line:

Code: Select all

Convert_esd_unencrypted_to_iso.bat Install_build_14367.esd itWin10Entb14367x64 06/19/2016 it_Windows_10_Enterprise_build_14367_x64.iso


Thanks

Bye

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

Re: bat script with parameters and personalized message

#4 Post by sambul35 » 21 Jun 2016 12:40

If your batch is supposed to have 4 arguments, try this to check if all 4 are entered, without validating if they're correct:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /F "tokens=1-4" %%k in ("%*") do (
  if "%%k"=="" (echo/ & echo Invalid Batch Arguments & goto :end))

:: put your code here

:end
echo/ & echo Exiting program...
timeout /t 10 /nobreak >nul
exit /b
Last edited by sambul35 on 21 Jun 2016 18:08, edited 1 time in total.

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

Re: bat script with parameters and personalized message

#5 Post by Compo » 21 Jun 2016 14:33

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.

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

Re: bat script with parameters and personalized message

#6 Post by Squashman » 21 Jun 2016 14:37

sambul35 wrote:If your batch is supposed to have 4 arguments, try this to check if all 4 are entered, without validating if they're correct:

Code: Select all

@echo off
for /F "tokens=1-4" %%k in ("%*") do (
  if "%%k"=="" (echo/ & echo Invalid Batch Arguments & goto :end))

:: put your code here

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

Did you even test this? :?:

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

Re: bat script with parameters and personalized message

#7 Post by sambul35 » 21 Jun 2016 18:13

Squashman wrote:Did you even test this?

No, its offered to read the Help link and TRY relevant approach ideas. I believe, finding errors in someone's or your own script is a desirable part of learning process. :!:

Now try answering my question: "When was the last time you posted YOUR OWN script anywhere on the web? Pls provide a link to it." :mrgreen: Note: this is an innocent question, don't read more into it than its verbatim. :cry:

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

Re: bat script with parameters and personalized message

#8 Post by sambul35 » 21 Jun 2016 20:40

balubeto wrote:So, how do I do this?

Yet another headache test for a batch with 4 expected arguments (args beyond 4th are ignored): :D

Code: Select all

@echo off
setlocal EnableDelayedExpansion
echo/ & set "a=1" & for %%i in (%*) do (set /a a+=1)
if !a! leq 4 (echo Low arguments count)

:: put your code here

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

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

Re: bat script with parameters and personalized message

#9 Post by balubeto » 22 Jun 2016 03:47

sambul35 wrote:
balubeto wrote:So, how do I do this?

Yet another headache test for a batch with 4 expected arguments (args beyond 4th are ignored): :D

Code: Select all

@echo off
setlocal EnableDelayedExpansion
echo/ & set "a=1" & for %%i in (%*) do (set /a a+=1)
if !a! leq 4 (echo Low arguments count)

:: put your code here

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


Or better:

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


What is the echo\ command?

Now, I would like to know how do I make sure that the script asks the arguments and that it prevents the entry of null values.

In addition, I would like that this script checks for the existence of the directories or files placed and that, if these elements do not exist, the entry will be required.

Thanks

Bye
Last edited by balubeto on 22 Jun 2016 11:49, edited 1 time in total.

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

Re: bat script with parameters and personalized message

#10 Post by foxidrive » 22 Jun 2016 05:22

sambul35 wrote:If your batch is supposed to have 4 arguments


Code: Select all

if "%~4"=="" echo Feed me more parameters! & pause & goto :eof

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

Re: bat script with parameters and personalized message

#11 Post by balubeto » 23 Jun 2016 08:40

balubeto wrote:
sambul35 wrote:
balubeto wrote:So, how do I do this?

Yet another headache test for a batch with 4 expected arguments (args beyond 4th are ignored): :D

Code: Select all

@echo off
setlocal EnableDelayedExpansion
echo/ & set "a=1" & for %%i in (%*) do (set /a a+=1)
if !a! leq 4 (echo Low arguments count)

:: put your code here

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


Or better:

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


What is the echo\ command?

Now, I would like to know how do I make sure that the script asks the arguments and that it prevents the entry of null values.

In addition, I would like that this script checks for the existence of the directories or files placed and that, if these elements do not exist, the entry will be required.

Thanks

Bye


Someone wants to help me again?

Thanks

Bye

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

Re: bat script with parameters and personalized message

#12 Post by foxidrive » 23 Jun 2016 12:55

balubeto wrote:Now, I would like to know how do I make sure that the script asks the arguments and that it prevents the entry of null values.

In addition, I would like that this script checks for the existence of the directories or files placed and that, if these elements do not exist, the entry will be required.

Someone wants to help me again?

Thanks

Bye


If you need more specific code than shown below then make your questions clearer in what you need to do.
Showing the code is also valuable so we can see what your problem is.

Code: Select all

   rem detect if the folder exists
if not exist "c:\folder\" echo The folder doesn't exist
   rem or just create the folder if it doesn't exist
if not exist "c:\folder\" md "c:\folder"

   rem detect if a file exists
if not exist "c:\folder\your file.txt" echo The file is missing

   rem the line of code in my previous reply detects if any of the 4 input parameters are missing
if "%~4"=="" echo Feed me more parameters! & pause & goto :eof

   rem it does that by checking if the 4th entry is present
   rem and if that one isn't present then less than 4 parameters have been entered = not enough      

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

Re: bat script with parameters and personalized message

#13 Post by balubeto » 24 Jun 2016 03:12

I wrote this script

Code: Select all

@echo off

set /p ESD_directory="Enter the directory where the esd unencrypted file will be inserted:"


for %%i in (%ESD_directory%) do (
    if %%i="" set /p ESD_directory="Enter the directory where the esd unencrypted file will be inserted:"
)

If not exit "%ESD_directory%\" md %ESD_directory%


but if I press Enter at the first entry, this script does not repeat this entry. Why?

Also, how do I make sure that the ESD_directory variable takes only absolute paths?

Thanks

Bye

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

Re: bat script with parameters and personalized message

#14 Post by foxidrive » 24 Jun 2016 05:34

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

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

Re: bat script with parameters and personalized message

#15 Post by balubeto » 24 Jun 2016 08:53

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

Locked