1.

Solve : Question on SHIFT?

Answer»

Hi,

Was quite fresh in DOS.
Fyi I am using Window XP.

Having a prob to LEARN the shift command.

I have read up a few example on shift, but i just can't get it and utilise it on my own.
Is there anyone can show an simple example to explain how it works?
It will be better if i can paste the command and try it myself.

Thanks and will wait for anyone replies...

The SHIFT command will shift command line switches, or parameters down (line an assembly POP) to let you process in a loop, or access parameters higher than %9. Do you understand %1 and %2, etc.? Use the following example:
Code: [SELECT]@echo off
echo Command line is "%0 %*"
echo Now Param 1 is "%1"
echo Shifting
shift
echo Now Param 1 is "%1"
echo Shifting again
shift
echo Now Param 1 is "%1"
echo Shifting again
shift
echo Now Param 1 is "%1"
echo Shifting again
shift
echo Now Param 1 is "%1"
echo Shifting again
shift
echo Now Param 1 is "%1"
echo Shifting again
shift
echo Now Param 1 is "%1"
echo Shifting again
shift
echo Now Param 1 is "%1"
Save the file as foo.bat and execute with parameters like:
foo.bat This is a test for the Shift command

Each time a SHIFT command is executed the parameters shift down by 1, so %1 goes away, and %2 becomes %1, %3 becomes %2, etc.

Does that explain?Hi, thx for your prompt reply,

but just another question, as i am not quite certain about the below:

"Save the file as foo.bat and execute with parameters like:
foo.bat This is a test for the Shift command"

I have save the foo.bat files with the above code inside, but how can i put in the parameters "This is a test for the Shift command"?

Please let me know again, thanks in advance.
Sorry, one more thing, i am not sure about the %1 and %2, etc.

I only know %%i in For command.

Thanks.Execute the command with the extra WORDS at the end. For example, if you saved the file as foo.bat, to execute script from the command prompt, type:
foo.bat This is a test for the Shift command

The output should look similar to:
Quote

C:\>foo.bat This is a test for the Shift command
Command line is "foo.bat This is a test for the Shift command"
Now Param 1 is "This"
Shifting
Now Param 1 is "is"
Shifting again
Now Param 1 is "a"
Shifting again
Now Param 1 is "test"
Shifting again
Now Param 1 is "for"
Shifting again
Now Param 1 is "the"
Shifting again
Now Param 1 is "Shift"
Shifting again
Now Param 1 is "command"

C:\>

Parameters are what you type in after the command. %1 is the first parameter, %2 is the second, parameter, etc.. So when you first RUN the command with "This is a test for the Shift command", then: %1="This", %2="is" %3="a", %4="test" ... Each time you execute the "shift" command from within the batch file, the parameters shift down one number. So after the first shift command it is: %1="is" %2="a", %3="test".

Is that more clear now?Thanks for the example, its works now.

But can the SHIFT function function within a single batch file alone?
Can the parameters be obtained from a text file instead of keying them in cmd prompt?

Possible to give another example?

Thanks =)You could have one batch file call another. For example, if we keep our original batch file as foo.bat, we could have test.bat with the following code:
Code: [Select]@echo off
echo Testing the SHIFT command
call foo.bat This is another test for the shift command

That way you have not provided any parameters, but the Shift command is still used.

Is that what you were asking?Yup, more or less like this.
That mean the baseline is it only can work only when 2 bat files are used?
My initial THOUGHT is actually by reading the parameters from a text file when the .bat file run and auto asign each parameter into %1, %2, %3, ...

Thanks for your time!




Discussion

No Comment Found