0

I am trying to create batch script which can take multiple inputs from user & then create file & save all the inputs in that file. Below is the code i created but it id not working. Can you please help me.

@echo off   
set /P inst=Enter number of installation:    
set /A ha_inst=%inst%    
FOR /L %%i IN (1,1,%ha_inst%) DO (     
    set /P hostname= Enter host name:   
    set /P sid=Enter SID:    
    echo. >C:\Users\smnadm\Desktop\hdbinst.cfg_%%i   
    (    
        echo # Local Host Name     
        echo hostname=%hostname%    
        echo # SAP HANA System ID    
        echo sid=%sid%    
    ) >C:\Users\smnadm\Desktop\%hdbinst.cfg_%%i%   
)    

Thanks

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
hans
  • 41
  • 1
  • 10
  • 2
    What is the error or issue that you are facing? – Bilal Ahmad Aug 08 '17 at 11:24
  • 1
    Telling us "it doesn't work" doesn't help us help you troubleshoot it. You need to explain _how_ it fails, including the exact text of any error messages. You should also explain what steps you have taken to analyze the error yourself, and what your findings are. – Jeff Zeitlin Aug 08 '17 at 11:26
  • @BilalFarooqAhmad - he's main error is that he is using sap products. – npocmaka Aug 08 '17 at 11:31
  • this codes created multiple files but does not write input (hostname & SID) in it & that is the issue. I want this code to create multiple files based on the value of ha_inst & write the hostname & SID value in individual files. – hans Aug 08 '17 at 11:47
  • 1
    you need [delayed expansion](https://stackoverflow.com/a/30284028/2152082) – Stephan Aug 08 '17 at 13:17

1 Answers1

0

In last line of code you write into the file

 C:\Users\smnadm\Desktop\%hdbinst.cfg_%%i%

However the variable %hdbinst.cfg_% has never been set in your code. You might want to use hdbinst.cfg_%%i instead?

The following code works for me:

@echo off 
setlocal ENABLEDELAYEDEXPANSION  
set /P inst=Enter number of installation:    
set /A ha_inst=%inst%    
FOR /L %%i IN (1,1,%ha_inst%) DO (     
    set /P hostname=Enter host name:   
    set /P sid=Enter SID:    
    echo. >C:\temp\hdbinst.cfg_%%i   
    echo # Local Host Name     >>C:\temp\hdbinst.cfg_%%i
    echo hostname=!hostname!   >>C:\temp\hdbinst.cfg_%%i 
    echo # SAP HANA System ID  >>C:\temp\hdbinst.cfg_%%i  
    echo sid=!sid!             >>C:\temp\hdbinst.cfg_%%i

) 

writing in the Directory c:\temp however.

Andre Ruebel
  • 518
  • 3
  • 12
  • Thank you for your reply. I used below two syntex as well but it did not work. >%C:\Users\smnadm\Desktop\hdbinst.cfg_%%i% >C:\Users\smnadm\Desktop\%hdbinst.cfg_%%i – hans Aug 08 '17 at 13:30
  • I added a working example to my answer. You have to change the path accordingly. – Andre Ruebel Aug 08 '17 at 13:36
  • It worked for me as well. Can you please let me know what is the difference b/w below two syntex !hostname! or %hostname% which should be used to read input value from user – hans Aug 09 '17 at 09:05
  • The Syntax with the ! (exclamation mark) is only available if ENABLEDELAYEDEXPANSION is defined. Read the help text in the command window for the "set" command for further details. This help text includes also an example. Also the link given by Stephan in his comment to your question above gives some background information. – Andre Ruebel Aug 10 '17 at 09:27