Decim
Decim

Reputation: 53

Questions regarding passing variables from one batch file to another

I do know there is a similar topic on this however this is slightly different. I have read online that there are ways to pass variables using the %~ I have many variables I would like to pass for instance:

BATCH FILE 1:

@echo off


set file_var1=world
set file_var2=%computername%
set file_var3=hehexd
set file_var4=yolo
set file_var5=h
set file_var6=he
set file_var7=heh
set file_var8=hehe
set file_var9=hehex
set file_var10=hehexd
set file_var11=hehexd1
set file_var12=hehexd12
set file_var13=hehexd123
set file_var14=hehexd1234
set file_var15=hehexd12345
set file_var16=hehexd123456
set file_var17=hehexd1234567
set file_var18=hehexd12345678
set file_var19=hehexd123456789
set file_var20=hehexd1234567890



call arg_batch2.bat %file_var1% %file_var2% %file_var3% %file_var4% %file_var5% %file_var6% %file_var7% %file_var8% %file_var9% %file_var10% %file_var11%  

BATCH FILE 2:

@echo off
set arg1=%~1
set arg2=%~2
set arg11="%~11"

echo Hello, %arg1% AND %arg2% ! My name is %arg11%.
PAUSE

I am expected to have the result of hehexd1. However I realise that the result was world1. Is there any way I can fix this?

Upvotes: 1

Views: 63

Answers (1)

JoshMc
JoshMc

Reputation: 10662

To answer the question on why the result you received was world1 instead of hehexd1, the following line is not setting arg11 to the 11th argument passed to arg_batch2.bat it is setting it to the 1st argument followed by the character 1. This is because as mentioned by @Stephan there are only nine arguments to reference unless you shift them over.

set arg11="%~11"

Since the first arg is world, the result is world + 1 = world1

Upvotes: 1

Related Questions