bat to replace text from text file1 to file2 (relative search) - batch-file

textfile1 is source and textfile2 is target
Example textfile1.txt:
server1.net 2100 /l /n /k port:2000,server2.net 2100 /l /n /k port:20000
textfile2.txt:
server3.net 2000 /l /k port:xxxx,server4.net 2000 /l /k port:yyyyy
I need to find "port:" characters in textfile1.txt and subst the port number (n character after "port:" characteres), in textfile2.txt
Note: portnumber is variable number in textfile1, but text: "port:" is fixed.
Thanks
Basically I need to put the same port numbers in textfile1 and textfile2

The Batch file below works with an unlimited list of servers in both text files and allows the port:nnnn option be included at any place in the line. It does not check for any errors to made it simpler.
#echo off
setlocal EnableDelayedExpansion
rem Read data from both files
set /P "data1=" < textfile1.txt
set /P "data2=" < textfile2.txt
rem Replace "port" data in all servers
:nextServer
rem Get port number after "port:" in first data and eliminate it
for /F "tokens=1* delims=," %%a in ("%data1:*port:=%") do set port1=%%a& set data1=%%b
rem Replace port number in second data and change colon by semicolon
for /F "tokens=1 delims=," %%a in ("%data2:*port:=%") do set data2=!data2::%%a=;%port1%!
rem Pass to next server, if any
if defined data1 goto nextServer
rem Output result restoring colons
echo %data2:;=:%
For example, with this data:
server1.net 2100 /l /n /k port:2000,server2.net 2100 /l /n /k port:20000,server5.net 2100 /l /n /k port:1234
server3.net 2000 /l /k port:xxxx,server4.net 2000 /l /k port:yyyyy,server6.net 2000 /l /k port:zzzz
The output is:
server3.net 2000 /l /k port:2000,server4.net 2000 /l /k port:20000,server6.net 2000 /l /k port:1234

I don't see why you want to do this task with batch - it would be much simpler using another scripting language like VBScript, JScript, or perhaps PowerShell.
But here is a native batch solution. My solution makes no assumption as to the order of the options in each command. It allows for any number of options both before and after /port:nnnn.
#echo off
setlocal enableDelayedExpansion
::read the line from textfile1
set "str="
<"textfile1.txt" set /p "str="
if not defined str exit /b
::enclose each command in quotes
set "str="!str:,=","!""
::extract the port from each command into an array
set /a n=0
for %%A in (!str!) do (
set "cmd=%%~A"
set /a n+=1
for /f %%B in ("!cmd:*port:=!") do set "port!n!=%%B"
)
::read the line from textfile2
set "str="
<"textfile2.txt" set /p "str="
if not defined str exit /b
::enclose each command in quotes
set "str="!str:,=","!""
::process each command
set /a n=0
set "ln="
for %%A in (!str!) do (
set "cmd=%%~A"
REM break the command into 2 parts, discarding port:
REM %%B = before port
REM %%E = after port
for /f "tokens=1* delims=," %%B in ("!cmd:port:=,!") do (
for /f "tokens=1*" %%D in ("%%C") do (
set /a n+=1
REM transfer !n! into %%N
for %%N in (!n!) do (
REM build the new line
set "ln=!ln!,%%B port:!port%%N! %%E"
)
)
)
)
::remove space before comma
set "ln=!ln: ,=,!"
::write the result back to textfile2
>"textfile2.txt" echo !ln:~1!

A little more clarification on what you're looking to do in the long-term may help, but in the meantime, here is a script I just wrote and tested and it does exactly what you're asking.
for /f "tokens=6,12 delims=, " %%a in (textfile1.txt)do (
set portOne=%%a
set portTwo=%%b
call :writeTextTwo
)
type texttwo_temp>textfile2.txt
goto :EOF
:writeTextTwo
for /f "tokens=1-12 delims=, " %%c in (textfile2.txt)do (echo %%c %%d %%e %%f %portOne%,%%h %%i %%j %%k %portTwo% >>texttwo_temp )
goto :EOF
I hope this helps.

Related

How to search and replace strings that contain "=" characters in text file with Windows CMD (not powershell)

I wrote this code based on some other examples but just can't get it to work? (it's a .bat file)? The code writes the new file with all the old lines just won't edit the three lines right with the "=" character. Can someone point me in the right direction please.
This is what the INTOUCH.INI file looks like to start:
[InTouch]
AppMode=2
AppName0=test
AppName1=
AppName2=
AppName3=
AppDesc0=New InTouch application
AppDesc1=
AppDesc2=
AppDesc3=
SAOConverted=1
WinFullScreen=1
WinLeft=-4
WinTop=-4
WinWidth=1032
WinHeight=748
UseNewSendKeys=1
DebugScripts=0
UseBigBitmap=1
WindowViewerStartupIconic=0
CloseOnTransfer=0
And this is what is written:
[InTouch]
AppMode=2
AppName0=test
AppName1=
AppName2=
AppName3=
AppDesc0=New InTouch application
AppDesc1=
AppDesc2=
AppDesc3=
SAOConverted=1
1=WinFullScreen=0=1
WinLeft=-4
WinTop=-4
1032=WinWidth=1000=1032
748=WinHeight=700=748
UseNewSendKeys=1
DebugScripts=0
UseBigBitmap=1
WindowViewerStartupIconic=0
CloseOnTransfer=0
This is my .bat file code:
Set "OldString1=WinFullScreen=1"
Set "NewString1=WinFullScreen=0"
Set "OldString2=WinWidth=1032"
Set "NewString2=WinWidth=1000"
Set "OldString3=WinHeight=748"
Set "NewString3=WinHeight=700"
#ECHO OFF &SETLOCAL
cd /d F:\
for %%x in (INTOUCH.INI) do call:process "%%~x"
goto:eof
:process
set "outFile=%~n1_edited%~x1"
(for /f "skip=2 delims=:" %%a in ('find /n /v "" "INTOUCH.INI"') do (
set "ln=%%a"
Setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
if defined ln (
set "ln=!ln:%OldString1%=%NewString1%!"
set "ln=!ln:%OldString2%=%NewString2%!"
set "ln=!ln:%OldString3%=%NewString3%!"
)
echo(!ln!
endlocal
))>"%outFile%"
Exit /b
If you use the file's format to your advantage you can set the values of the new variables at the top of the script and then as you are reading the variable names from the settings file you can see if those variables are defined. If they are defined then output the new value, otherwise output the original value.
The trick to this is the double variable expansion you get when you use the CALL and ECHO commands together. First the for variable is expanded the name of the variable and then in the second phase of expansion the value of the variable is then expanded. That is the reason for the extra sets of percent symbols.
#echo off
Set "WinFullScreen=0"
Set "WinWidth=1000"
Set "WinHeight=700"
REM cd /d F:\
for %%F in (INTOUCH.INI) do set "outFile=%%~nF_edited%%~xF"
REM Read first line of file
set /p line1=<INTOUCH.INI
(echo %line1%
for /f "usebackq skip=1 tokens=1,2 delims==" %%G in ("INTOUCH.INI") do (
if defined %%G (
CALL echo %%G=%%%%G%%
) else (
echo %%G=%%H
)
))>"%outFile%"
Exit /b
#echo off
Set "AppMode=x"
Set "WinFullScreen=0"
Set "WinWidth=1000"
Set "WinHeight=700"
for /f "skip=1 usebackq tokens=1,2 delims==" %%G in ("a.INI") do call :proc "%%G" %%H
exit /b
:proc
set val=%2
for /F "tokens=* eol= " %%S in ("%~1") do set trimmed=%%S
call :getoverrideval %trimmed%
if "%override%" == "" (
echo %~1=%2%
) else (
echo %~1=%override%
)
goto :EOF
:getoverrideval
call set override=%%%1%%
Output:
C:\Users\w16coreeval>cmd /c a.bat
AppMode=x
AppName0=test
AppName1=
AppName2=
AppName3=
AppDesc0=New
AppDesc1=
AppDesc2=
AppDesc3=
SAOConverted=1
WinFullScreen=0
WinLeft=-4
WinTop=-4
WinWidth=1000
WinHeight=700
UseNewSendKeys=1
DebugScripts=0
UseBigBitmap=1
WindowViewerStartupIconic=0
CloseOnTransfer=0

Windows Batch FOR Loop improvement

I have a batch to check the duplicate line in TXT file (over one million line) with 13MB, that will be running over 2hr...how can I speed up that? Thank you!!
TXT file
11
22
33
44
.
.
.
44 (over one million line)
Existing Batch
setlocal
set var1=*
sort original.txt>sort.txt
for /f %%a in ('type sort.txt') do (call :run %%a)
goto :end
:run
if %1==%var1% echo %1>>duplicate.txt
set var1=%1
goto :eof
:end
This should be the fastest method using a Batch file:
#echo off
setlocal EnableDelayedExpansion
set var1=*
sort original.txt>sort.txt
(for /f %%a in (sort.txt) do (
if "%%a" == "!var1!" (
echo %%a
) else (
set "var1=%%a"
)
)) >duplicate.txt
This method use findstr command as in aschipfl's answer, but in this case each line and its duplicates are removed from the file after being revised by findstr. This method could be faster if the number of duplicates in the file is high; otherwise it will be slower because the high volume data manipulated in each turn. Just a test may confirm this point...
#echo off
setlocal EnableDelayedExpansion
del duplicate.txt 2>NUL
copy /Y original.txt input.txt > NUL
:nextTurn
for %%a in (input.txt) do if %%~Za equ 0 goto end
< input.txt (
set /P "line="
findstr /X /C:"!line!"
find /V "!line!" > output.txt
) >> duplicate.txt
move /Y output.txt input.txt > NUL
goto nextTurn
:end
#echo off
setlocal enabledelayedexpansion
set var1=*
(
for /f %%a in ('sort q42574625.txt') do (
if "%%a"=="!var1!" echo %%a
set "var1=%%a"
)
)>"u:\q42574625_2.txt"
GOTO :EOF
This may be faster - I don't have your file to test against
I used a file named q42574625.txt containing some dummy data for my testing.
It's not clear whether you want only one instance of a duplicate line or not. Your code would produce 5 "duplicate" lines if there were 6 identical lines in the source file.
Here's a version which will report each duplicated line only once:
#echo off
setlocal enabledelayedexpansion
set var1=*
set var2=*
(
for /f %%a in ('sort q42574625.txt') do (
if "%%a"=="!var1!" IF "!var2!" neq "%%a" echo %%a&SET "var2=%%a"
set "var1=%%a"
)
)>"u:\q42574625.txt"
GOTO :EOF
Supposing you provide the text file as the first command line argument, you could try the following:
#echo off
for /F "usebackq delims=" %%L in ("%~1") do (
for /F "delims=" %%K in ('
findstr /X /C:"%%L" "%~1" ^| find /C /V ""
') do (
if %%K GTR 1 echo %%L
)
)
This returns all duplicate lines, but multiple times each, namely as often as each occurs in the file.

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
)
)

How can I read the last 2 lines of a file in batch script

I have a Java program that appends new builds information in the last two lines of a file.
How can I read them in batch file?
This code segment do the trick...
for /F "delims=" %%a in (someFile.txt) do (
set "lastButOne=!lastLine!"
set "lastLine=%%a"
)
echo %lastButOne%
echo %lastLine%
EDIT: Complete TAIL.BAT added
This method may be modified in order to get a larger number of lines, that may be specified by a parameter. The file below is tail.bat:
#echo off
setlocal EnableDelayedExpansion
rem Tail command in pure Batch: Tail.bat filename numOfLines
rem Antonio Perez Ayala
for /F "delims=" %%a in (%1) do (
set /A i=%2, j=%2-1
for /L %%j in (!j!,-1,1) do (
set "lastLine[!i!]=!lastLine[%%j]!
set /A i-=1
)
set "lastLine[1]=%%a"
)
for /L %%i in (%2,-1,1) do if defined lastLine[%%i] echo !lastLine[%%i]!
2ND EDIT: New version of TAIL.BAT added
The version below is more efficient:
#echo off
setlocal EnableDelayedExpansion
rem Tail command in pure Batch, version 2: Tail.bat filename numOfLines
rem Antonio Perez Ayala
set /A firstTail=1, lastTail=0
for /F "delims=" %%a in (%1) do (
set /A lastTail+=1, lines=lastTail-firstTail+1
set "lastLine[!lastTail!]=%%a"
if !lines! gtr %2 (
set "lastLine[!firstTail!]="
set /A firstTail+=1
)
)
for /L %%i in (%firstTail%,1,%lastTail%) do echo !lastLine[%%i]!
This will solve the problem, where someFile.txt is the file where you want to read the lines from:
for /f %%i in ('find /v /c "" ^< someFile.txt') do set /a lines=%%i
echo %lines%
set /a startLine=%lines% - 2
more /e +%startLine% someFile.txt > temp.txt
set vidx=0
for /F "tokens=*" %%A in (temp.txt) do (
SET /A vidx=!vidx! + 1
set localVar!vidx!=%%A
)
echo %localVar1%
echo %localVar2%
del temp.txt
::change the values bellow with a relevant ones.
set "file=C:\some.file"
set "last_lines=2"
for /f %%a in ('findstr /R /N "^" "%file%" ^| find /C ":"') do #set lines=%%a
set /a m=lines-last_line
more +%m% "%file%"
Directly from the command line:
C:\>set "file=C:\some.file"
C:\>set "last_lines=5"
C:\>(for /f %a in ('findstr /R /N "^" "%file%" ^| find /C ":"') do #set lines=%a)&#set /a m=lines-last_lines&call more +%m% "%file%"

Split a file using windows batch script

I have a csv file and i need to split it in to n files such that each split file should not exceed 100 mb. I need to achieve it in windows batch script. I tried the below way but its taking lot of time as my unsplit file is in GBs
#echo off
setlocal enableextensions enabledelayedexpansion
set count=1
set maxbytesize=100000000
set size=1
type NUL > output_1.csv
FOR /F "tokens=*" %%i in (myfile.csv) do (
FOR /F "usebackq" %%A in ('!filename!_!count!.csv') do (
set size=%%~zA)
if !size! LSS !maxbytesize! (
echo %%i>>!filename!_!count!.csv) else (
set /a count+=1
echo %%i>>!filename!_!count!.csv
))
please let me know if there is a better way to achieve this. I cant go to any other scripting languages as my server is windows
This would do the trick assuming your lines are roughly the same size.
Its advantage is that it is only a 2 pass solution, One for counting the lines and the other for printing them.
#rem echo off
#rem usage: batchsplit.bat <file-to-split> <size-limit>
#rem it will generate files named <file-to-split>.part_NNN
setlocal EnableDelayedExpansion
set FILE_TO_SPLIT=%1
set SIZE_LIMIT=%2
for /f %%s in ('dir /b %FILE_TO_SPLIT%') do set SIZE=%%~Zs
for /f %%c in ('type "%FILE_TO_SPLIT%"^|find "" /v /c') do set LINE_COUNT=%%c
set /a AVG_LINE_SIZE=%SIZE%/%LINE_COUNT%
set /a LINES_PER_PART=%SIZE_LIMIT%/%AVG_LINE_SIZE%
set "cmd=findstr /R /N "^^" %FILE_TO_SPLIT%"
for /f "tokens=1,2* delims=:" %%a in ('!cmd!') do #(
set /a ccc = %%a / %LINES_PER_PART%
echo %%b >> %FILE_TO_SPLIT%.part_!ccc!
)
save it as batchsplit.bat and run it using:
batchsplit.bat myfile.csv 100000000

Resources