-1

The reference at ss64.com clearly mentions that the call command can call an internal command whereas the windows xp command reference doesn't mention it all. Even the old MS DOS command reference doesn't mention it. This was the reason why I found this syntax confusing :

call set X= %X% This is an elegant way of expanding the user input environment variable as compared to using the parsing capability of the for command to get the expanded value of the environment variable. Why does the command processor have to read the input variable twice to expand it ? If what the user input is entered directly the command using it works without any problem. For eg. "%userprofile%\desktop\file.txt" is a no brainer for the processor when typed directly but when the same string is input via the prompt the system is lost. When the input variable has no spaces or special characters there's no need to call it,it can be used directly.

C_urious' '
  • 101
  • 2
  • 5

2 Answers2

1

Your confusion comes from the fact that call set X=%X% is a replacement of set X=!%X%!, not of any for feature. The purpose is to expand a variable value twice. A simple example:

set /P "str=Enter a string: "
set /P "start=Enter start char: "
set /P "len=Enter number of chars: "

echo The substring is: "!str:~%start%,%len%!"

call echo The substring is: "%%str:~%start%,%len%%%"

Its easy to realize that there is no way to get the substring defined by variables without expand the value two times: one for the start/len values and another one for the substring itself.

The same happen with your path: if the value read include any %variable%, there is no way to get the full path without expanding it twice!

The key here is that these values COMES FROM USER INPUT, that is different than put they in a Batch file or enter they in the command prompt.

EDIT: Another example; execute it from the command prompt:

C:\> set var=

C:\> set command=echo %var%

C:\> echo %command%
echo %var%

C:\> %command%
%var%

C:\> set var=This is var value

C:\> %command%
%var%

C:\> call %command%
This is var value
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • First off thank you for your patience and help.Here's where I was going with it. – C_urious' ' Jul 23 '15 at 08:45
  • REM the user inputs a string which shows the full path to his file where he wants the sytem perform operations REM to give the result/results he seeks.In this case he seeks to know the number of lines/strings in his chosen text file. REM We start off by asking him to input the string at the prompt. `set /p X=please type the name of your text file appended to its full path.Like for eg. if the file is on your desktop. Type "C:\Documents and Settings\Your name\Desktop\file.txt"` – C_urious' ' Jul 23 '15 at 08:48
  • REM Now life would be easy if he were to type exactly this. But what if he were to type "%userprofile%\desktop\file.txt" ? We have to make allowance for his love of special characters or our batch will disappoint him. REM Here's how. When he inputs his string with his syntax it has to be expanded to the form I gave as an example, for it to be used in subsequent commands successfully. REM The trick is in capturing his input from the console in an expanded form to a varaiable.We use the following for the same – C_urious' ' Jul 23 '15 at 08:49
  • `for /f "tokens=*" %I in ('echo %X%') do set X =%I` REM Here we have used the parsing capability of the FOR command to parse command outputs to do our job.The output of the echo command is the string input by the user(in a compressed form). REM When the FOR command sees it ,it automatically 'translates' it to what you see in your explorer bar if you have enabled full paths therein. `find / v /c "" < %X%` REM This will give us the number of lines in the text file input as "%userprofile%\desktop\file.txt whose uncompressed form is "C:\ Documents and Settings\Username\desktop\file.txt" – C_urious' ' Jul 23 '15 at 08:50
  • The code without comments: `set /p X=please type the name of your text file appended to its full path.Like for eg. if the file is on your desktop. Type "C:\Documents and Settings\Your name\Desktop\file.txt" for /f "tokens=*" %I in ('echo %X%') do set X =%I find / v /c "" < %X%` – C_urious' ' Jul 23 '15 at 08:55
  • oops! you need to replace %I with %%I since we are referring from a batch file. ` set /p X=please type the name of your text file appended to its full path.Like for eg. if the file is on your desktop. Type "C:\Documents and Settings\Your name\Desktop\file.txt" for /f "tokens=*" %%I in ('echo %X%') do set X=%%I find /v /c "" < %X% pause` – C_urious' ' Jul 23 '15 at 09:19
0

The usage of call in the indicated scenarios simply force a double pass of the batch parser over the command on the right of the call. In a simplified sequence (here the full process)

  • The first pass will read the line, replace the variables with the values inside them and the command is converted to the batch internal representation. As the command to execute is call, the text (it has not been parsed to be identified as a command) after the call is inserted in the current batch context as a new command.

  • Now, to execute the inserted command, it needs to be parsed again (the second pass) to generate the internal representation of the new command.

If the first parser pass generated a variable reference, the second pass will replace this reference with the value in the variable.

Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126