New line symbol into variable batch - batch-file

I am having one annoying problem while writing script in batch.
for /f "Tokens=* Delims=" %%x in (xxtempLinkMulti3.txt) do set LinkMulti2=!LinkMulti2!%%x
input xxtempLinkMulti3.txt looks like
www.google.com
www.yahoo.com
www.facebook.com
www.linkedin.com
Code is working fine, but when i want to echo my variable the result is:
www.google.comwww.yahoo.comwww.facebook.comwww.linkedin.com
but it should be excatly like output. (with new lines signs)(i've tried using "^" sign between !LinkMulti2! and %%x but it didn't affect on the output.
Please help.thanks. It may be other way to save from file to variable (perfectly the same, with new line signs).

This should do it:
setlocal EnableDelayedExpansion
rem get new-line into variable (note the two empty lines which are mandatory)
set NewLine=^
for /f "Tokens=* Delims=" %%x in (xxtempLinkMulti3.txt) do set LinkMulti2=!LinkMulti2!%%x!NewLine!
echo !LinkMulti2!
endlocal
Note that empty lines in the original file are not included.
You need delayed expansion in order to echo the string correctly.

Why you try to concatenate the lines into a single line?
for /f "Tokens=* Delims=" %%x in (xxtempLinkMulti3.txt) do echo %%x will do just fine.
And why do you need to save all of it as a variable? You can type it and it will display all the words in the text file.

Related

Using a filename variable in a for loop

I have a directory with a file in it (actually it has a lot of files, but I figured getting one to work was the first step before building the loop to hit each file), that needs to be edited and saved with a similar filename. Instead of manually typing in the filename, I'd like to use a variable containing the filename.
Sample Input Data
FileX.txt
Text line
Text line
Text line
Desired Output Data
FileX2.txt
1 Text line
2 Text line
3 text line
I can manage this with one file at a time, I'm struggling to write one script to look through the contents of a folder and do this to each text file within the folder.
I'm on windows 7 and this is what I have so far:
#echo off
setLocal EnableDelayedExpansion
Set N=0
REM THE BELOW LINE IS THE VARIABLE I'M TRYING TO SET AND THEN HAVE PASSED AS THE FOR LOOP PARAMETER
Set F="FILENAME"
REM IF I SKIP A VARIABLE AND HARDCODE THE FILENAME IN THE BELOW LINE, IT WORKS FOR THE ONE FILE
for /f "tokens=* delims= " %%a in (FILENAMEVARIABLE.txt) do (
Set /a N=!N!+1
echo !N! %%a, >> !F!.txt
)
The opening statement of the for loop, is where I can't get the variable to take. In FILENAMEVARIABLE.txt I have tried %%F, %F, !F!, and %%~nxf none of which manage to call the correct file to start the loop. Any ideas what I'm doing wrong here?
Here's what I think you were trying to do:
#Echo Off
SetLocal EnableDelayedExpansion
For %%A In (*.txt) Do (Set "N=0"
(For /F "UseBackQ Tokens=*" %%B In ("%%A") Do (Set /A N +=1
Echo !N! %%B))>"%%~nA2%%~xA")
You could also use FindStr:
#Echo Off
For /F "Tokens=1-2* Delims=:" %%A In ('FindStr /N "^" *.txt 2^Nul'
) Do >>"%%~nA2%%~xA" Echo %%B %%C
In both of the above examples I have assumed that the source directory and script directory are the same.

Extract a variable value from a text

it's quite complicated....using a batch script on windows platform, I need to extract a variable string from an output file, editing the final result towards another file.
Example, the file text.log comes to me filled in this way:
C:Windows\xfile\CODSSD1.XXX
C:Windows\xfile\RPDDFSS.XXX
C:Windows\xfile\XCCLDJW.XXX
I need to catch only the last word in all rows, editing them in this manner:
XXXCODSSD1
XXXRPDDFSS
XXX......
I've tried different commands but I'm not an expert.
Thank you very much!
Kind regards.
Joe
Given your output file named text.log, I would suggest something similar to the following:
#Echo Off
For /F "UseBackQ Delims=" %%A In ("text.log"
) Do For /F "Tokens=* Delims=." %%B In ("%%~xA%%~nA") Do Echo %%B
Pause
I have split the For loop over two lines only to maintain line length within 80 characters. Whilst it isn't a requirement, It's a best practice preference.
Edit
To test it at the command prompt, you can use this single line:
For /F "UseBackQ Delims=" %A In ("text.log") Do #For /F "Tokens=* Delims=." %B In ("%~xA%~nA") Do #Echo %B
This is a pretty simple one line FOR command using the /F option to read the file. You can use the FOR command modifiers to break up the file name into its individual parts. The x modifier is for the file extension. The n modifier is for the base file name.
#echo off
setlocal enabledelayedexpansion
FOR /F "usebackq delims=" %%G IN ("text.log") DO (
set "var=%%~xG%%~nG"
echo !var:~1!
)
pause

Batch: Reading in a line from a file that has a colon and exclamation

I'm trying to read a line from a file that contains a colon and exclamation point. When I echo that line, it only shows everything AFTER the colon. I need to keep the enabledelayedexpansion for more advanced code I will be doing in the do loop. But right now I just want to echo properly.
The file should say something like this:
! 12345 APX:
6.32
The code I tried was:
setlocal EnableDelayedExpansion
cd C:\Users\jwagh\Desktop\
for /f "usebackq delims=" %%a in (test.txt) DO (
setlocal DisableDelayedExpansion
echo %%a
set line=%%a
set example=%line:~0,-1%
echo %example%
)
Probably disabling delayedexpansion is the only practicable solution, but depending on your data, setting delims to ! would set %%a to the entire line, minus the initial ! (assuming obviously only one !-sequence of known length and it always leads the line)
OR
for /f "usebackq delims=!" %%a in (%FamilyFile%) DO echo ^^!%%a
The only bullet-proof solution is to disable delayed expansion during expansion of %%a, and to enable it only when it is actually needed. For example, something like this:
setlocal DisableDelayedExpansion
for /F "usebackq delims=" %%a in (%File%) do (
set "Item=%%a"
setlocal EnableDelayedExpansion
echo(!Item:,-1!
endlocal
)
Since localised environments created by setlocal/endlocal blocks are limited in terms of nesting, you need to put also endlocal within a loop. Regard that after endlocal, all environment changes since the latest setlocal get lost.
But now for something completely different (related to revision 3 of the question):
I assume %File% holds a quoted file path/name, because of the usebackq option.
Let me recommend not to include the quotes in the variable value, using the following syntax:
set "File=D:\Data\file.ext" & rem // (particularly note the position of the opening quote)
Reference this post to learn why this is better.

Trouble in Batch using quotes in a variable

Relatively new to batch scripts here and I have been searching everywhere for the answer only to not find anything.
Here is what I have for a batch script so far..
#echo off
set addtext="text to add includes spaces"
for /f "delims=" %%l in (file.txt) do (
echo %%l %addtext% >> tmpfile.txt
)
I'm looking to add a line of text to every line in the file but my problem comes in with the double quotes. I don't want the quotes to display with the text.
I only have the quotes there because there are spaces in the string of text that I'm looking to add to every line.
#echo off
setlocal enableextensions disabledelayedexpansion
set "addtext=text to add includes spaces"
for /f "delims=" %%l in (file.txt) do (
>> tmpfile.txt echo %%l %addtext%
)
This should work. Just not include the quotes in the value of the variable, but use them to wrap the assignment.
In cases where the string could contain more problematic characters, this is a safer version
#echo off
setlocal enableextensions disabledelayedexpansion
set "addtext=text to add includes spaces, > redirections & more problems !"
(for %%a in ("%addtext%") do for /f "delims=" %%l in (file.txt) do (
echo %%l %%~a
)) >> tmpfile.txt
Quotes are not included in the value, but wraps the assignation
To prevent problem accessing the variable, it is wrapped in quotes, stored in a for replaceable parameter (%%a) and when requested echoed without the quotes (%%~a)
Just to get better performance (should be used also in the first code) instead of open/write/close the output file for each line (redirection for each echo), redirection is handled for the full for command.

Concatenating files in a Batch script

I have a batch script that would concatenate the content of files that were listed in an index file. It used to work until there were spaces in the paths. I have edited it a bit, but it is something like this:
SET INPUT="C:\Has Spaces In Path\indexfile.txt"
SET ROOT="C:\Has Spaces In Path\inputdirectory\"
SET OUTPUT="C:\Has Spaces In Path\outputdirectory\mergedfile.txt"
FOR /F %%A IN (%INPUT%) DO TYPE "%ROOT%%%A" >> "%OUTPUT%"
The problem I have is that %INPUT% now appears to get tokenised in the for loop and if I put quotes around it (i.e. "%INPUT%") it does not work either. Is there any way I can get this loop to iterate over each line in the file specified by INPUT and concatenate the contents to the OUTPUT file?
Thanks.
Edit: Based on the answer, this did what I wanted:
FOR /F %%A IN ('type "%INPUT%"') DO TYPE "%ROOT%%%A" >> "%MERGED%"
FOR /F %%A IN ('type %INPUT%') DO echo %%A
' in the brackets will cause to execute the statement and uses the output like a file source
FOR /F "usebackq" %%A IN ("%INPUT%") DO TYPE "%ROOT%%%A" >> "%OUTPUT%"
or, better yet:
(FOR /F "usebackq" %%A IN ("%INPUT%") DO TYPE "%ROOT%%%A") > "%OUTPUT%"
For further details, see: FOR /?

Resources