Using a BAT file in Windows is as simple as double-clicking or double-tapping it. You don't need to download any special program or tool. To use the first example from above, entering that text into a text file with a text editor and then saving the file with the .
By default when a command line execution is completed it should either return zero when execution succeeds or non-zero when execution fails. When a batch script returns a non-zero value after the execution fails, the non-zero value will indicate what is the error number.
You can run findstr from the command line or as a batch file. Open a new command line prompt by clicking on the Windows-key, typing cmd.exe, and selecting the result. Alternatively, use the Run command to open findstr.
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.
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
...
just do:
type version.txt
and it will be displayed as if you typed:
set /p Build=<version.txt
echo %Build%
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 !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With