Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 461 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to read file contents into a variable in a batch file?

#1
This batch file releases a build from TEST to LIVE. I want to add a check constraint in this file that ensures there is an accomanying release document in a specific folder.

"C:\Program Files\Windows Resource Kits\Tools\robocopy.exe" "\\testserver\testapp$"
"\\liveserver\liveapp$" *.* /E /XA:H /PURGE /XO /XD ".svn" /NDL /NC /NS /NP
del "\\liveserver\liveapp$\web.config"
ren "\\liveserver\liveapp$\web.live.config" web.config

So I have a couple of questions about how to achieve this...

1. There is a `version.txt` file in the `\\testserver\testapp$` folder, and the only contents of this file is the build number (for example, 45 - for build 45)
How do I read the contents of the `version.txt` file into a variable in the batch file?

2. How do I check if a file ,`\\fileserver\myapp\releasedocs\ {build}.doc`, exists using the variable from part 1 in place of {build}?
Reply

#2
Read file contents into a variable:

for /f "delims=" %%x in (version.txt) do set Build=%%x

or

set /p Build=<version.txt

Both will act the same with only a single line in the file, for more lines the `for` variant will put the *last* line into the variable, while `set /p` will use the *first.*

Using the variable – just like any other environment variable – it is one, after all:

%Build%

So to check for existence:

if exist \\fileserver\myapp\releasedocs\%Build%.doc ...

Although it may well be that no UNC paths are allowed there. Can't test this right now but keep this in mind.
Reply

#3
just do:

type version.txt
and it will be displayed as if you typed:

set /p Build=<version.txt
echo %Build%
Reply

#4
You can read multiple variables from file like this:

for /f "delims== tokens=1,2" %%G in (param.txt) do set %%G=%%H

where param.txt:

PARAM1=value1
PARAM2=value2
...
Reply

#5
To get all the lines of the file loaded into the variable, Delayed Expansion is needed, so do the following:

SETLOCAL EnableDelayedExpansion

for /f "Tokens=* Delims=" %%x in (version.txt) do set Build=!Build!%%x

There is a problem with some special characters, though especially `;`, `%` and `!`
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through