Limit amount of lines concatenated from list BATCH - batch-file

I am trying to make a batch file that will concatenate a list but I want it to make a new line after 13 items. Below is the code that I am using to pull the data into one line but I am having issues limiting it. I have tried putting a counter on it but I am not implementing it correctly. Any help would be appreciated.
#echo off
setlocal enableextensions enabledelayedexpansion
set "var="
for /f "usebackq delims=" %%a in (%~1) do set "var=!var!%%a "
echo(%var%
)

Your supplied code did not really attempt the task, and you did not make clear what you meant by concatenating. What the following example does therefore, is write a new file, defined on line 4 with the content from your input file argument, %1, but with the content of each block of thirteen lines of input per line of output. I have separated each original line with a single whitespace character, based upon %%a in your own code.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "InputFile=%~1"
Set "OutputFile=concatenated.txt"
If Not Exist "%InputFile%" Exit /B
For /F %%G In ('Copy /Z "%~f0" NUL') Do Set "CR=%%G" & (Set LF=^
% 0x0A %
)
SetLocal EnableDelayedExpansion
Set "i=0"
1>"%OutputFile%" (For /F UseBackQ^ Delims^=^ EOL^= %%G In ("%InputFile%"
) Do (Set /A "i += 1, # = i %% 13"
If !#! Equ 0 (Set /P "=%%G!CR!!LF!" 0<NUL) Else Set /P "=%%G " 0<NUL))
EndLocal

Related

Semicolons at beginning of the line, delims and FOR cycle

I cannot figure out how to correctly identify an empty/undefined variable having two semicolon one after one other or the line that starts with it.
This is the cycle:
for /F "delims=; tokens=1-7" %%m IN (testlist.txt) DO echo FUNCGROUP=%%r a=%%m b=%%n c=%%o d=%%p e=%%q f=%%s
I also tried adding "eol=;" and "......eol=" without success.
This is content of the first line of the file testlist.txt:
;xxxxxx;Active;;FALSE;con ter - dong;HWID000001;Item;sites/coll-
The result I need is, for the first cycle:
a=
b=xxxxxx
c=Active
d=
e=FALSE
f=con ter - dong
g=HWID000001
Thanks for any help.
To achieve that, you need to put a "NULL value" for empty fields in the lines of the file.
As there is no direct string substitution utilities with batch, you have to make substitutions beforehand to create "empty" fields. I suggest you to use a "NULL" character for empty fields like unbreakable space (Alt+0160).
In your case, this gives :
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "eol= tokens=*" %%l in (file.txt) DO (
SET "LINE=%%l"
SET "LINE=###!LINE:;;=; ;!###"
SET "LINE=!LINE:###;= ;!"
SET "LINE=!LINE:;###=; !"
SET "LINE=!LINE:###=!"
for /F "delims=; tokens=1-7" %%m in ("!LINE!") DO (
SET "RES=FUNCGROUP=%%r a=%%m b=%%n c=%%o d=%%p e=%%q f=%%s"
echo !RES: =!
)
)
Note that the SET "LINE=###!LINE:;;=; ;!###", SET "LINE=!LINE:###;= ;!" and the SET "LINE=!LINE:;###=; !" sections use the unbreakable space (Alt+0160) and replace beginning ";" with "Alt+0160;", the ending ";" with ";Alt+0160" and any following ";;" with ";Alt+0160;". The for loop parses then correctly the line, and next you just have to remove the unbreakable space to get "empty" variables.
EDIT: As brightly suggested by #jeb in the comments, you can also use quotes to handle empty fields. Each for loop variables can be then directly and simply unquoted.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "eol= tokens=*" %%l in (file.txt) DO (
SET "LINE=%%l"
SET "LINE=###^"!LINE:;=^";^"!^"###"
SET "LINE=!LINE:###^";=^"^";!"
SET "LINE=!LINE:;^"###=;^"^"!"
SET "LINE=!LINE:###=!"
for /F "delims=; tokens=1-7" %%m in ("!LINE!") DO (
echo FUNCGROUP=%%~r a=%%~m b=%%~n c=%%~o d=%%~p e=%%~q f=%%~s
)
)
Unfortunately, you have not provided sufficient information for what you intend to do with your empty field, so this is only a demonstration to provide you with output similar to that which you've indicated in your question:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims==" %%G In ("(Set Field[) 2>NUL") Do Set "%%G="
Set "i=0"
For /F UseBackQ^ Delims^=^ EOL^= %%G In ("testlist.txt") Do Call :GetFields "%%G"
Pause
GoTo :EOF
:GetFields
Set "Record=%~1"
Set /A i += 1
SetLocal EnableDelayedExpansion
Set "Index=1"
Set "Field[!Index!]=%Record:;=" & Set /A Index +=1 & Set "Field[!Index!]=%"
Echo(&Echo Record !i!
For /L %%G In (1,1,!Index!) Do If Not Defined Field[%%G] (Echo Field[%%G]=) Else Set Field[%%G]
Exit /B

windows batch escape whole string

Ok. this is a self updating batch file. I just simplified the problem from a bigger file.
this is a windows batch file(.bat) that upon execution should open itself and update first line
SET variableName=D:\Data
setlocal enableextensions enabledelayedexpansion
set /A i=0
for /f "tokens=*" %%f in ('type "%0"^&cd.^>"%0"') do (
set /A i=!i!+1
if !i! EQU 1 (
echo SET variableName=D:\Data2>>%0
) else (
echo %%f>>%0
)
)
endlocal
so let explain the situation.
i have !i! variable in lines 5 and 6. after executing this file, the variable in each line will replace by line number. it obviously because of echo %%f>>%0 that could not ignore and escape variable.
and my question is how to solve this problem?
another less problem is that the above code ignores spaces at beginning of line (indents) and generates a flat file.
the result of executing this file is:
SET variableName=D:\Data2
setlocal enableextensions enabledelayedexpansion
set /A i=0
for /f "tokens=*" %%f in ('type "%0"^&cd.^>"%0"') do (
set /A i=5+1
if 6 EQU 1 (
echo SET variableName=D:\Data2>>%0
) else (
echo %%f>>%0
)
)
endlocal
Stopping expansion of the variable when executing the file is as simple as turning delayed expansion off prior to the line that updates the file, and pairing it with an endlocal.
Retaining the space / tab formatting is achieved by including delims= in the For loop options.
Set variableName=D:\Data
Setlocal enableextensions enabledelayedexpansion
Set /A i=0
For /f "tokens=* delims=" %%f in ('type "%0"^&cd.^>"%0"') do (
Set /A i+=1
If !i! EQU 1 (
Echo SET variableName=D:\Data2>>%0
) Else (
Setlocal DisableDelayedExpansion
Echo(%%f>>%0
Endlocal
)
)
Endlocal
set "variableName=D:\Data"
setlocal enableextensions enabledelayedexpansion
rem !test! exclaimations, %test% percentages
for /f "skip=1 delims=" %%A in ('
type "%~f0" ^&
^> "%~f0" echo set "variableName=D:\Data2"
') do (
setlocal disabledelayedexpansion
>> "%~f0" echo %%A
endlocal
)
endlocal
You can avoid counting as skip=1 can be used to skip the first line. Use delims= to avoid delimiting the line. tokens=* ignores delimiters at start of the line and get the remainder of the line so that can be omitted for this task.
The new first line is now in the for loop command instead of erasing the file to empty. If you echo more lines, then increase the skip number.
Also may need to use setlocal disabledelayedexpansion so exclamation marks are retained.
Modifying the same file that is being read can a risk, though I assume you understand the risk.

How to extract a word after specific character in batch program?

For example I have a text file test.txt that contains like this:
Item1 $23 Item2 $24
Item3 $25
When I run this script:
#echo off
for /f "delims=$ tokens=1*" %%A in (test.txt) do echo %%B >> result.txt
The result is:
23 Item2 $24
25
I want the result is:
23
24
25
How should I repair the script?
Try the following script (see the explanatory rem remarks in the code):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_IFILE=test.txt"
set "_OFILE=result.txt"
rem // Store line-break in variable:
(set ^"LF=^
%= blank line =%
^")
rem // Write to resulting file:
> "%_OFILE%" (
rem // Loop through lines of text file:
for /F "usebackq delims=" %%L in ("%_IFILE%") do (
rem // Store current line in variable:
set "LINE=%%L"
rem // Toggle delayed expansion to be able to write and read variable in same code block:
setlocal EnableDelayedExpansion
rem // Replace each `$` by line-break and loop over resulting lines skipping the first one:
for /F "skip=1" %%K in (^"!LINE:$^=^%LF%%LF%!^") do (
rem // Return first token of each line:
echo(%%K
)
endlocal
)
)
endlocal
exit /B
At first, this replaces every $ by a line-break, so the first line of test.txt becomes:
Item1
23 Item2
24
And the second one becomes:
Item3
25
Then it extracts the first SPACE- or TAB-separated token from each of the newly formed multi-line strings, skipping the first line, so only the numbers are returned.
I have decided to post this example batch-file to show a method of parsing each line and returning every other string, (doesn't work with poison characters).
#(For /F "UseBackQDelims=" %%A In ("test.txt")Do #(Set "i=0"
For %%B In (%%A)Do #(Set /A "i=(i+1)%%2"&Cmd /V:On /Q /C Exit !i!
If Not ErrorLevel 1 Echo %%B)))>"results.txt"
In your posted example code and 'wanted' result, you could probably use that idea to output the strings with their first character removed if it is a $.
#(For /F "UseBackQDelims=" %%A In ("test.txt")Do #(Set "i=0"
For %%B In (%%A)Do #(Set /A "i=(i+1)%%2"&SetLocal EnableDelayedExpansion
If !i! Equ 0 (Set "Val=%%B"&If "!Val:~,1!"=="$" (Echo !Val:~1!
)Else Echo %%B)&EndLocal)))>"results.txt"
This should do exactly what you want.
#echo off
setlocal enabledelayedexpansion
(for /f "usebackq tokens=* delims= " %%i in ("test.txt") do (
set "_br=%%i"
echo(!_br: =^
!
)
)>_tmp
(for /f "delims=$ tokens=*" %%a in ('type _tmp ^| find "$"') do echo %%a)>result.txt
Note that you must copy the script as is. The extract of the script as show below does the replacement of whitespace with new line and therefore cannot change at all, the position and the extra line must remain as is for this to work as intended.
echo(!_br: =^
!
This script just splits each word separated by a space and redirects it to a new file, we then specifically search for the words containing $ and redirect those hits only.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%A in (test.txt) do (
for %%T in (%%A) do (
set "term=%%T"
if "!term:~0,1!"=="$" echo !term:~1!
)
)>> result.txt
endlocal
Read each line to %%A. Tokenise to %%T. Assign each token in turn to term, examine the first character and if $, echo the term, except the first character.

Batch - Extract every word on a line to a markdown link

This is code from a call above it. I extract line 5 from a file with this code which leaves it in %%c. I then want to take each word in that line (there can be from 1 to nth words) and create a markdown link like
[word](word.html)
and append those to a txt file. This is what I have so far. If I have 10 words do I really have to add all the tokens in the command like %%d, %%e, etc.? Can I use something like tokens=1-*. * meaning the last token in the line? Using tokens=* uses the whole line of words as one token I believe.
setlocal enabledelayedexpansion
set "lineNr=5"
set /a lineNr-=1
for /f "usebackq delims=" %%c in (`more +!lineNr! "%~1"`) DO (
for /f "tokens=1-2 delims= " %%d in ("%%c") do (
echo [%%d]^(tags/%%d.html^) [%%e]^(tags/%%e.html^) ^<br^> >> index.txt
)
goto :eof
)
Here is an example of how you can do that. Note I just used some parts as an example as I do not have time to format escape characters now, but you'll get the idea :)
#echo off
set "lineNr=5"
set /a lineNr-=1
for /f "usebackq delims=" %%c in (`more +%lineNr% "%~1"`) DO set "line=%%c"
for %%i in (%line%) do echo| set /p =[%%i](tags/%%i.html)>>index.txt
OK, I got it working by doing this -
set "lineNr=5"
set /a lineNr-=1
for /f "usebackq delims=" %%c in (`more +%lineNr% "overview.md"`) DO (
set "line=%%c"
goto :next
)
:next
for %%i in (%line%) do echo| set /p =[%%i](tags/%%i.html) >> index.txt
Have to break out of the loop line listing after the first line wanted(line 5).

Batchfile: read last lines from logfiles and copy them to a new file

This is my first posting so if the format is not as it supposed to be please excuse me for this. (Suggestions for
improvement are welcome.)
I am trying to create a batchfile that will read last lines from logfiles and copy them to a new file.
Until now I have found here a way to read the last line.
Code would be something like:
for /f %%i in ('find /v /c "" ^< someFile.txt') do set /a lines=%%i
set /a startLine=%lines% - 1
more /e +%startLine% someFile.txt > lastLines.txt
The above code works for one file at a time. What I need is to read the last line from all files in a known list and add this line to a new .csv file.
I have been using the following code for getting the 4th entry in the logfiles but it returns every line of every logfile:
for /f %%x in (%list%) do for /f "delims=.txt, tokens=4" %%i in (%%x.txt) do echo %%x, %%i >> output.csv
What I would need is a sort of combination of both but I don't know how to combine them and make the complete last line be copied to the .csv file.
===
#Magoo:
Thanx for your reaction.
In every logfile can be 1 to >100 lines with comma separated information. Something like:
"LOGON,6-1-2015,12:43:39,USERNAME,HOSTNAME,,,,192.168.209.242,00:21:5A:2E:64:5E"
The last code with the 4th entry was used to get a list of all accounts that had logged in to the computers. This code gave me a very large list of all logon/logoff events on all computerlogs I checked in %list%.
In %list$ I had all the names of logfiles I wanted to be checked. This returned all lines.
For a new batchfile I need only the last logon/logoff entry and I want the whole last line.
So I have a .txt file with the hostnames of all computers I need to examine.
This .txt file will be read line by line via the variable %list%.
From every logfile I need only the last line copied to an output file.
===
I just tried the solution offered by JosefZ. Unfortunately this does not work for me yet. No lastlines are copied to the resultfile. In the code I removed the extra entry for possible lastlines for there are no empty lines in the logs, I also added an entry for the hostname I want to be available in the result. JosefZ had the filename there:
#ECHO OFF >NUL
#SETLOCAL enableextensions disabledelayedexpansion
type nul>output.csv
set "list=_listing.txt"
for /F "tokens=*" %%x in ('type "%list%"') do (
set "host=%%~x"
for /F "tokens=*" %%G in ('type "%%~x"') do set "lastline=%%G"
call :lline
)
:endlocal
#ENDLOCAL
goto :eof
:lline
set "filename=.\logs\%filename:&=^&%.txt"
echo %host%,%lastline%>>output.csv
goto :eof
The resultfile shows only the hostnames. I'll puzzle some more with this but all tips are welcome!
===
Got it!!!
#ECHO OFF >NUL
#SETLOCAL enableextensions disabledelayedexpansion
type nul>output.csv
set "list=_listing.txt"
for /F "tokens=*" %%x in ('type "%list%"') do (
set filename= :: *empty previous filename*
set lastline= :: *empty previous lastline*
set "host=%%~x"
set "filename=.\logs\%host%.txt" :: *creating the filename from path+hostname+extention*
for /F "tokens=*" %%G in ('type "%filename%"') do set "lastline=%%G"
call :lline
)
:endlocal
#ENDLOCAL
goto :eof
:lline
echo %host%,%lastline%>>output.csv
goto :eof
Your approach with line numbering could fail if a file has more trailing empty lines. Fortunately for /F loop ignores (does not iterate) empty lines; let's put to use this feature: in the script used next practices:
disabledelayedexpansion to allow ! in file names
set "list=_listing.txt" where the _listing.txt contains list of file names (full path and extension .txt including), one file name on one line: got by dir /b /s *.txt>_listing.txt
type nul>files\output.csv to empty the output file (optional)
set "lastline=!!!file empty!!!" to initialize variable %lastline%; could be set "lastline=" as well
call :lline to process variables %filename% and %lastline%
set "filename=%filename:&=^&%" to allow & in file names
The script is as follows:
#ECHO OFF >NUL
#SETLOCAL enableextensions disabledelayedexpansion
type nul>files\output.csv
set "list=_listing.txt"
for /F "tokens=*" %%x in ('type "%list%"') do (
set "filename=%%~x"
set "lastline=!!!file empty!!!"
rem the whole line
for /F "tokens=*" %%G in ('type "%%~x"') do set "lastline=%%G"
rem the fourth token only
rem for /F "tokens=4" %%G in ('type "%%~x"') do set "lastline=%%G"
call :lline
)
:endlocal
#ENDLOCAL
goto :eof
:lline
set "filename=%filename:&=^&%"
echo %filename% %lastline%
rem >>files\output.csv
goto :eof
Sample _listing.txt file:
d:\bat\files\1exclam!ation.txt
d:\bat\files\2exc!lam!ation.txt
d:\bat\files\11per%cent.txt
d:\bat\files\12per%cent%.txt
d:\bat\files\17per%Gcent.txt
d:\bat\files\18per%%Gcent.txt
d:\bat\files\21ampers&nd.txt
d:\bat\files\22ampers&&nd.txt
Output:
d:\bat>lastlines
d:\bat\files\1exclam!ation.txt 0 15.01.2015 1:52:28.48 -15072 20465
d:\bat\files\2exc!lam!ation.txt 6 15.01.2015 1:52:28.50 3250 16741
d:\bat\files\11per%cent.txt -8 15.01.2015 1:52:28.50 -3692 27910
d:\bat\files\12per%cent%.txt !!!file empty!!!
d:\bat\files\17per%Gcent.txt 0 15.01.2015 1:52:28.56 14508 12374
d:\bat\files\18per%%Gcent.txt 1 15.01.2015 1:52:28.56 30540 26959
d:\bat\files\21ampers&nd.txt 15.01.2015 1:22:50.18
d:\bat\files\22ampers&&nd.txt 15.01.2015 1:22:50.18
Honestly, all that ballast is for (possibly) trailing empty lines in files and for (possibly) ! and & in file names only; all could be done with
for /f %%x in (%list%) do for /f "skip=%startLine% tokens=4" %%i in (%%x) do echo %%x, %%i >> output.csv
You should use a simple FOR to iterate a list of values, not FOR /F.
Something like the following should work:
#echo off
setlocal enableDelayedExpansion
>>output.csv (
for %%F in (
"file1.log"
"file2.log"
"file3.log"
etc.
) do (
for /f %%A in ('find /v /c "" <%%F') do set /a skip=%%A-1
more +!skip! %%F
)
)
The quotes around the file names are there in case you get a name with spaces.
You could use your LIST variable if it looks something like
set LIST="file1.log" "file2.log" "file3.log" etc.
#echo off
setlocal enableDelayedExpansion
set LIST="file1.log" "file2.log" "file3.log" etc.
>>output.csv (
for %%F in (%LIST%) do (
for /f %%A in ('find /v /c "" <%%F') do set /a skip=%%A-1
more +!skip! %%F
)
)
If any of your file names contain the ! character, then you must toggle delayed expansion ON and OFF within your loop. Otherwise the delayed expansion will corrupt the names when %%F is expanded.
#echo off
setlocal disableDelayedExpansion
set LIST="file1.log" "file2.log" "file3.log" etc.
>>output.csv (
for %%F in (%LIST%) do (
for /f %%A in ('find /v /c "" <%%F') do set /a skip=%%A-1
setlocal enableDelayedExpansion
more +!skip! %%F
endlocal
)
)

Resources