y.mor
y.mor

Reputation: 1

read path from a .txt file in a batch file

my batch file starts a program and direct it to the location of the data file. i would like that instead of writing the actual path for the data file (shown below), that the batch file would receive it from a .txt file.

my batch file does:

set appPath=c:/aplication folder
set dbPath=c:/somewhere/data place/programeDb

my .txt file is in a fixed place (c:/aplication folder) and has one line (that can change)

c:/somewhere/data place/programeDb

thanks a lot

Upvotes: 0

Views: 3285

Answers (1)

Mofi
Mofi

Reputation: 49216

Command FOR can be used to read a line from a text file and assign it to an environment variable.

@echo off
for /F "usebackq delims=" %%L in ("Name of text file with path.txt") do set "DataPath=%%L"
set "DataPath=%DataPath:/=\%"
echo Application path is: %DataPath%

This batch file replaces also all slashes by backslashes as on Windows the backslash is the directory separator and not the slash as on Unix, Linux, or Mac.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • for /?
  • set /?

Upvotes: 1

Related Questions