## How to combine text of two seperate files in a new file

Get help with using AutoHotkey and its commands and hotkeys
taghi_ar
Posts: 20
Joined: 26 Dec 2016, 10:28

### How to combine text of two seperate files in a new file

Dear Friends i am very new to this forum (beginner)
i have two seperate text files text1.txt and text2.txt .
text1.txt contains a list of urls without path and file names (ie. http://11.22.33.44/)
and text2.txt contains some online path to files ( ie. /path1/path2/filename.exe)
i want to combine these two files through loop command
so i can put one by one urls and path+file names to gether so i can make my download list.
any help would be appreciated
garry
Posts: 2459
Joined: 22 Dec 2013, 12:50

### Re: How to combine text of two seperate files in a new file

example , select 2 text files and writes content in NewText.txt
( File1-Line1=File2-Line1 ... )

Code: Select all

;- select 2 text-files
FileSelectFile, Path1, 3,, Choose first text file:,Supported Files (*.txt;*.log)
If Path1 =
ExitApp
FileSelectFile, Path2, 3,, Choose second text file:,Supported Files (*.txt;*.log)
If Path2 =
ExitApp
;---------------------
Loop
{
a=
b=
If ErrorLevel != 0
break
if (a!="" and b!="")                        ;- if A is not empty AND B is not empty
e .= a . "=" . b . "rn"                 ;- e collects A = B  ( = is separator  rn is EndOfLine )
}
if e<>                                         ;- if e is not empty
{
f2=%a_scriptdir%\NewText.txt
ifexist,%f2%
filedelete,%f2%
fileappend,%e%,%f2%                          ;- write to new file
e=
run,%f2%
}
exitapp


Last edited by garry on 26 Dec 2016, 14:41, edited 2 times in total.
taghi_ar
Posts: 20
Joined: 26 Dec 2016, 10:28

### Re: How to combine text of two seperate files in a new file

Dear Garry thanks fo the anwer you did the job bro!
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: How to combine text of two seperate files in a new file

Classic programming problem!
Hopefully my script is pretty self-explanatory.

Code: Select all

vPath1 = %A_Desktop%\z my file 1.txt
vPath2 = %A_Desktop%\z my file 2.txt
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText1)*2+StrLen(vText2)*2)

vIsV1 := !!SubStr(1,0)
if (SubStr(vText1, vIsV1-2) = "rn")
vText1 := SubStr(vText1, 1, -2)
if (SubStr(vText2, vIsV1-2) = "rn")
vText2 := SubStr(vText2, 1, -2)

vText1 := StrReplace(vText1, "rn", "n")
vText2 := StrReplace(vText2, "rn", "n")
oArray1 := StrSplit(vText1, "n")
oArray2 := StrSplit(vText2, "n")
vMax1 := oArray1.MaxIndex()
vMax2 := oArray2.MaxIndex()

if !(vMax1 = vMax2)
MsgBox % "warning: mismatch in number of items:rn" vMax1 "rn" vMax2

vDelim := "="
vMax := vMax1 > vMax2 ? vMax1 : vMax2
Loop, % vMax
vOutput .= oArray1[A_Index] vDelim oArray2[A_Index] "rn"

vPath = %A_Desktop%\z my files combined %A_Now%.txt
FileAppend, % vOutput, % "*" vPath, UTF-8
;FileAppend, % vOutput, % "*" vPath ;for ANSI
MsgBox, % "done"
return

Last edited by jeeswg on 11 Jun 2017, 19:14, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
taghi_ar
Posts: 20
Joined: 26 Dec 2016, 10:28

### Re: How to combine text of two seperate files in a new file

Dear Jeeswg thanks man i tested your code and it works like a charm.
i use the following code to extract file path (my second text file i mentioned above)
there is an extra word attached to the first of file path when the script does the job(ie. file=/path1/path2/file.exe ) here i want my script to delete " file=/"
i should say that my desired strings are started with "file=/" but i want it to be omited in my destination text file.

here is the code

{
IfInString, A_LoopReadLine, file= , FileAppend, %A_LoopReadLine%n
}

text.txt is my source file for extracting online path
garry
Posts: 2459
Joined: 22 Dec 2013, 12:50

### Re: How to combine text of two seperate files in a new file

remove file=/

Code: Select all

F1=%a_scriptdir%\text.txt
x=file=/

stringreplace,b,a,%x%,,all
msgbox,%b%
return

taghi_ar
Posts: 20
Joined: 26 Dec 2016, 10:28

### Re: How to combine text of two seperate files in a new file

Dear Garry You are amazing thank you
garry
Posts: 2459
Joined: 22 Dec 2013, 12:50

### Re: How to combine text of two seperate files in a new file

@jeeswg , you have an optimal script ( varsetcapacity / array etc ... ) , I just made some small modification to my basic script above ( added also comments )
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: How to combine text of two seperate files in a new file

@taghi_ar, cheers, I haven't really shared much code before the last few days, nice to get some good feedback!

@garry, yeah I know over time I've learnt some useful tidbits, but if someone can catch me out on anything that can be further improved I would like that!

FileReadLine was always said to be deprecated
StrSplit variable next best (which creates a tonne of variables)
and StrSplit array even better I believe (anyhow it only uses 1 object and not loads of vars)
(I did write a script that repeatedly used InStr to find the next line in each var)

VarSetCapacity, gives the var room for all the text before you begin,
or it has to keep shuffling the data about to a new location, as the var gets full
best way to truncate string - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=26016

FileAppend with * stops some CR/LF checking,
manual:
'End of line (EOL) translation: To disable EOL translation, prepend an asterisk to the filename.
...
However, specifying the asterisk when Text contains rn improves performance because the program does not need to scan Text for rn.'
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
garry
Posts: 2459
Joined: 22 Dec 2013, 12:50

### Re: How to combine text of two seperate files in a new file

@jeeswg , thank you , I'll have to learn from your script and also better read the autohotkey help
taghi_ar
Posts: 20
Joined: 26 Dec 2016, 10:28

### Re: How to combine text of two seperate files in a new file

i got a question
in a text file how can i delete two phrases embraced by three symbols( ie. /Phrase/phrase/ ) slash / is the symbol
in all over the text
i found a solution in notepad++ but i want to do it by ahk script
any help would be appreciated
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: How to combine text of two seperate files in a new file

If there is only one instance of the '/text/text/' issue per line, I might use a parse loop, InStr and SubStr.
There may be a RegExReplace technique, but I only use RegEx rarely.
It might be of interest to explain how you do it in Notepad++,
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
taghi_ar
Posts: 20
Joined: 26 Dec 2016, 10:28

### Re: How to combine text of two seperate files in a new file

Dear Jeeswg inorder to replace '/text/text/' with blank space or simply search and remove it i use Ctrl+h and put "\/.*?/.*?/" string without Quotes"" in find what field .
it does the job fast
could you please tell me how to do it in ahk? i am very weak in loops
taghi_ar
Posts: 20
Joined: 26 Dec 2016, 10:28

### Re: How to combine text of two seperate files in a new file

by the way my text file is filled with that pattern which i want to remove and in each line there is one instance and words between slashes are not the same .
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: How to combine text of two seperate files in a new file

Start with this: see how you go, and ask if you have problems. I'm going to be AFK.
By the way, RegExReplace might be able to do this in one line, if you check that in the manual.

Code: Select all

vPath = ;put path here
FileRead, vText, % vPath ;get the text
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2) ;prepare a variable with lots of room

vText := StrReplace(vText, "rn", "n")
Loop, Parse, vText, n
{
vOutput .= ;use InStr and SubStr here, use A_LoopField
}
Clipboard := vOutput
MsgBox, % "done"
return

Last edited by jeeswg on 11 Jun 2017, 19:16, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
taghi_ar
Posts: 20
Joined: 26 Dec 2016, 10:28

### Re: How to combine text of two seperate files in a new file

thanks bro
i am going to check it
taghi_ar
Posts: 20
Joined: 26 Dec 2016, 10:28

### Re: How to combine text of two seperate files in a new file

vOutput .= InStr(A_LoopField, /*/*/ [, CaseSensitive = false, StartingPos = 5, Occurrence = 1]) ;use InStr and SubStr here, use A_LoopField

i changed the line no 7 like this and nothing happens i dont know how should i define the pattern of /text/text/
sorry but i am very new to this
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: How to combine text of two seperate files in a new file

This should get you most of the way there:

Code: Select all

;InStr is not clever like RegEx or anything, it doesn't handle wildcards

;done simply
vPos1 := InStr(A_LoopField, "/") ;1st occurrence
vPos2 := InStr(A_LoopField, "/", 0, 1, 3) ;3rd occurrence

;done simply (slightly differently)
vPos1 := InStr(A_LoopField, "/", 0, 1, 1) ;1st occurrence
vPos2 := InStr(A_LoopField, "/", 0, 1, 3) ;3rd occurrence

;done cleverly
vPos1 := InStr(A_LoopField, "/") ;1st occurrence
vPos2 := InStr(A_LoopField, "/", 0, vPos1+1, 2) ;2nd occurrence after vPos1

;to finish get the text before vPos1 and the text after vPos2
vOutput .= SubStr(A_LoopField, 1, vPos1-1) . SubStr(A_LoopField, vPos2+1) . "rn"
`
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
taghi_ar
Posts: 20
Joined: 26 Dec 2016, 10:28

### Re: How to combine text of two seperate files in a new file

Dear Jeeswg
i was stucked in this and i searched again in google and found a relevant thread in following address
https://autohotkey.com/board/topic/3959 ... -patterns/

i tried the code provided there and with a few tries i found it operational
here is the code and i will try your code as well

FileAppend,% RegExReplace(var,"/.*/.*/"), update2.txt

jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

### Re: How to combine text of two seperate files in a new file

Could you explain how you do it Notepad++, do you type in some short code.
Cheers, thanks. Good luck with the script.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA