How to create a .BAT configuration file in windows?

4

I am automating some processes on a Windows server, and I would like to create some .bat files for this, but for that it would be necessary for the .BAT to be read from a configuration file so I did the following:

file get.bat:

@echo off
setlocal
@if not exist "%1" (
    echo Config file not found in "%1"
    exit /B
)

@for /f "tokens=1,2 delims= " %%A in (%1) do (
    set %%A=%%B
)

echo %folder%

and created a configuration file called winscp.conf with the following data:

folder    %appData%\winscp
version   5.7.4
visit     http://sourceforge.net/projects/winscp/files/WinSCP/
download  http://sourceforge.net/projects/winscp/files/WinSCP/5.7.4/winscp574.zip

and then I call .bat like this:

get winscp.conf

So far, the variable is read and created exactly as I would like it to be, but this is in the use of environment variables, I would like the app data to be interpreted and stored in the variable but instead of having something like this

C:\Users\root\AppData\Roaming\winscp

I get this:

%appData%\winscp

In short: I'd like% appData% to be interpreted but instead I'm getting the literal value.

Can anyone help?

    
asked by anonymous 23.07.2015 / 15:13

1 answer

2

Although I find your solution to be creative, I use another technique to have a sort of common configuration file for my batch files .BAT (or batch files):

My configuration file is itself a .BAT file, like this (Setup_Prod.bat file):

set HOME=\servidor\pasta_prod
set ENV=PROD
set DB=dabase_producao
set PASSWORD=senha_producao

So I have different configuration files for different environments, and I pass the configuration file as a parameter to my scripts. For example:

call AtualizaBanco.bat Setup_Prod.bat

In the above command, "Setup_Prod.bat" is a parameter, a string itself, passed to the UpdateBank.bat script.

The first thing the UpdateBank.bat file should do is to use the configuration file to set the variables. So in the first line of UpdateBanco.bat I have:

call %1%

In the above line I'm invoking the first received parameter as a script.

Now, in the UpdateBank.bat file, I consume the variables, eg:

copy %HOME%\*.sql

Conclusion

Instead of looping at the beginning of the main script by setting the variables according to the configuration file, I make the configuration file itself a .BAT batch file, and I invoke this configuration .BAT at the beginning of my main script.

Your problem probably lies in the way you arrow variables inside the loop. Using this technique I propose eliminates the loop, eliminating the problem.

However, I strongly suggest using Windows Powershell or Visual Basic Script (VBS) to resolve this type of problem (preferably Powershell) because these are tools more powerful, and provide a simpler, more expressive, and easier to write code.

Edit: correcting your code

answers obtained in SOen indicate the solution to the problem:

Instead of:

set %%A=%%B

Make:

call set %%A=%%B

The call causes the variable %appData% to be expanded before its value is used.

    
23.07.2015 / 15:47