How to create a. BAT configuration file in windows?

Asked

Viewed 9,015 times

4

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

get bat file.:

@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 the . bat so:

get winscp.conf

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

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

I get it:

%appData%\winscp

In short: I would like the %appData% to be interpreted but instead I am getting the literal value.

Anyone can help?

  • 1

    Very clever, huh. Ever thought of using that creativity with the Powershell? It is much more powerful and simpler. Now my question: what is the problem in getting %appData%\winscp value of the variable since this is a valid path for you to read and write files along the script?

  • The problem is that if I try to run something like mkdir %Folder% instead of creating the folder using the path of the environment variable it creates a folder relative to .bat. it creates a folder named %appData%. I’m thinking about doing this with Cli PHP or Nodejs, I thought it would be simpler with a file. bat

  • It has VBS option, too. The advantage of . BAT, Powershell and VBS is that they are native to Windows, do not need any additional tool to run. Anyway it’s weird. I tested it now set teste=%appdata%\teste followed by mkdir %teste% and the folder has been created correctly C:\Users\Caffé\AppData\Roaming\teste.

  • Yes it creates normally if I’ve been in the same file. bat, the problem is when I search the file. conf, if you can and will, try to recreate my password with the file . bat and the file . conf.

  • Really curious... I answered the question in Soen. http://stackoverflow.com/questions/31592509/how-to-set-a-variable-with-an-environment-variable-read-from-a-text-file Follow there.

  • Problem solved in Soen :-) Updated answer.

Show 1 more comment

1 answer

2


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

My configuration file is itself a file. BAT, more or less like this (file Setup_prod.bat):

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 parameter for my scripts. For example:

call AtualizaBanco.bat Setup_Prod.bat

In command above, "Setup_prod.bat" is a parameter, even a string, passed to the script Bank update.bat.

The first thing that the file Bank update.bat should do is use the configuration file to set the variables. Then on the first line of the Bank update.bat I have:

call %1%

In the line above I am calling as a script the first parameter received.

Now, in the file Bank update.bat, i consume the variables, example:

copy %HOME%\*.sql

Completion

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

Your problem probably lies in how you arrow the variables within the loop. Using this technique I proposed you eliminate the loop by eliminating the problem.

I reinforce however my suggestion to use Windows Powershell or Visual Basic Script (VBS) to solve this type of problem (preferably Powershell), as these are more powerful tools and provide simpler, expressive, and easy-to-write code.

Edit: fixing your code

As answers obtained on Soen indicate the solution of the problem:

Instead of:

set %%A=%%B

Do:

call set %%A=%%B

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

  • Thanks for the tip, I really believe I need something more powerful, I will analyze the alternatives. Anyway, thanks for the attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.