1.

Solve : Converting all the lines of a text file into a variable.?

Answer»

I have created a text FILE by interrogating an ORACLE database using sql.

This then gives me uptodate information. I need to pass all the lines from this file to a string for another programs to use.

I can read all the lines using
for /f %%i in (FILENAME) do set str=%%i but this only sets the variable (str) to the last line of the file as expected I suppose. How can I set the variable so that all the lines of the file appear in SERIES see below;
My file LOOKS like /[emailprotected]
/[emailprotected]
/[emailprotected]

and I want the variable to be equal to /[emailprotected]/[emailprotected]/aldi
The final @ symbol must be omitted also and help how to do this would also be appreciated.joinup.bat

Code: [Select]@echo off

REM this is crucial
setlocal enabledelayedexpansion

REM generate test file
echo /[emailprotected]>supermkts.txt
echo /[emailprotected]>>supermkts.txt
echo /[emailprotected]>>supermkts.txt
echo /[emailprotected]>>supermkts.txt
echo /[emailprotected]>>supermkts.txt
echo /[emailprotected]>>supermkts.txt
echo /[emailprotected]>>supermkts.txt

REM count lines in file
set /a lastline=0
for /f %%S in (supermkts.txt) do set /a lastline=!lastline!+1

REM main loop

REM set line counter to zero
set line=0

REM set output variable to blank
set variable=
REM read each line in input file
for /f "delims=" %%S in (supermkts.txt) do (

REM copy line into string
set string=%%S

REM add one to line counter
set /a line=!line!+1

REM if this is the last line remove the @
if !line! EQU %lastline% set string=!string:@=!

REM append the string to output variable
set variable=!variable!!string!

)


REM here is your output variable
echo %variable%
Code: [Select]D:\>joinup.bat
/[emailprotected]/[emailprotected]/[emailprotected]/[emailprotected]/[emailprotected]/[emailprotected]/monoprix
Alternatives,
1) vbscript

Code: [Select]Set objFSO=CreateObject("Scripting.FileSystemObject")
strMyFile = "c:\test\file.txt"
Set objFS = objFSO.OpenTextFile(strMyFile)
Do Until objFS.AtEndOfLine
s=Trim(objFS.ReadLine) & s
Loop
If Right(s,1) = "@" Then
WScript.Echo Mid(s,1,Len(s)-1)
End If
save as script.vbs and on command line
Code: [Select]C:\test>cscript /nologo script.vbs
/[emailprotected]/[emailprotected]/asda

2) if you can download gawk from here and install.
Code: [Select]{ s=$0 s }
END {
gsub(/ +|@$/,"",s)
print s
}
save as script.awk and on command line
Code: [Select]C:\test>gawk -f script.awk file.txt
/[emailprotected]/[emailprotected]/asda



Discussion

No Comment Found