Merge Two text files line by line using batch script - file

I have 2 text files; A.txt and B.txt and I want to merge them to make C.txt using a batch script.
However (here's the tricky part) I wish to do it so each line from A.txt is appended with a space then the first line from B.txt then a new line with the first line from A and the second from B and so on until the end of B is reached and then we start with the second line of A.
I know I haven't worded this well so here's an example:
A.txt;
1
2
3
4
5
B.txt;
Z
Y
X
W
V
T
R
So C.txt would have;
1 Z
1 Y
1 X
1 W
1 V
1 T
1 R
2 Z
2 Y
etc.

#echo off
for /f "delims=" %%a in (a.txt) do (
for /f "delims=" %%b in (b.txt) do (
>>c.txt echo %%a %%b
)
)

Related

If query for number sections in Batch

is there a way to make a If query for specifig
number sections in Batch?
Something Like this:
IF "Var1"=="1-10" (
do something
)
the 1-10 should stand for 1,2,3,4,5,6,7,8,9,10.
I want to make 10 queries (1-10,11-20,21-30, ... , 91-100)
Is that possible?
You can do this:
#echo off
set Var1=1
for /l %%i in (0,1,10) do if %%i==%Var1% echo 10 or below
for /l %%i in (11,1,20) do if %%i==%Var1% echo 11 - 20
for /l %%i in (21,1,30) do if %%i==%Var1% echo 21 - 30
for /l %%i in (31,1,40) do if %%i==%Var1% echo 31 - 40
for /l %%i in (41,1,50) do if %%i==%Var1% echo 41 - 50
for /l %%i in (51,1,60) do if %%i==%Var1% echo 51 - 60
for /l %%i in (61,1,70) do if %%i==%Var1% echo 61 - 70
for /l %%i in (71,1,80) do if %%i==%Var1% echo 71 - 80
for /l %%i in (81,1,90) do if %%i==%Var1% echo 81 - 90
for /l %%i in (91,1,100) do if %%i==%Var1% echo 91 - 100
Here you can change set Var1=1 to any other number and it will correspond. You can replace echo N - N with your commands.
Also, set Var1=1 can be removed from the above example if used with your code as I simply set it to demonstrate the behaviour. Here is an extract from the help when running for /? so you understand the numeric behaviour.
FOR /L %variable IN (start,step,end) DO command [command-parameters]
The set is a sequence of numbers from start to end, by step amount.
So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
generate the sequence (5 4 3 2 1)

Batch - Find Out Permutation

I am new to Batch and I would like to know if I can find out all combinations of numbers in order.
In this case I have 49 Numbers from 1 - 49 , and I have to pick 6 Numbers to be the results.
For example:
1 2 3 4 5 6
1 2 3 4 5 7
...
1 2 3 4 5 49
1 2 3 4 6 7
1 2 3 4 6 8
etc...
This is my old code:
#echo off > NEWFILE & setLocal EnableDelayedExpansion
set a=44
set b=45
set c=46
set d=47
set e=48
set f=49
for /L %%a in (1 1 !a!) do (
for /L %%b in (2 1 !b!) do (
for /L %%c in (3 1 !c!) do (
for /L %%d in (4 1 !d!) do (
for /L %%e in (5 1 !e!) do (
for /L %%f in (6 1 !f!) do (
echo.%%a %%b %%c %%d %%e %%f
))))))) >> NEWFILE
goto :EOF
However it returns:
1 2 3 4 5 6
1 2 3 4 5 7
...
1 2 3 4 5 49
1 2 3 4 6 6
Two 6's appeared.
I don't seem to be able to fix it, please help, thanks very much!
When you post a question you should post your efforts to solve it, describe the method used and the problems you had; otherwise you may get similar answers with no explanations at all, like this one:
EDIT: As users dbenham and aschipfl indicated, my original code have a small bug: the set /A i=M-1 line should be placed after the :nextSet label. This is the right code:
#echo off
setlocal EnableDelayedExpansion
set "N=%1"
set "M=%2"
set "line="
for /L %%i in (1,1,%M%) do (
set "C[%%i]=%%i"
set "line=!line! ^!C[%%i]^!"
)
:nextSet
set /A i=M-1
for /L %%j in (!C[%M%]!,1,%N%) do (
set "C[%M%]=%%j"
echo %line%
)
:nextPos
set "C=!C[%i%]!"
if %C% equ %N% (
set /A i-=1
if !i! equ 0 goto :EOF
goto nextPos
)
for /L %%i in (%i%,1,%M%) do (
set /A C+=1,C[%%i]=C
)
if !C[%M%]! gtr %N% goto nextPos
goto nextSet
Obviously, the corrected code generate a much larger number of results and this version is particularly slow... :(
The new version below use the exact same code of dbenham's solution; its only advantage is that you may change the parameters used to generate the result in a very easy way:
#echo off
setlocal EnableDelayedExpansion
set "N=%1"
set "M=%2"
set /A j=N-M, prev=0
set "for=" & set "line=" & set "endfor="
for /L %%i in (1,1,%M%) do (
set /A j+=1
set "for=!for! set /A start=!prev!+1 & for /L %%%%i in (^!start^!,1,!j!) do ("
set "line=!line! %%%%i"
set "endfor=!endfor!)"
set "prev=%%%%i"
)
REM ECHO !FOR! echo !LINE! %ENDFOR%
%for% echo %line% %endfor%
Output example:
C:\> test.bat 6 4
1 2 3 4
1 2 3 5
1 2 3 6
1 2 4 5
1 2 4 6
1 2 5 6
1 3 4 5
1 3 4 6
1 3 5 6
1 4 5 6
2 3 4 5
2 3 4 6
2 3 5 6
2 4 5 6
3 4 5 6
To get your results, use: test.bat 49 6
2ND EDIT: Faster method added
When the problem to solve is the excessive time a process takes, an obvious alternative is to use a faster programming language. The solution below use JScript, that is somewhat similar to Batch file programming:
#if (#CodeSection == #Batch) #then
#echo off
echo Start: %time%
cscript //nologo //E:JScript "%~F0" > result.txt
echo End: %time%
goto :EOF
#end
// JScript code section
for ( var A=1; A <= 44; ++A ) {
for ( var B=A+1; B <= 45; ++B ) {
for ( var C=B+1; C <= 46; ++C ) {
for ( var D=C+1; D <= 47; ++D ) {
for ( var E=D+1; E <= 48; ++E ) {
for ( var F=E+1; F <= 49; ++F ) {
WScript.Echo(A,B,C,D,E,F);
}
}
}
}
}
}
This is a Batch-JScript hybrid script; save it with .BAT extension. This program took a little less than 9 minutes in my cheap-and-slow lap-top computer to generate a 239 MB file with 13983816 lines.
The problem is compute intensive, given that there are 13,983,816 unique permutations. (See https://en.wikipedia.org/wiki/Lottery_mathematics#Calculation_explained_in_choosing_6_from_49.)
The Rojo answer should work, but the GOTO and repetitive FOR /F parsing and IF logic will slow things down considerably.
The code is much faster if you use nested FOR /L loops.
#echo off
setlocal enableDelayedExpansion
for /l %%A in (1 1 44) do (
set /a start=%%A+1
for /l %%B in (!start! 1 45) do (
set /a start=%%B+1
for /l %%C in (!start! 1 46) do (
set /a start=%%C+1
for /l %%D in (!start! 1 47) do (
set /a start=%%D+1
for /l %%E in (!start! 1 48) do (
set /a start=%%E+1
for /l %%F in (!start! 1 49) do (
echo %%A %%B %%C %%D %%E %%F
)
)
)
)
)
)
This will still be unbearably slow to let this script print the results to the screen. I estimate it will take 1.25 hours on my machine. Redirecting the output to a file is about 5 times faster, around 15 minutes.
In the future, please show some code demonstrating that you've attempted to solve the problem yourself, showing where you got stuck, where the output is not as expected, etc. Questions resembling "Here are my requirements. Code this for me" generally aren't well-received around here. How you got an upvote without showing any code is beyond me, but c'est la vie.
In this instance, I found the problem interesting, so I thought I'd go ahead and get you started. Challenge: accepted. Here's one way to do it.
#echo off
setlocal enabledelayedexpansion
set "series=1 2 3 4 5 6"
:loop
echo %series%
if "%series%"=="44 45 46 47 48 49" goto :EOF
for /f "tokens=1-6" %%a in ("%series%") do (
set /a i1=%%a, i2=%%b, i3=%%c, i4=%%d, i5=%%e, i6=%%f+1
if !i6! gtr 49 set /a i5+=1, i6=i5+1
if !i5! gtr 48 set /a i4+=1, i5=i4+1, i6=i5+1
if !i4! gtr 47 set /a i3+=1, i4=i3+1, i5=i4+1, i6=i5+1
if !i3! gtr 46 set /a i2+=1, i3=i2+1, i4=i3+1, i5=i4+1, i6=i5+1
if !i2! gtr 45 set /a i1+=1, i2=i1+1, i3=i2+1, i4=i3+1, i5=i4+1, i6=i5+1
set "series=!i1! !i2! !i3! !i4! !i5! !i6!"
)
goto loop
Here's another solution that should be more efficient.
#echo off
setlocal enabledelayedexpansion
set "series=1 2 3 4 5 6"
set total=0
for /L %%a in (1,1,44) do (
set /a i2 = %%a + 1
for /L %%b in (!i2!, 1, 45) do (
set /a i3 = %%b + 1
for /L %%c in (!i3!, 1, 46) do (
set /a i4 = %%c + 1
for /L %%d in (!i4!, 1, 47) do (
set /a i5 = %%d + 1
for /L %%e in (!i5!, 1, 48) do (
set /a i6 = %%e + 1
for /L %%f in (!i6!, 1, 49) do (
rem // Uncomment this echo to watch the progress (severely decreases efficiency)
rem echo %%a %%b %%c %%d %%e %%f
set /a total += 1
)
)
)
)
)
echo Total so far: !total!
)
rem // Should have gone through 13983816 iterations

Adding strings at certain places in a text file

I have a text file with the following contents:
-849.4471 1272.173 22.8698 0 0 -1 7.54979E-008 Fire_Esc_6 385 792 24 -1
-837.0507 1270.862 28.1249 0 0 -1 7.54979E-008 Fire_Esc_6b 385 792 24 -1
-837.0654 1270.879 24.09248 0 0 -1 7.54979E-008 Fire_Esc_6 385 792 24 -1
For each of the lines, I need to
add setAttr "sth"; to the beginning of the line
add sth between the first and second numbers
delete everything on the line from 385 through the end of the line
I'm a total beginner in batch and have no idea where to start. Any help you can give you be greatly appreciated.
I've done until here by somethingDark's help :0
FOR /F "tokens=8* delims= " %%G IN (C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt) DO ECHO set %%G >12.txt
Since you've made an attempt to solve this yourself, I feel better about showing you what I was imagining for your script.
#echo off
:: If 12.txt exists, delete it. This way, the entire file will be recreated when the script is re-run.
:: (If you don't want this to happen and you just want new data added to the end of the file every
:: time the script is run, just delete this part.)
if exist 12.txt del 12.txt
:: An example line looks like this:
:: -849.4471 1272.173 22.8698 0 0 -1 7.54979E-008 Fire_Esc_6 385 792 24 -1
:: Iterate through each line in 1.txt, storing each space-delimited string in a unique variable
:: %%A: -849.4471
:: %%B: 1272.173
:: %%C: 22.8698
:: %%D: 0
:: %%E: 0
:: %%F: -1
:: %%G: 7.54979E-008
:: %%H: Fire_Esc_6
:: Since we don't care about anything after the eighth token, we can just ignore it
:: The redirection command is at the start of the line to avoid an extra space at the end of the line
for /f "tokens=1-8" %%A in (C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt) do >>12.txt echo setAttr "sth"; %%A sth %%B %%C %%D %%E %%F %%G %%H
Since this script is so short (it's really just three lines with a whole bunch of comments), you could even run this one-liner from the command line:
for /f "tokens=1-8" %A in (C:\Users\Sherlock\Documents\3DReaperDX\Frames\1.txt) do >>12.txt echo setAttr "sth"; %A sth %B %C %D %E %F %G %H
This will create the file 12.txt with the contents
setAttr "sth"; -849.4471 sth 1272.173 22.8698 0 0 -1 7.54979E-008 Fire_Esc_6
setAttr "sth"; -837.0507 sth 1270.862 28.1249 0 0 -1 7.54979E-008 Fire_Esc_6b
setAttr "sth"; -837.0654 sth 1270.879 24.09248 0 0 -1 7.54979E-008 Fire_Esc_6

Using the batch command %random% to get to a random spot on the file

Is it possible to use the %random% command to go to a random spot on the batch file? For example:
set /p input=%random%
if %input%==[whatever the random number generated... (example 1004] goto a
if %input%==[a different number generated by %random% (example 203)] goto b
Yes, and there is no need to translate a number into a label. Simply use the random number as the label. Here is a trivial demonstration that randomly selects 1 of 5 GOTO labels:
#echo off
setlocal
set /a label=%random% %% 5
goto %label%
:0
echo random 0 abc
exit /b
:1
echo random 1 def
exit /b
:2
echo random 2 ghi
exit /b
:3
echo random 3 jkl
exit /b
:4
echo random 4 mno
exit /b
You could use the same technique to CALL a random label instead. Here is a demonstration that makes 10 random CALLs.
#echo off
setlocal enableDelayedExpansion
for /l %%N in (1 1 10) do (
set /a label=!random! %% 5
call :!label!
)
exit /b
:0
echo random 0 abc
exit /b
:1
echo random 1 def
exit /b
:2
echo random 2 ghi
exit /b
:3
echo random 3 jkl
exit /b
:4
echo random 4 mno
exit /b
--Sample output--
random 2 ghi
random 4 mno
random 3 jkl
random 2 ghi
random 2 ghi
random 3 jkl
random 1 def
random 4 mno
random 3 jkl
random 1 def
Perhaps your script has differerent contexts, and within each context you have random outcomes. Your labels can consist of a context followed by a number.
#echo off
setlocal enableDelayedExpansion
for /l %%N in (1 1 10) do (
set /a A=!random!%%5, B=!random!%%3
call :A!A!
call :B!B!
echo(
)
exit /b
:A0
echo context A random 0 abc
exit /b
:A1
echo context A random 1 def
exit /b
:A2
echo context A random 2 ghi
exit /b
:A3
echo context A random 3 jkl
exit /b
:A4
echo context A random 4 mno
exit /b
:B0
echo context B random 0 xyz
exit /b
:B1
echo context B random 1 123
exit /b
:B2
echo context B random 2 apple orange banana
exit /b
-- Sample output--
context A random 3 jkl
context B random 0 xyz
context A random 1 def
context B random 1 123
context A random 4 mno
context B random 2 apple orange banana
context A random 2 ghi
context B random 1 123
context A random 1 def
context B random 2 apple orange banana
context A random 3 jkl
context B random 2 apple orange banana
context A random 0 abc
context B random 1 123
context A random 2 ghi
context B random 1 123
context A random 2 ghi
context B random 1 123
context A random 4 mno
context B random 1 123
Yes it is possible, depending on what your view of a random spot on the batch file means. The code you provided almost works, here's an updated version:
set NUMBER=%random%
if %NUMBER%==1004 goto A
if %NUMBER%==203 goto B
:A
REM Do something
GOTO :EOF
:B
REM Do something else
You cannot jump to a random spot inside your batch file, but based on a random value, you can go to pre-defined labels. You can even define some rules where you jump to a label Z if the random value is between X and Y.
set /a number=%random% %% 5
goto destination%number%
would goto one of destination0, destination1, destination2, destination3, destination4 if that's your intent.

Creating a word list using batch

I want to make a batch file that prints out to a text file a list of combination of numbers and letters.
Using {0, ..., 9, a, ..., z, A, ..., Z} as my character pool, I have 62 unique characters.
The word length starts as 1 and increases up to a predetermined value.
The script starts at length = 1 and prints out 0 to Z.
Then it proceeds to length = 2 and prints out 00 to ZZ, and so on...
Here is an iterative solution that is much faster.
No need for CALL.
Each permutation is only generated once.
I was able to generate up to length 4 with over 15 million permutations in less than 5 minutes.
#echo off
setlocal enableDelayedExpansion
set chars=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
set maxPos=61
del output.txt 2>nul
>prior.txt echo(""
for /l %%I in (1 1 %1) do (
>new.txt (
for /f %%A in (prior.txt) do for /l %%N in (0 1 %maxPos%) do echo(%%~A!chars:~%%N,1!
)
type new.txt>>output.txt
move /y new.txt prior.txt >nul
)
del prior.txt
Perhaps this is what you want?
TEST.BAT
#echo off
set charPool=_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
set charLen=62
(for /L %%a in (1,1,%1) do (
set permutation=
call :makePermutation %%a
)) > textfile.txt
goto :EOF
:makePermutation level
setlocal EnableDelayedExpansion
set lastPermutation=%permutation%
for /L %%i in (1,1,%charLen%) do (
set permutation=!lastPermutation!!charPool:~%%i,1!
if %1 gtr 1 (
set /A newLevel=%1-1
call :makePermutation !newLevel!
) else (
echo(!permutation!
)
)
exit /B
The batch file must be started with a number as parameter which is the unit length.
For example on using TEST.BAT 1 the text file textfile.txt contains 62 lines.
Note that TEST.BAT 2 generates 3906 combinations (strictly speaking, permutations in statistical sense) from 0 to ZZ, and TEST.BAT 3 generates 242234 combinations from 0 to ZZZ!
Calculation example for estimating the number of strings in text file (size of file):
Running TEST.BAT with 5 as parameter produces
62 ^ 5 + 62 ^ 4 + 62 ^ 3 + 62 ^ 2 + 62 ^ 1 = 931.151.402
strings in text file.

Resources