I have the path to a text file stored in a variable like so:
set readfile="%USERPROFILE%\Utilities\list_2.txt"
I use a for loop to read it like this:
FOR /F "tokens=1" %%i IN (%readfile%) DO
The problem is, it doesn't read the file, it skips the for loop altogether. The only way it can read the file is if I plug in the path directly, no variable in it's place. I've tried !readfile! as well, but it doesn't work, same result.
FOR /F "usebackqtokens=1" %%i IN (%readfile%) DO
or
FOR /F "tokens=1" %%i IN ('type %readfile%') DO
should fix your problem.
See for /?|more from the prompt for documentation.
Related
I have the following in a batch file
for /f "delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set %%a
net use J: https://csv/dav %p% /user:%u% /persistent:yes
I get an error:
Environment variable C:\Program Files (x86)\Wowza\creds.txt not defined
What do I need to resolve this?
Secondly, it works for all colleagues apart from one. Same laptop make, model and build. I used my details and it failed on his but worked on mine.
What fails is that it asks for the credentials to map the drive instead of taking them from the file
creds.txt
u:JoeBloggs
p:Password1234
Any idea?
Thanks
the reason for your errormessage is, your for /f loop doesn't evaluate the contents of the file. It takes a quoted string as string not as filename. Usebackq changes that behaviour.
You have another failure in your script: With your code, set %%a translates to set u:JoeBloggs, which is invalid syntax. Correct syntax requrires set u=Joebloggs. Therefore you have to split the line in a part before the colon and a part after the colon and build your set command accordingly (just set %%a would work, when the contents of the file would look like u=JoeBloggs)
Change your for loop to:
for /f "usebackq tokens=1,* delims=:" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%a=%%b"
I was going to post a similar answer to #stephan but he beat me to it. If however you have the option to change your creds.txt file to the below:
u=JoeBloggs
p=Password1234
You can shorten the for loop a bit to this:
for /f "usebackq delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%~a"
which effectively just does this:
set "u=JoeBloggs"
set "p=Password1234"
Suppose I have a text file with following contents:
Hello
World
Abc
Now I want to read this in batch and copy them into a single variable. So my variable should contain:
var=Hello World Abc
What is possible work around for this?
If I am trying I am either getting the first word or the last line word.
Thanks
This seems to work. I have appended \n to each line in the read file. I'm not sure how you expect that behavior to actually work:
#echo off
SETLOCAL EnableDelayedExpansion
set "filecontents="
for /f "delims=" %%x in (input.txt) do (
set filecontents=!filecontents!%%x\n
)
echo %filecontents%
To read the contents of a file in a batch file you can use a FOR /F command. You then use the SET command to assign a value to a variable. Also, you will need to utilized delayed expansion to reassign the existing variable to the original variable. A leading space will get assigned so the echo command is using a substring to remove it.
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%G IN (file.txt) do set "var=!var! %%G"
echo %var:~1%
Here is my file that is trying to read hosts file. For some reason, it's not workin properly. Try the code yourself to see the error messages.
#echo off
cd %windir%/System32/drivers/etc
for /f "eol=# tokens=* delims=," %%a in (%windir%/System32/drivers/etc/hosts) do type %%a
pause>nul
type expects a file name. you probably meant echo.
as another issue, the hosts file is not comma, but whitespace separated. so you'll need to change your delims to include space and tab, assuming you want to have only the hostname.
I have a log file containing a stack trace split over a number of lines. I need to read this file into a batch file and remove all of the lines breaks.
As a first step, I tried this:
if exist "%log_dir%\Log.log" (
for /F "tokens=*" %%a in ("%log_dir%\Log.log") do #echo %%a
)
My expectation was that this would echo out each line of the log file. I was then planning to concatenate these lines together and set that value in a variable.
However, this code doesn't do what I would expect. I have tried changing the value of the options for delims and tokens, but the only output I can get is the absolute path to the log file and nothing from the contents of this file.
How can I set a variable to be equal to the lines of text in a file with the line breaks removed?
If you want to use quotes for your filename in the FOR/F loop you need to add the usebackq option too, else you get a string not the content of your file.
for /F "usebackq delims=" %%a in ("%log_dir%\Log.log") do #echo %%a
Or remove the quotes
if exist "%log_dir%\Log.log" (
for /F "tokens=*" %%a in (%log_dir%\Log.log) do #echo %%a
)
I have a file like this, that needs to be parsed and needs to extract a version number.
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
[assembly: AssemblyFileVersion("0.4.2")]
...
...
I need to parse it and extract the 0.4.2 from it.
I have to do this in windows batch file only. I did this the following way, but I know this is NOT the best way.
setlocal enabledelayedexpansion
SET VERSIONSTR=
REM This is an ugly way to do this, because due to some reason I cant save output of for loop to a variable
REM I would like this in a variable instead of a temp file.
FOR /f "tokens=2*" %%j in (AssemblyInfo.cs) do #echo %%j >> tempfile.txt
REM The "AssemblyFileVersion" line has the version number.
REM I would like this in a variable instead of a temp file.
FOR /f %%i in (tempfile.txt) do #echo %%i | find "AssemblyFileVersion" >> anothertempfile.txt
REM Now this for loop will set a variable.
FOR /f %%k in (anothertempfile.txt) do set VERSIONSTR=%%k
REM Now the line is `AssemblyFileVersion("0.4.2")]` so, I am getting the substring from 21st character
REM till the end and then removing last 3 characters, getting us the string 0.4.2
SET VERSIONSTR=!VERSIONSTR:~21,-3!
I hate having this ugliness in my code, any ideas on how I can modify this? At the very least I would like to know a way for me to get the output of a for loop in a variable in windows batch file.
for /f "tokens=2 delims=()" %%a in ('findstr AssemblyFileVersion AssemblyInfo.cs') do set Version=%%~a
echo %Version%
It got a little ugly because I cannot (apparently) use quotation marks as delimiters, so I chose the parentheses and removed the quotes with %%~x. You can avoid all temporary files here, by the way.
The code above just directly grabs the appropriate line (with findstr) and tokenizes it with for /f.