I am trying to edit some lines of text within a text file using a batch file. The lines are as below -
#TEST_RSA_KEY=1
#V3SERVER0=109.73.122.107;29006
#DOWNLOAD0=109.73.122.112;29006
I need to change these to be
#TEST_RSA_KEY=0
#V3SERVER0=91.207.36.31;29006
#DOWNLOAD0=91.207.36.37;29006
How would you recommend I do this via a batch file, I am very new to this and have very basic knowledge so simple and clear answers please! :) Thank you
Not tested
#echo off
set file_loc=C:\text.file.txt
for "usebackq tokens=* delims=" %%a in ("%file_loc%") do (
set "%%a"
)
set "#TEST_RSA_KEY=0"
set "#V3SERVER0=91.207.36.31;29006"
set "#DOWNLOAD0=91.207.36.37;29006"
set #>"%file_loc%"
I've assumed that all properties start with #
Note - I have not tested any of the code examples below, so there could be bugs. But I have successfully used all of the techniques below in the past, and any fixes (if necessary) should be fairly trivial.
All of my solutions below assume that spaces never appear in any of the name value pairs. Each of the solutions could be adapted to support spaces, if necessary.
If the order of the lines is not important, then the following pure batch solution works well and is quite fast. I use FINDSTR to remove the lines that are to be altered, before appending the new values to the end:
#echo off
>"file.txt.new" (
findstr /v /b "#TEST_RSA_KEY= #V3SERVER0= #DOWNLOAD0=" "file.txt"
echo #TEST_RSA_KEY=0
echo #V3SERVER0=91.207.36.31;29006
echo #DOWNLOAD0=91.207.36.37;29006
)
move /y "file.txt.new" "file.txt" >nul
If the order of the lines is important, then I would use my JREPL.BAT utility - a hybrid JScript/batch regular expression text processor. It is pure script that runs natively on any Windows machine from XP onward. I recommend putting the script within a directory that is listed within your path. I like to use "c:\utils" for all my non-standard utilities.
A simple one replacement at a time strategy is the simplest effective solution, and it should be plenty fast unless the file is exceptionally large:
#echo off
call jrepl "^(#TEST_RSA_KEY)=.*" "$1=0" /f "file.txt" /o -
call jrepl "^(#V3SERVER0)=.*" "$1=91.207.36.31;29006" /f "file.txt" /o -
call jrepl "^(#DOWNLOAD0)=.*" "$1=91.207.36.37;29006" /f "file.txt" /o -
The above strategy can be made easy to maintain (add additional replacements) with a bit more code, as long as none of the strings contain * or ?, and no value begins with =:
#echo off
for %%A in (
"#TEST_RSA_KEY=0"
"#V3SERVER0=91.207.36.31;29006"
"#DOWNLOAD0=91.207.36.37;29006"
) do for /f "tokens=1* delims==" %%B in (%%A) do (
call jrepl "^(%%B)=.*" "$1=%%C" /f "file.txt" /o -
)
The * and ? limitation can be removed if the replacement strings are already in a separate file (replace.txt):
#echo off
for /f "tokens=1* delims==" %%A in (replace.txt) do (
call jrepl "^(%%A)=.*" "$1=%%B" /f "file.txt" /o -
)
Or the replacement strings could be embedded within the batch script itself
#echo off
for /f "tokens=1* delims==" %%A in ('findstr "^#" "%~f0"') do (
call jrepl "^(%%A)=.*" "$1=%%B" /f "file.txt" /o -
)
exit /b
#TEST_RSA_KEY=0
#V3SERVER0=91.207.36.31;29006
#DOWNLOAD0=91.207.36.37;29006
The entire job can be done much more efficiently in one pass if you are willing to do the capture group bookkeeping and prepare a long command line (8191 byte length max):
#call jrepl "(#TEST_RSA_KEY)=.* (#V3SERVER0)=.* (#DOWNLOAD0)=.*" "$2=0 $4=91.207.36.31;29006 $6=91.207.36.37;29006" /b /t " " /f "file.txt" /o -
A variation of any of the prior 3 methods could be used to make this efficient solution easier to maintain, as long as ! never appears in the values. For example:
#echo off
setlocal enableDelayedExpansion
set "find="
set "repl="
set "n=0"
for /f "tokens=1* delims==" %%A in ('findstr "^#" "%~f0"') do (
set "find=!find! (%%A)=.*"
set /a n+=2
set "repl=!repl! $!n!=%%B"
)
call jrepl "!find:~1!" "!repl:~1!" /b /t " " /f "file.txt" /o -
exit /b
#TEST_RSA_KEY=0
#V3SERVER0=91.207.36.31;29006
#DOWNLOAD0=91.207.36.37;29006
Related
I'm writing batch script on Windows, with it's help I would like to sort out information from many files to smaller files.
I got ~3000 long lines in log files, from whom I need get few things, basically there are name and value (example ",INC_LIMI=050,ISO_LIMI=050,INC_MLIM=074,"), and everything is separated with "," symbol. My question how you can read long string line and just read values like:
String LineString[]
LineString = Line.split(,)
String s = "INC_MLIM"
For elem in LineString
if elem.exist(s)
NewLine.append(elem)
and latter on just save to new file.
EDIT:
There is service.log file which contains multiple lines with same variable names, but I don't need all of them so the thing I'm trying to do is
From line :
",INC_MLIM=074,ISO_MLIM=074,LOC_LI_P=050,LOC_LI_L=050,TRI_LI_P=074,TRI_LI_L=074,"
Transform to new line structure with less variables and separate with tabs instead of comma. New line should look something like this:
"INC_MLIM=074 ISO_MLIM=074 LOC_LI_L=050 TRI_LI_L=074"
You don't state which values you want. I'll arbitrarily assume you want INC_LIMI and INC_MLIM.
Like most any text file manipulation, this is a pain to do with pure batch. But it is possible.
I'm assuming your lines are all <8192 characters long. If you have lines that are longer than that, then a pure batch solution is not possible, and you should skip right down to the bottom of this answer for a JREPL solution
Batch does not have a convenient split function that allows splitting at a specific user defined character. The FOR command almost works, but it also splits at ;, =, <tab>, and <space>. So it is not a good choice.
With the correct arcane syntax, you can use variable expansion find/replace to substitute a newline (0x0A) for every comma. This will generate one name=value pair per line, which is very convenient for letting FINDSTR filter out the values that you want.
Here is a solution that relies on a temporary table. This iterates all *.log files, and for each one, it creates output in *.log.new.
#echo off
setlocal enableDelayedExpansion
(set LF=^
%= This creates a newline 0x0A character =%
)
for %%N in ("!LF!") do for %%F in (*.log) do (
(
for /f "usebackq delims=" %%A in ("%%F") do (
set "ln=%%A"
echo(!ln:,=%%~N!
)
)>"%%F.temp"
findstr /b "INC_LIMI= INC_MLIM=" "%%F.temp" >"%%F.new"
del "%%F.temp"
)
type *.log.new
exit /b
Note that the above can fail if your log files contain !. This could be solved by toggling delayed expansion on and off as needed.
Some people don't like to use temp files. In this case, getting rid of the temp file introduces even more arcane batch constructs. But it does eliminate the ! delayed expansion issue, and the code is shorter. This version can also be significantly slower if the source files are very large.
#echo off
setlocal disableDelayedExpansion
(set LF=^
%= This creates a newline 0x0A character =%
)
for %%F in (*.log) do (
for /f "usebackq delims=" %%A in ("%%F") do (
set "ln=%%A"
cmd /v:on /c "for %%N in ("!LF!") do #echo(!ln:,=%%~N!"|findstr /b "INC_LIMI= INC_MLIM="
)
) >"%%F.new"
type *.log.new
exit /b
It is also possible to solve this without using FINDSTR. But this solution assumes the same name never appears more than once on any given line, and all found names have a value:
#echo off
setlocal disableDelayedExpansion
for %%F in (*.log) do (
for /f "usebackq delims=" %%A in ("%%F") do (
set "ln=,%%A"
for %%N in (INC_LIMI INC_MLIM) do call :findName %%N
)
) >"%%F.new"
type *.log.new
exit /b
:findName Name
setlocal enableDelayedExpansion
set "test=!ln!"
:loop
set "test2=!test:*,%1=!"
if "!test2!" equ "!test!" return
if not defined test2 return
if "!test2:~0,1!" neq "=" set "test=,!test2:*,=!" & goto :loop
for /f "delims=," %%V in ("!test2:~1!") do (
endlocal
echo(%1=%%V
)
exit /b
Here is a variation that handles empty values, but can break if a value contains quotes or poison characters:
#echo off
setlocal disableDelayedExpansion
for %%F in (*.log) do (
for /f "usebackq delims=" %%A in ("%%F") do (
set "ln=,%%A"
for %%N in (INC_LIMI INC_MLIM) do call :findName %%N
)
) >"%%F.new"
type *.log.new
exit /b
:findName Name
setlocal enableDelayedExpansion
set "test=!ln!"
:loop
set "test2=!test:*,%1=!"
if "!test2!" equ "!test!" return
if not defined test2 return
if "!test2:~0,1!" neq "=" set "test=,!test2:*,=!" & goto :loop
set "test2=%1!test2!
endlocal&echo(%test2:,=&rem %
exit /b
But I wouldn't use any of the above. In fact, I would never restrict myself to pure batch because text file manipulation is so darn inefficient and inscrutable.
Instead, I would use JREPL.BAT - a regular expression command line text processing utility. JREPL.BAT is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward - no 3rd party exe file required.
With JREPL, the solution is as simple as
#echo off
for %%F in (*.log) do call jrepl "(?:^|,)((?:INC_LIMI|INC_MLIM)=[^,]*)" "$txt=$1" /jmatchq /f "%%F" /o "%%F.new"
type *.log.new
Not only is the code nice and clean, it is much faster than any pure batch solution.
Need help ! Working on a batch file which will replace a set of characters from a bunch of text files in a folder. I have found the code which will do that. But it does for only one file. IS there a way where it can do it for all the files in the folder. There a total of 1000 files inside the folder. I am using a Windows 7 OS. Attaching the code I found where it does for one file.
Thanks
Harry
#echo off
setlocal enabledelayedexpansion
set INTEXTFILE=Replace_string.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=Apple
set REPLACETEXT=Mango
set SEARCHTEXT=Cat
set REPLACETEXT=Dog
set OUTPUTLINE=
for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
SET string=%%A
for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
if "!string!" == "" (
echo.>>%OUTTEXTFILE%
) else (
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified! >> %OUTTEXTFILE%
)
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
If you want a pure batch solution, then the simplest thing to do is to encapsulate the code in a subroutine that takes the name of the file as an argument, and call that routine from within a FOR loop that iterates the file names. This is a general approach that can be used for any code that you want to run iteratively
against files in a folder.
The end result of your code does not create or rename any folders, so a simple FOR is safe to use. But if your code creates or renames folders, then a FOR loop could also process the newly created or renamed files. This can be solved by using FOR /F with the DIR /B command instead.
Note - I eliminated dead (unused) variables from the code.
#echo off
setlocal enabledelayedexpansion
pushd "c:\path\to\your\folder\containing\files\to\modify"
for %%F in (*.txt) do call :replace "%%F"
exit /b
:replace
set INTEXTFILE=%1
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=Cat
set REPLACETEXT=Dog
for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
SET string=%%A
for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
if "!string!" == "" (
echo.>>%OUTTEXTFILE%
) else (
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified! >> %OUTTEXTFILE%
)
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
exit /b
But there are many limitations and inefficiencies with the code, with lots of room for improvement.
Limitations:
Input and output lines must be a bit less than 8191 bytes long.
The search ignores case
The search string cannot contain = or begin with ~, * or !
The replace string cannot contain !
Lines containing ! will be corrupted because delayed expansion is enabled when %%A is expanded. This can be solved by strategically toggling delayed expansion on and off within the loop(s).
Leading : will be stripped from all lines because consecutive delimiter characters are treated as a single delimiter.
The replacement will be corrupted if the search term contains %%a or %%b or %%A. This can be avoided by transferring the search and replacement terms to FOR variables.
Certain characters within the search and/or replacement terms could cause problems or require complex escape sequences. This can be simplified by getting the desired strings in environment variables (which may still require escape sequences) and then using delayed expansion and FOR /F to transfer the values to FOR variables.
There are obscure situations where ECHO. can fail. The only safe variant that is guaranteed to work is ECHO(.
A non empty line could become empty after replacement if the replacement string is empty, and the empty line will not be output properly because neither ECHO. nor ECHO( was used.
Inefficiencies / other issues
Redirection is performed for each line of output, which is slow. Better (faster) to redirect once outside the loop.
The DEL/RENAME pair can be replaced by a single MOVE command
CALL is relatively slow. Best to minimize use of CALL if you want the fastest possible solution. But sometimes it cannot be avoided.
I prefer to have my temp file name to be a derivative of the original name. I typically append a .new extension to the original name, so "original.txt" becomes "original.txt.new"
Below is highly optimized code that addresses all but the first 4 points above. It is about as robust and efficient as pure batch can get if you want to use FOR /F to read the file.
#echo off
setlocal disableDelayedExpansion
pushd "c:\path\to\your\folder\containing\files\to\modify"
set "find=Cat"
set "repl=Dog"
setlocal enableDelayedExpansion
for /f delims^=^ eol^= %%S in ("!find!") do (
for /f delims^=^ eol^= %%R in ("!repl!") do (
endlocal
for %%F in (*.txt) do (
for /f "delims=" %%L in ('findstr /n "^" "%%F"') do (
set "ln=%%L"
setlocal enableDelayedExpansion
set "ln=!ln:*:=!"
if defined ln set "ln=!ln:%%S=%%R!"
echo(!ln!
endlocal
)
) >"%%F.new" & move /y "%%F.new" "%%F" >nul
)
)
popd
The above required a lot of experience and arcane knowledge to develop. Even after all the work, it still has the following limitations.
Input and output lines must be a bit less than 8191 bytes long.
The search ignores case
The search string cannot contain = or begin with ~, * or !
The replace string cannot contain !
Removing those limitations would require a ridiculous amount of slow and even more impenetrable code. Hardly worth it.
That is why I have abandoned using pure batch for text processing, and I developed JREPL.BAT to provide powerful regular expression text processing capabilities to the Windows command environment. JREPL is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
JREPL makes the solution so simple, it can easily be run directly on the command line, without any batch file.
pushd "c:\your\path\with\files\to\be\modified"
for %F in (*.txt) do call jrepl "Cat" "Dog" /l /f "%F" /o -
popd
The search is case sensitive. You can add the /i option if you want the search to ignore case. Change the %F to %%F if you use the command within a batch script.
Since all you need to do is replace text in an existing file, use a tool like FART which is specifically designed to do this:
FART -r "C:\Data\Directory\*.txt" "OldText" "NewText"
The -r switch says to process all txt files in provided directory and subfolders.
Note that you can add the -i switch to ignore the case when searching.
I have written a simple script which will find and replace a matching string. I am getting issue in the output file. Following is my script
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1 delims=" %%a in ('FINDSTR "^applicationPort" "E:\BATCH-SCRIPTING\Version.txt"') do (
echo Installed version is %%a
set oldPort=%%a
)
for /f "tokens=1 delims=" %%b in ('FINDSTR "^applicationPort" "E:\BATCH-SCRIPTING\Sample.txt"') do (
echo Installed version is %%b
set newPort=%%b
)
set SEARCHTEXT="applicationPort=8080"
set REPLACETEXT="applicationPort=8090"
set file="E:\BATCH-SCRIPTING\Sample.txt"
for /f "tokens=1,* delims=]" %%A in ('"type %file% |find /n /v """') do (
set "line=%%B"
if defined line (
call echo %%line:%SEARCHTEXT%=%REPLACETEXT%%%>> %file%_new
) ELSE echo.
)
move /Y %file%_new %file% > nul
In my output file the expected output should be : applicationPort=8090, but i am getting 8080=applicationPort=8090=8080.
Can anyone help me in resolving this issue.
Look at what your key replacement line would like like after the first round of variable expansion:
call echo %%line:%SEARCHTEXT%=%REPLACETEXT%%%>> %file%_new
becomes
call echo %line:applicationPort=8080=applicationPort=8090%>>"E:\BATCH-SCRIPTING\Sample.txt"
The search/replace parser ends the search phrase at the first = it sees. So after the CALL it searches for applicationPort and replaces it with 8080=applicationPort=8090. Perfectly logical.
The bad news is there is absolutely no way to escape the = in the search term. Replacing = is extremely difficult using a batch file. There are solutions, but I don't recommend them. Instead, I would use my hybrid JScript/batch utility called REPL.BAT. It is pure script that will run on any Windows machine from XP onward. It performs a regex search and replace on stdin and writes the result to stdout. Full documentation is embedded within the script.
#echo off
type "E:\BATCH-SCRIPTING\Sample.txt"|repl "applicationPort=8080" "applicationPort=8090" >"E:\BATCH-SCRIPTING\Sample.txt_new"
move /y "E:\BATCH-SCRIPTING\Sample.txt_new" "E:\BATCH-SCRIPTING\Sample.txt"
REPL.BAT is much simpler and faster (and more powerful) than anything you can do using pure batch script.
I am new to batch file scripting and need to develop a script to replace a character in a file using a batch script.
I have to replace "servername/ActionService.asmx,1"
with "servername/ActionService.asmx,0"
in file called APP.
Please let me know if there is any solution using only commands.
You can use GNUWin32 sed:
#ECHO OFF &SETLOCAL
set "string=servername/ActionService.asmx,1"
FOR /f %%a IN ('echo "%string%" ^| sed "s/[0-9]/0/"') DO set "newstring=%%~a"
ECHO %newstring%
If you're toggling back and forth between these two states, it might be easier to create two copies of the file with different names, together with a couple of batch files (e.g. actionService1.bat and actionService2.bat) to copy the appropriate file over your APP file.
Otherwise you might consider getting Windows versions of the Unix tools sed and awk, which excel at this type of file manipulation.
The Batch file below assume that there is precisely one line with the target string. This method is relatively fast.
#echo off
for /F "delims=:" %%a in ('findstr /N "servername/ActionService.asmx,1" theFile.txt') do set lineNum=%%a
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" theFile.txt do (
set "line=%%b"
setlocal EnableDelayedExpansion
if %%a equ %lineNum% (
echo !line:1=0!
) else (
echo(!line!
)
endlocal
)) > theFile.new
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: Way the first - suppresses emptylines
FOR /f "delims=" %%i IN (app) DO SET line=%%i&set line=!line:servername/ActionService.asmx,1=servername/ActionService.asmx,0!&ECHO(!line!
ECHO ====================
:: Way the second
FOR /f "delims=" %%i IN ('type app^|findstr /n "$"') DO (
SET line=%%i
set line=!line:servername/ActionService.asmx,1=servername/ActionService.asmx,0!
SET line=!line:*:=!
ECHO(!line!
)
ECHO ====================
GOTO :EOF
Two ways here. You would need to redirect your choice to a new file as you cannot update in-place.
Doing a quick google search I found this http://www.dostips.com/?t=Batch.FindAndReplace
Using a helper batch file called repl.bat from here: http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
type app |repl "servername\/ActionService.asmx,1" "servername/ActionService.asmx,0" >appnew.txt
This DOS batch script is stripping out the blank lines and not showing the blank lines in the file even though I am using the TYPE.exe command to convert the file to make sure the file is ASCII so that the FIND command is compatible with the file. Can anyone tell me how to make this script include blank lines?
#ECHO off
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE.exe "build.properties" ^| FIND.exe /V ""`) DO (
ECHO --%%A--
)
pause
That is the designed behavior of FOR /F - it never returns blank lines. The work around is to use FIND or FINDSTR to prefix the line with the line number. If you can guarantee no lines start with the line number delimiter, then you simply set the appropriate delimiter and keep tokens 1* but use only the 2nd token.
::preserve blank lines using FIND, assume no line starts with ]
::long lines are truncated
for /f "tokens=1* delims=]" %%A in ('type "file.txt" ^| find /n /v ""') do echo %%B
::preserve blank lines using FINDSTR, assume no line starts with :
::long lines > 8191 bytes are lost
for /f "tokens=1* delims=:" %%A in ('type "file.txt" ^| findstr /n "^"') do echo %%B
::FINDSTR variant that preserves long lines
type "file.txt" > "file.txt.tmp"
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" "file.txt.tmp"') do echo %%B
del "file.txt.tmp"
I prefer FINDSTR - it is more reliable. For example, FIND can truncate long lines - FINDSTR does not as long as it reads directly from a file. FINDSTR does drop long lines when reading from stdin via pipe or redirection.
If the file may contain lines that start with the delimiter, then you need to preserve the entire line with the line number prefix, and then use search and replace to remove the line prefix. You probably want delayed expansion off when transferring the %%A to an environment variable, otherwise any ! will be corrupted. But later within the loop you need delayed expansion to do the search and replace.
::preserve blank lines using FIND, even if a line may start with ]
::long lines are truncated
for /f "delims=" %%A in ('type "file.txt" ^| find /n /v ""') do (
set "ln=%%A"
setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
echo(!ln!
endlocal
)
::preserve blank lines using FINDSTR, even if a line may start with :
::long lines >8191 bytes are truncated
for /f "delims=*" %%A in ('type "file.txt" ^| findstr /n "^"') do (
set "ln=%%A"
setlocal enableDelayedExpansion
set "ln=!ln:*:=!"
echo(!ln!
endlocal
)
::FINDSTR variant that preserves long lines
type "file.txt" >"file.txt.tmp"
for /f "delims=*" %%A in ('findstr /n "^" "file.txt.tmp"') do (
set "ln=%%A"
setlocal enableDelayedExpansion
set "ln=!ln:*:=!"
echo(!ln!
endlocal
)
del "file.txt.tmp"
If you don't need to worry about converting the file to ASCII, then it is more efficient to drop the pipe and let FIND or FINDSTR open the file specified as an argument, or via redirection.
There is another work around that completely bypasses FOR /F during the read process. It looks odd, but it is more efficient. There are no restrictions with using delayed expansion, but unfortunately it has other limitations.
1) lines must be terminated by <CR><LF> (this will not be a problem if you do the TYPE file conversion)
2) lines must be <= 1021 bytes long (disregarding the <CR><LF>)
3) any trailing control characters are stripped from each line.
4) it must read from a file - you can't use a pipe. So in your case you will need to use a temp file to do your to ASCII conversion.
setlocal enableDelayedExpansion
type "file.txt">"file.txt.tmp"
for /f %%N in ('find /c /v "" ^<"file.txt.tmp"') do set cnt=%%N
<"file.txt.tmp" (
for /l %%N in (1 1 %cnt%) do(
set "ln="
set /p "ln="
echo(!ln!
)
)
del "file.txt.tmp"
I wrote a very simple program that may serve as replacement for FIND and FINDSTR commands when they are used for this purpose. My program is called PIPE.COM and it just insert a blank space in empty lines, so all the lines may be directly processed by FOR command with no further adjustments (as long as the inserted space don't cares). Here it is:
#ECHO off
if not exist pipe.com call :DefinePipe
FOR /F "USEBACKQ delims=" %%A IN (`pipe ^< "build.properties"`) DO (
ECHO(--%%A--
)
pause
goto :EOF
:DefinePipe
setlocal DisableDelayedExpansion
set pipe=´)€ì!Í!ŠÐŠà€Ä!€ü.t2€ü+u!:æu8²A€ê!´#€ì!Í!².€ê!´#€ì!Í!²+€ê!´#€ì!Í!Šò€Æ!´,€ì!Í!"Àu°´LÍ!ëÒ
setlocal EnableDelayedExpansion
echo !pipe!>pipe.com
exit /B
EDIT: Addendum as answer to new comment
The code at :DefinePipe subroutine create a 88 bytes program called pipe.com, that basically do a process equivalent to this pseudo-Batch code:
set "space= "
set line=
:nextChar
rem Read just ONE character
set /PC char=
if %char% neq %NewLine% (
rem Join new char to current line
set line=%line%%char%
) else (
rem End of line detected
if defined line (
rem Show current line
echo %line%
set line=
) else (
rem Empty line: change it by one space
echo %space%
)
)
goto nextChar
This way, empty lines in the input file are changed by lines with one space, so FOR /F command not longer omit they. This works "as long as the inserted space don't cares" as I said in my answer.
Note that the pipe.com program does not work in 64-bits Windows versions.
Antonio
Output lines including blank lines
Here's a method I developed for my own use.
Save the code as a batch file say, SHOWALL.BAT and pass the source file as a command line parameter.
Output can be redirected or piped.
#echo off
for /f "tokens=1,* delims=]" %%a in ('find /n /v "" ^< "%~1"') do echo.%%ba
exit /b
EXAMPLES:
showall source.txt
showall source.txt >destination.txt
showall source.txt | FIND "string"
An oddity is the inclusion of the '^<' (redirection) as opposed to just doing the following:
for /f "tokens=1,* delims=]" %%a in ('find /n /v "" "%~1"') do echo.%%ba
By omitting the redirection, a leading blank line is output.
Thanks to dbenham, this works, although it is slightly different than his suggestion:
::preserve blank lines using FIND, no limitations
for /f "USEBACKQ delims=" %%A in (`type "file.properties" ^| find /V /N ""`) do (
set "ln=%%A"
setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
echo(!ln!
endlocal
)
As mentioned in this answer to the above question, it doesn't seem that lines are skipped by default using for /f in (at least) Windows XP (Community - Please update this answer by testing the below batch commands on your version & service pack of Windows).
EDIT: Per Jeb's comment below, it seems that the ping command, in at least Windows XP, is
causing for /f to produce <CR>'s instead of blank lines (If someone knows specifically why, would
appreciate it if they could update this answer or comment).
As a workaround, it seems that the second default delimited token (<space> / %%b in the example)
returns as blank, which worked for my situation of eliminating the blank lines by way of an "parent"
if conditional on the second token at the start of the for /f, like this:
for /f "tokens=1,2*" %%a in ('ping -n 1 google.com') do (
if not "x%%b"=="x" (
{do things with non-blank lines}
)
)
Using the below code:
#echo off
systeminfo | findstr /b /c:"OS Name" /c:"OS Version"
echo.&echo.
ping -n 1 google.com
echo.&echo.
for /f %%a in ('ping -n 1 google.com') do ( echo "%%a" )
echo.&echo.&echo --------------&echo.&echo.
find /?
echo.&echo.
for /f %%a in ('find /?') do ( echo "%%a" )
echo.&echo.
pause
.... the following is what I see on Windows XP, Windows 7 and Windows 2008, being the only three versions & service packs of Windows I have ready access to: