0

Operating system is Windows 10. My batch file code is:

@echo off
:start
REM check if there are more then one arguments
if not "%2" == "" (
    echo Too many parameters entered 
) ELSE (
    REM check if argument one is empty
    if "%1"=="" (
        ECHO Enter File Name Your want to edit
        SET /P name=
        ECHO Your Name  is %name%
    ) 
) 

I am not getting code working in Set /p name= section.

On first run code works fine.

C:\Users\Ahmad khan\Desktop\work>test.bat
Enter File Name Your want to edit
asd
Your Name  is asd

But when run the code again, the input section doesn't work.

Enter File Name Your want to edit
arslan
Your Name  is asd

You can see on second run that I entered name Arslan, but displayed is asd as entered on first run.

The same happens on third run showing the name entered on second execution.

Enter File Name Your want to edit
qqqqq
Your Name  is arslan
Mofi
  • 46,139
  • 17
  • 80
  • 143
Arslan Ahmad khan
  • 5,426
  • 1
  • 27
  • 33
  • As the real reason is missing [delaydexpansion](http://ss64.com/nt/delayedexpansion.html) I think your 1st run was the 2nd with same values. –  Apr 20 '19 at 16:28
  • @ArslanAhmad, if you were to visit your previous duplicate question, you'd note that it has already received an answer. Please do not ask the same question more than once, have patience and wait for responses, more especially during the weekends. – Compo Apr 20 '19 at 19:31

2 Answers2

2

Your problem comes from the fact batch files evaluate commands in IF blocks at the same time. That was outlined in this Q/A. Here are some edits I brought to your code:

@echo off
:start
REM check if there are more then one argumnets
if not "%2" == "" (
echo Too many parameters entered 
) ELSE (
REM check if argument one is empty
if "%1"=="" (
ECHO Enter File Name Your want to edit
SET /P name=
goto echoname
) 
) 

:echoname
ECHO Your Name  is %name%

Now it waits for the input before displaying the variable name.

0

I suggest this code:

@echo off
rem Is there specified more than one argument?
if not "%~2" == "" goto ErrorArgCount
rem Is there specified as second argument just " or just ""?
set SecondArgument=%2
if not defined SecondArgumentgoto CheckFirstArg
set "SecondArgument="

:ErrorArgCount
echo ERROR: Too many parameters used on calling "%~nx0".
echo/
pause
goto :EOF

:CheckFirstArg
setlocal EnableExtensions DisableDelayedExpansion
if not "%~1" == "" set "FileName=%~1" & goto ProcessFile

:Begin
set "FileName="
set /P "FileName=File name: "
rem Has the user entered anything at all?
if not defined FileName goto Begin
rem Remove all double quotes from file name.
set "FileName=%FileName:"=%"
rem Has the user entered anything else than double quotes?
if not defined FileName goto Begin

:ProcessFile
echo Continue "%~nx0" with file "%FileName%" ...
echo/
pause

endlocal

The batch file first checks if it was called with more than one argument. The first IF condition is for typical arguments like Argument2 or "Argument 2".

But a user could run the batch file also with the arguments list "File Name" "" "third argument" or with FileName " on which first IF condition removing surrounding double quotes from second argument fails to detect the empty second argument string. For that reason the second argument string is assigned to an environment variable which is not defined anymore if there is really no second argument string. Otherwise environment variable SecondArgument is defined with string value " or "" which results in second IF condition being false and therefore running into error code for too many arguments.

Beginners in batch file writing should read carefully How does the Windows Command Interpreter (CMD.EXE) parse scripts?

It is most important to know how cmd.exe processes environment variable references using syntax %variable%, especially on using immediately expanded environment variables within a command block starting with ( and ending with ). Such a command block is parsed by Windows command processor completely with replacing all %variable% variable references by current values of the referenced environment variables before the command block is used at all. Command blocks are used usually with the commands IF, ELSE and FOR. Every environment variable defined or modified inside a command block must be referenced inside the command block using delayed expansion or the code is not working as expected by the writer of the batch file. The help/documentation output on running in a command prompt window set /? explains when and how to use delayed environment variable expansion on an IF and a FOR example.

It is good practice on batch file writing to avoid command blocks where it is easy possible by writing the code in a simple top to down manner with execution branches done using command GOTO.

There is the command START as it can be seen on running in a command prompt window help or help start or start /?. For that reason it is not good to use as label the string start. It is possible, but it is not advisable as it makes it difficult to search for start being interpreted as command in comparison to start being used as label. For that reason the code above uses Begin as label which is not a Windows command as documented by Microsoft on page Windows Commands as well as and even better by SS64 on SS64.com - A-Z index of the Windows CMD command line.

It is necessary to take care on prompting a user for an input with set /P because the user has the freedom to enter nothing at all which means the environment variable is still not defined and not being defined before or keeps its value on being already defined. The user can also enter by mistake or intentionally a string which could result in executing something for which the batch file is not written at all or Windows command processor exits batch file processing because of a syntax error caused by user input and not good written batch file code. Please read my answer on very detailed How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?

See also single line with multiple commands using Windows batch file for an explanation of operator & used in batch code above to execute two commands specified on one command line.

Mofi
  • 46,139
  • 17
  • 80
  • 143