I have around 3000 txt file and I want to split each file into two separate txt files based on specific phrase, for example section one and section two.
Each file looks like this :
Section one
Xxxxxxxxx
Xxxxxxxxxx
Xxxxxxxxx
Section two
Xxxxxxxxxc
Xxxxccxccc
Xxxxxxxcxx
I want to have section one and section two in two separate txt files.
Please suggest or advise me any help to perform this task. I heard about batch file but I have no idea how it works.
I'm using windows 10.
Many thanks in advance.
I agree to the previous comments. If you want to know what's going on, you should use your own efforts in reading the help for the commands used here (ie for /?, findstr /?, ...).
Despite the above, give this a try. It may need modifications involving handling paths or whatever your real task may be. Try your own efforts and then come back to show your progress, where are you stuck, and may be find a solution.
This script split txt files where section two is found and changes extension from blabla.txt to blabla.one.txt and blabla.two.txt each of these containing the splitted version of the original file.
The strings Section ... are supposed to be at the beginning of the line.
#echo off
for /F %%f in ('dir /B *.txt') do (
for /F "usebackq tokens=1-3 delims=: " %%1 in (`findstr /B /N "Section" "%%f"`) do (
if /I "%%3" EQU "two" call :split %%f, %%1
)
)
exit/B
:split
Setlocal
set "fileone=%~1" & set "filetwo=%~1" & set/a split=%2
set "fileone=%fileone:.=.one.%"
set "filetwo=%filetwo:.=.two.%"
copy NUL "%fileone%">NUL
copy NUL "%filetwo%">NUL
for /F "tokens=1,* delims=[]" %%a in ('"type "%~1"|find /N /V """') do (
if %%a lss %split% (
echo(%%b>>"%fileone%"
) else (
echo(%%b>>"%filetwo%"
)
)
Endlocal
exit/B
EDIT Did a rework of my batch to get all files and circumvent recursing already splittet files
#Echo off
For /f "Delims=" %%F in ('Dir /B/S File*.txt^|Findstr /v "_"'
) Do Set "Fout=NUL"&Set "Fin=%%~F"&For /f "Tokens=1*Delims=:" %%A in (
'Findstr /N /V §³$ %%F') Do Call:Sub %%B
Goto :Eof
:Sub
If %1. Equ Section. Call :SetFout %Fin% %2
>>"%Fout%" Echo:%*
Goto :Eof
:SetFout
Set "Fout=%~dpn1_%2%~x1"
Type NUL>"%Fout%"
Some explanations
findstr /N is used to number the lines (required otherwise empty
lines would be dropped)
/V §³$ means every line NOT containing these chars, ergo all.
for /f parses the line and cuts of the number and the remainder of the
line is an arg to the call :sub
if the first word is Section Sub :SetFout is called to build a new name Fout from Fin (In file) by appending _secondWord to it (one, two) The new file is set to NUL
All other lines are simply copied to the current Fout
Related
I am looking for one solution, which will help me to move files to folders with similar name.
I have filenames like TEST1_2018P2.xlsx, TEST2_2018P2.xslx, etc.
And I have folders with names TEST1_City1, TEST2 City2...
What I need is to move file TEST1_2018P2.xlsx to folder TEST1_City1, TEST2_2018P2.xslx to TEST2 City2 and so on.
How can I do that?
Here's my latest code, which is also not working.
#ECHO OFF
SETLOCAL
SET "sourcedir=my_folder"
SET "destdir=my_folder"
FOR /f "delims=" %%a IN ( 'dir /b /a-d "%sourcedir%\*.xlsx" ' ) DO (
FOR /f "tokens=1delims=" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN ( 'dir /b /ad "%destdir%\*%%b*" ' ) DO (
ECHO(MOVE "%%a" "%destdir%\%%d\"
)
)
)
GOTO :EOF
I'm not sure of your exact task, so this relatively basic example should move any .xlsx file to the first existing directory whose name matches the portion of the filename up to the underscore, plus a space.
Adjust the values on lines 2 and 3 to match your actual directory specs, (without trailing backslashes).
#Echo Off
Set "SourceDir=my_folder"
Set "DestDir=my_folder"
For /F Delims^=^ EOL^= %%A In ('Dir /B/A-D-L "%SourceDir%\*_*.xlsx" 2^>Nul'
) Do Call :Sub "%%A"
GoTo :EOF
:Sub
Set "DirName=%~1"
Set "DirName=%DirName:_="&:"%"
For /F Delims^=^ EOL^= %%A In ('Dir /B/AD-L "%DestDir%\%DirName% *" 2^>Nul'
) Do If Exist "%SourceDir%%~1" Move /Y "%SourceDir%\%~1" "%DestDir%\%%A" 2>Nul
Exit /B
It was not designed to be the most efficient method of performing the task!
Please also note that your existing directory names did not have a clear pattern so this was written for TEST1 City1 TEST2 City2 etc.
If they are all underscores, e.g. TEST1_City1 TEST2_City2 etc. then change "%DestDir%\%DirName% *" on line 11 to "%DestDir%\%DirName%_*".
If the directories can be either of them, and you are sure that no two directories will begin with the string TEST1, TEST2 etc., (which would limit you to only numbers 0..9 in this case), you could probably use "%DestDir%\%DirName%?*" on line 11 as an alternative.
Assuming that actual text of TEST1 doesn't contain any _ characters, you can use:
#echo off
setlocal EnableDelayedExpansion
cd /d "your_folder"
for /F "delims= eol=" %%A IN ('dir /B /A-D "TEST*_2018P2.xlsx"') do (
for /F "tokens=1 delims=_" %%B IN ("%%A") do (
rem Define some important variables:
set "token_1=%%B"
set "num_test=!token_1:~-1!"
set "foldername=!token_1!_City!num_test!"
md "!foldername!\" >nul 2>&1
move "%%~fA" "!foldername!\"
)
)
Let me explain my code:
The first for /F loop is used to find all the files you want (TEST*_2018P2.xlsx) excluding all directories (/A-D) and headers. delims= and eol= options are used: loop through the whole line without skipping lines starting with ;.
The second for /F loop is used to get the first token of the output of the first loop (IN ("%%A")).
The first token is set to token_1 variable and then substract the last number/letter from it setting it to the num_test variable.
A foldername is set because it is used two time, it is really hard to understand this code without setting it in a variable. It is actually set by token_1 variable (TESTn), _City and n (number).
A folder is created with that name. Both STDIN and STDERR are redirected to nul. This happens not to have many processed if exist statement. The current file (%%~fA; full path) is moved to this folder.
Remember to replace "your_folder" with your actual folder!
I have some of log files formatted like this "name.log"
I would like to copy those from one folder to another folder like
xcopy /y "C:\Folder1" "D:\Folder2"
Adding I need to rename file with created date of original file (no copy file) so that the text file in Folder2 would be like "yyyymmddhhmm.log" if some file has the same name (date of creation) it will be overwritten.
I have a code with help of #Wes Larson but there is something wrong.
set Source=C:\Users\user1\Desktop\1
set Dest=C:\Users\user1\Desktop\2
if not exist %Dest% md %Dest%
for /F %%a in ('dir /b "%Source%\*.txt"') do call :Sub %%a
goto :eof
:Sub
set "filename=%1"
for /F %%s in ("%Source%\%1") do if %%~zs==0 goto :eof
set "datepart="
FOR /F "tokens=1-5 delims=/-: " %%a IN ('dir /tc "%filename%" ^| findstr "%filename%"') DO (
IF "%%c" neq "" SET "datepart=%%c%%a%%b%%d%%e"
)
FOR /F %%a IN ("%filename%") DO (
set "NewName=%%~na %datepart%%%~xa"
)
xcopy /y "%Source%\%filename%" "%Dest%\%NewName%*"
GOTO :EOF
The problem is that If I don't put the .bat in the same folder that origin files (Folder1),some files aren't change name. For example, if it is out some files change name with old name and one white space. The command windows tell me that it doesn't find the file when it get the creation date. It's very strange because some files are copied well.
What do I need to solve this problem?
Before we get to an answer: When troubleshooting/debugging batch scripts, don't use #echo off. It's great once you have it working the way you want, but comment it out when you need to see what your code is doing line by line. Also, you'll want to open a cmd window and run your script from a command line, so the window doesn't close as soon as your script finishes.
Now on to your code. This for loop is part of your problem:
FOR /f "tokens=1-3delims=/-:" %%a IN ('dir /tc "%filename%"') DO IF "%%c" neq "" SET "datepart=%%a-%%b-%%c"
Firstly, you haven't set %filename%, so this loop will fail. You should probably have a line in your :Sub like this:
set "filename=%1"
Now, assuming that %filename% is fixed, by the time this loop has finished, your %%a, %%b, and %%c variables have been set to the values in the last line of output from the command 'dir /tc "%filename%"', which is something like X Dir(s) XXX,XXX,XXX,XXX bytes free, and isn't the information you're looking for.
So, instead, you can tweak it a little so that you pipe the output of dir to findstr, looking for just the one, single line that you want to use, like this:
FOR /f "tokens=1-3delims=/-:" %%a IN ('dir /tc "%filename%" ^| findstr "%filename%"') DO (
IF "%%c" neq "" SET "datepart=%%a-%%b-%%c"
)
But then, you have another problem: your %datepart% looks like MM-DD-YYYY hh. This is because you changed your delims from the default (which includes spaces and tabs), to only the specified characters. Also, from your question, you want to also include hour and minute all formatted as "yyyymmddhhmm.log" , which means you'll need those next two tokens, too. Then your line becomes this:
FOR /f "tokens=1-5 delims=/-: " %%a IN ('dir /tc "%filename%" ^| findstr "%filename%"') DO (
IF "%%c" neq "" SET "datepart=%%c%%a%%b%%d%%e"
)
Next, your nested for loop is confusing, unnecessary, and causing problems:
FOR /f %%a IN ("%filename%") DO FOR /f %%d IN ("%datepart%") DO (
set NewName="%%~na %%d%%~xa
xcopy e/d/y "%Source%\%OrgName%" "%Dest%\%NewName%"
)
You already have the %datepart% variable, and you don't need to turn it into one of the for %%d variables to be able to reference it. So it becomes much simpler when you do it like this:
FOR /f %%a IN ("%filename%") DO (set "NewName=%%~na %datepart%%%~xa")
Also, because you run into delayed expansion issues, it's simpler to take the xcopy command out of the loop.
And once again, you have a variable %OrgName% that you never set. Fortunately, this is actually unnecessary as you already have the original file name captured as %filename%. So your overly complicated nested for loop becomes this:
FOR /f %%a IN ("%filename%") DO (
set "NewName=%%~na %datepart%%%~xa"
)
echo xcopy e/d/y "%Source%\%filename%" "%Dest%\%NewName%"
Firstly, there are a couple of similar questions on here to this (Rename file based on file Content batch file being the one I have tried to work an answer from - but I have no real clue what I'm doing), however I cannot find anything that meets my exact needs, and this is my first real foray into batch programming so the syntax is fairly new to me.
The question:
I have several hundred text files, with different names, where the header is formatted like so:
"Event Type : Full Histogram"
"Serial Number : xxxxxx"
"Version : V 10.60-8.17 "
"File Name : W133FA0Z.580H"
"Histogram Start Time : 12:39:08"
"Histogram Start Date : 2014-04-11"
I would like if possible to create a batch file to rename all the files in the folder to the format of:
StartDate StartTime
so for this example:
2014-04-11 12:39:08
My problems lie in the fact I'm not sure how to actually point it to where to find the string if it was for just one line (I've tried editing the answers in the question I posted above). And, futhermore, I have no idea how to add a second bit of code to find the StartTime string and then append that to the StartDate.
Thanks in advance,
Chris
Here is a very efficient method.
#echo off
pushd "pathToYourFolderContainingFilesToRename"
for /f "tokens=1,3 delims=:" %%A in (
'findstr /bc:^"\"Histogram Start Date :" *.txt'
) do for /f delims^=^"^ %%C in (
"%%B"
) do for /f tokens^=4-6^ delims^=^":^ %%D in (
'findstr /bc:^"\"Histogram Start Time :" "%%A"'
) do ren "%%A" "%%C %%D.%%E.%%F.txt"
popd
The 1st loop serves two purposes. It establishes file names that contain the start date string, as well as also returning the date string for each file.
The 2nd loop strips out spaces and quotes from the date string.
The 3rd loop parses out the start time from the file.
The 2nd and 3rd loops have very awkward syntax to enable including a quote in the list of delimiters. The 2nd loop sets DELIMS to a quote and a space. The 3rd set DELIMS to quote, colon, and a space.
Assuming you JUST have file formatted like in your description in the working directory :
#echo off&cls
setlocal EnableDelayedExpansion
for %%x in (*.txt) do (
set /a $sw=1
for /f "skip=4 tokens=2-4 delims=:" %%a in ('type "%%x"') do (
if !$sw! equ 1 set $Time=%%a-%%b-%%c
if !$sw! equ 2 (
set $Date=%%a
call:DoRen !$Time:~1,-1! !$Date:~1,-1! %%~nx%%~xx)
set /a $sw+=1
)
)
exit/b
:DoRen
echo ren "%3" "%2 %1"
If the output is OK you can remove the echo
The following will get the output you want, where the output will look like 2014-04-11 123908.
#echo off
set file=test.txt
for /f "delims=: tokens=2-4" %%a in ('find "Time" %file%') do set ftime=%%a%%b%%c
for /f "delims=: tokens=2" %%a in ('find "Date" %file%') do set fdate=%%a
echo %fdate:~1,-1% %ftime:~1,-1%
If all the files are in the same directory, then you can simply do this in a another for loop.
#echo off
setLocal enableDelayedExpansion
for /f %%f in ('dir C:\whatever\path\*.txt /B') do (
for /f "delims=: tokens=2-4" %%a in ('find "Time" %%a') do set ftime=%%a%%b%%c
for /f "delims=: tokens=2" %%a in ('find "Date" %%a') do set fdate=%%a
ren "%%~a" "!fdate:~1,-1! !ftime:~1,-1!.txt"
)
This will rename all text files in a specified directory the date and time in their contents. Note that this does not account for text files that do not have the date and time in their contents. You can (and probably should) add that as you see fit.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
DEL incorrectformat.log 2>nul
DEL alreadyprocessed.log 2>nul
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%\*.txt" ') DO (
SET keepprocessing=Y
SET "newname="
FOR /f "tokens=1-5delims=:" %%b IN (
'TYPE "%sourcedir%\%%a"^|findstr /n /r "$" ') DO IF DEFINED keepprocessing (
IF %%b==1 IF NOT "%%~c"=="Event Type " SET "keepprocessing="&>>incorrectformat.log ECHO %%a
IF %%b==5 SET newname=%%d%%e%%f
IF %%b==6 (
SET "keepprocessing="
SET "newname=%%d!newname!.txt"
SET "newname=!newname:"=!"
SET "newname=!newname:~1!"
IF "!newname!"=="%%a" (>>alreadyprocessed.log ECHO(%%a) ELSE (ECHO REN "%sourcedir%\%%a" "!newname!")
)
)
)
GOTO :EOF
Here's my version.
You'd need to set the value of sourcedir to your target directory.
A list of files not matching the specified format is produced as incorrectformat.log
A list of already-processed files is produced as alreadyprocessed.log
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
I am looking to find a batch or VBS solution to strip out lines in a program generated text file with the extension of .trs.
In every .trs file created there is a line that contains the word 'labour'. I need every line after the line that contains the word labour to be deleted.
The .trs files are all stored in c:\export
I have searched for this but some of the commands were well over my head. Could anyone be so kind as to offer me a cut and paste open of the whole batch file, please.
I believe this is the code you are looking for (in a batch file) to remove all of the lines above the word "labour". Let me know if modifications need to be made to the code (such as if there are more than one instance of "labour" in the file).
#echo OFF
setLocal EnableDelayedExpansion
cd C:\export
for /f "delims=" %%I in ('findstr /inc:"labour" "test.trs"') do (
set /A"line=%%I"
)
set count=0
for /f "delims=" %%A in (test.trs) do (
If !count! GEQ %line% goto ExitLoop
echo %%A >>temp.txt
set /A count+=1
echo !count!
)
:ExitLoop
type temp.txt > test.trs
del temp.txt
endlocal
OUTPUT:
test.trs (BEFORE changes)
this
is
a
labour
test
of
the
results
test.trs (AFTER changes)
this
is
a
Here is an alternate method to process every .trs file in "C:\export":
#echo off
if not exist "C:\export\*.trs" goto :EOF
if exist "C:\export\queue.tmp" del /q "C:\export\queue.tmp"
for /f "tokens=*" %%A in ('dir /b "C:\export\*.trs"') do (
for /f "tokens=1,2 delims=:" %%B in ('findstr /inc:"labour" "C:\export\%%A" ^| findstr /n .*') do if "%%B" equ "1" set LineNumber=%%C
for /f "tokens=1* delims=:" %%D in ('findstr /n .* "C:\export\%%A"') do if %%D lss %LineNumber% echo.%%E>>"C:\export\queue.tmp"
move /y "C:\export\queue.tmp" "C:\export\%%A">NUL
)
First, I do some error checking to avoid things that would break the script. Next, I pull a list of .trs files stored in C:\export, and loop through each file.
I use 'findstr /inc:"labour" "C:\export\%%A"' to get the line number where "labour" is found in the current file, then pipe it into 'findstr /n .*' to number the results in case more than one match is found.
I then use a for loop with "tokens=1,2 delims=:" to find the first result (if "%%B" equ "1") and store the line number (set LineNumber=%%C).
Next, I use 'findstr /n .* "C:\export\%%A"' to read every line of the file, "tokens=1* delims=:" to separate out the line numbers again, then copy all the data to a temp file until %LineNumber% has been reached. This method of reading the file (using findstr and numbering the lines) also ensures that no blank lines will be skipped by the for loop.
Finally, I replace the original file with the temp file, then loop through to the next file.
I tried to keep the above code as slimmed down as possible. Here is the same script with formatting, comments, visual feedback, and user-definable variables:
#echo off
::Set user-defined Variables
set FilePath=C:\export
set FileType=*.trs
set Keyword=labour
::Check for files to process and exit if none are found
if not exist "%FilePath%\%FileType%" echo Error. No files to process.&goto :EOF
::Delete temp file if one already exists
if exist "%FilePath%\queue.tmp" del /q "%FilePath%\queue.tmp"
::List all files in the above specified destination, then process them one at a time
for /f "tokens=*" %%A in ('dir /b "%FilePath%\%FileType%"') do (
::Echo the text without a line feed (so that "Done" ends up on the same line)
set /p NUL=Processing file "C:\export\%%A"... <NUL
::Search the current file for the specified keyword, and store the line number in a variable
for /f "tokens=1,2 delims=:" %%B in ('findstr /inc:"%Keyword%" "%FilePath%\%%A" ^| findstr /n .*') do (
if "%%B" equ "1" set LineNumber=%%C
)>NUL
::Output all data from the current file to a temporary file, until the line number found above has been reached
for /f "tokens=1* delims=:" %%D in ('findstr /n .* "%FilePath%\%%A"') do (
if %%D lss %LineNumber% echo.%%E>>"%FilePath%\queue.tmp"
)>NUL
::Replace the current file with the processed data from the temp file
move /y "%FilePath%\queue.tmp" "%FilePath%\%%A">NUL
echo Done.
)
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: