Removing 'Line Feed/Carriage Return' via DOS

  • Thread starter Thread starter Dennis
  • Start date Start date
D

Dennis

Using only DOS commandline functions, how can I reformat
two textfile lines into one line? I often use batchfile
routines to extract or generate a line of data which I
want to SET into the Environment as a Variable. Thus the
process goes something like this:

echo SET VAR1=>SetVar1.bat
TYPE Sourcefile.txt | FIND "TextString">>SetVar1.bat

I now have a secondary batch file which contains 2 lines -

SET VAR1=
TextString

I need to remove the 'Line Feed/Carriage Return' to
reformat it into 1 line -

SET VAR1=TextString

Then I can 'CALL SetVar1.bat' to SET the Environmental
Variable for use in the original batchfile routine.

Is there a DOS-based method to do this, since I cannot
rely on a supplemental 'Search & Replace' utility which
may not exist on a remote User's PC.
OS is Win2000 & WinXP - would be helpful if method also
works for WinME/Win98/Win95.
Suggestions? Thanks for help.
 
For Win NT/2000/XP ...

for /f "tokens=*" %%a in (Sourcefile.txt) do (
set VAR1=%%a)

For Win 95/98/SE/Me it is MUCH harder, unless you use a
third party utility. Ways to do this without a third
party utility are discussed at the following link:

http://www.infionline.net/~wtnewton/batch/batinput.txt

These two approaches can be married together with a simple
OS decison branch to select which to use ...

if '%OS'==' goto NotNT
:: NT/2000/XP solution
for /f "tokens=*" %%a in (Sourcefile.txt) do (
set VAR1=%%a)
goto :EOF
:NotNT Win 95/98/SE/Me solution goes here
:: ...

Tom Lavedas
===========
 
One step solution:

for /f "Tokens=*" %%v in ('TYPE Sourcefile.txt ^| FIND "TextString"') do (
set VAR1=%%v
)


Using only DOS commandline functions, how can I reformat
two textfile lines into one line? I often use batchfile
routines to extract or generate a line of data which I
want to SET into the Environment as a Variable. Thus the
process goes something like this:

echo SET VAR1=>SetVar1.bat
TYPE Sourcefile.txt | FIND "TextString">>SetVar1.bat

I now have a secondary batch file which contains 2 lines -

SET VAR1=
TextString

I need to remove the 'Line Feed/Carriage Return' to
reformat it into 1 line -

SET VAR1=TextString

Then I can 'CALL SetVar1.bat' to SET the Environmental
Variable for use in the original batchfile routine.

Is there a DOS-based method to do this, since I cannot
rely on a supplemental 'Search & Replace' utility which
may not exist on a remote User's PC.
OS is Win2000 & WinXP - would be helpful if method also
works for WinME/Win98/Win95.
Suggestions? Thanks for help.


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Dennis said:
Using only DOS commandline functions, how can I reformat
two textfile lines into one line? I often use batchfile
routines to extract or generate a line of data which I
want to SET into the Environment as a Variable. Thus the
process goes something like this:

echo SET VAR1=>SetVar1.bat
TYPE Sourcefile.txt | FIND "TextString">>SetVar1.bat

I now have a secondary batch file which contains 2 lines -

SET VAR1=
TextString

I need to remove the 'Line Feed/Carriage Return' to
reformat it into 1 line -

SET VAR1=TextString

Then I can 'CALL SetVar1.bat' to SET the Environmental
Variable for use in the original batchfile routine.

Is there a DOS-based method to do this, since I cannot
rely on a supplemental 'Search & Replace' utility which
may not exist on a remote User's PC.
OS is Win2000 & WinXP - would be helpful if method also
works for WinME/Win98/Win95.
Suggestions? Thanks for help.

Here's a solution that does not have 'VAR1' hard-coded but rather
reads it from the file (so the variable name could be anything).

- - - - - - - - - - begin screen capture - - - - - - - - - -
C:\cmd>type c:\temp\Test2345.txt
set VAR1=
TextString

C:\cmd>set var1
Environment variable var1 not defined

C:\cmd>demo\SetVarFromFile

C:\cmd>set var1
VAR1=TextString

C:\cmd>rlist demo\SetVarFromFile.cmd
=====begin C:\cmd\demo\SetVarFromFile.cmd ====================
1. @echo off
2. set %~n0_ctr=0
3. for /f "tokens=*" %%a in (c:\temp\Test2345.txt) do (
4. set /a %~n0_ctr+=1&set element!%~n0_ctr!=%%a
5. )
6. %element1%%element2%
7. set %~n0_ctr=
=====end C:\cmd\demo\SetVarFromFile.cmd ====================
- - - - - - - - - - end screen capture - - - - - - - - - -

However, you should ask yourself why you are creating these two-line
files in the first place, if all you want to do is to assign a value
to some environment variable.
 
Thanks - I'll study this alternate method.
Creating the 2-line batchfile on-the-fly was the only
method I knew to call and set an environmental variable
for an 'unknown' data string.
 
Hello Dennis.
Using only DOS commandline functions, how can I reformat
two textfile lines into one line? I often use batchfile
routines to extract or generate a line of data which I
want to SET into the Environment as a Variable. Thus the
process goes something like this:

echo SET VAR1=>SetVar1.bat
TYPE Sourcefile.txt | FIND "TextString">>SetVar1.bat

I now have a secondary batch file which contains 2 lines -

SET VAR1=
TextString

I need to remove the 'Line Feed/Carriage Return' to
reformat it into 1 line -

SET VAR1=TextString

Then I can 'CALL SetVar1.bat' to SET the Environmental
Variable for use in the original batchfile routine.

For WinNT/2K/XP, I have a solution:

TYPE Sourcefile.txt | FIND "TextString">SetVar1.txt
For /f "tokens=*" %%s in (SetVar1.txt) do set VAR1=%%s
 
Back
Top