1

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?

JoshMc
  • 10,239
  • 2
  • 19
  • 38
Decim
  • 53
  • 1
  • 7
  • 3
    There is just `%1` to `%9`. To use more parameters, use [shift](https://ss64.com/nt/shift.html) – Stephan Aug 10 '17 at 10:16
  • There is absolutely no need to pass the variables via command line arguments. The called batch shares the same environment and can use the vars directly. –  Aug 10 '17 at 12:28
  • You may pass _the name_ of the array in the parameter (instead of all their values): `call arg_batch2.bat file_var` and then: `setlocal EnableDelayedExpansion` and `set argv=%1` and `set arg1=!%argv%1!`, and `set arg2=!%argv%2!` etc... See [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Aug 10 '17 at 15:16
  • @Stephan Thanks, I'll take a look at this method – Decim Aug 15 '17 at 08:07
  • @LotPings This method allowed me to display my expected variables. Thank you – Decim Aug 15 '17 at 08:08
  • @Aacini Hmm, making use of the setlocal EnableDelayedExpansion, I never thought of it, Thanks I'll take a look at it. – Decim Aug 15 '17 at 08:08
  • Setlocal will restrict changes to the environment to the scope of the current batch. Sharing the same environment allows to use changed vars back in the calling batch if that matters. –  Aug 15 '17 at 08:16
  • @LotPings That's the clarification I need, I'll keep that in mind when I do my alter my batch files. Thank you very much! :D – Decim Aug 15 '17 at 08:34
  • Why are there random `{}`? I don't think they work in a batch file. –  Aug 16 '17 at 10:17
  • @SteveFest Oh, please ignore that. I'm new to stackoverflow and I thought that {} are need to be included to write codes in stackoverflow. – Decim Aug 17 '17 at 01:39
  • Maybe you can remove the `{}` by pressing the edit button? –  Aug 17 '17 at 04:37

1 Answers1

1

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

JoshMc
  • 10,239
  • 2
  • 19
  • 38