bat script with parameters and personalized message
Moderator: DosItHelp
bat script with parameters and personalized message
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
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
Re: bat script with parameters and personalized message
Post the bat you created; with the example parameters and the method you use to enter those parameters by example.
Re: bat script with parameters and personalized message
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
Re: bat script with parameters and personalized message
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.
Re: bat script with parameters and personalized message
I would use something similar to this:
Just bear in mind that his only checks the number, not the correctness, of parameters.
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.
Re: bat script with parameters and personalized message
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?
Re: bat script with parameters and personalized message
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." Note: this is an innocent question, don't read more into it than its verbatim.
Re: bat script with parameters and personalized message
balubeto wrote:So, how do I do this?
Yet another headache test for a batch with 4 expected arguments (args beyond 4th are ignored):
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
Re: bat script with parameters and personalized message
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):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.
Re: bat script with parameters and personalized message
sambul35 wrote:If your batch is supposed to have 4 arguments
Code: Select all
if "%~4"=="" echo Feed me more parameters! & pause & goto :eof
Re: bat script with parameters and personalized message
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):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
Re: bat script with parameters and personalized message
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
Re: bat script with parameters and personalized message
I wrote this script
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
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
Re: bat script with parameters and personalized message
You had some typos:
= instead of ==
exit instead of exist
I'm not sure if this is what you are attempting to do:
= 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%"
Re: bat script with parameters and personalized message
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