Copy one line from one file, Completely replace Text on another specific line in another file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wizshaw
Posts: 14
Joined: 21 Oct 2021, 20:57

Copy one line from one file, Completely replace Text on another specific line in another file

Post by wizshaw » 21 Oct 2021, 21:10

I think this might be a bit more complicated then this appears.. because of the replacement of a line.. I was messing with other code.. but when the file resaved... it completely deleted the line, and consolidated the file. When I need the line to be empty.. after being deleted.

Basically its

1. Read file
2. Copy a specific Line in file. Which pastes to clipboard (Line 3 for instance. I might have to run the process twice to get my result I need, which is fine)
3. Paste that line to another file. (However there is already text here)

So would I have to delete the text and leave the line blank? First? Or would it just replace everything in that line on the second file?

File 1

Birds
Yoyo blanket keyboard <--- Copy this, line 2
Water

File 2

Computer
Screen harddrive hub <--- Copy and replace this with "Yoyo blanket keyboard"
Speakers

Etc

Any help would be greatly appreciated.

Stuff ive been playing with.. This deletes the line.. but consolidates the file.

Loop, Read, D:\Arcade\Emulators\epsxe\plugins\crypt_killer\nuvee_psx_guncon1.ini, D:\Arcade\Emulators\epsxe\plugins\crypt_killer\nuvee_psx_guncon1_.ini
If A_Index=10
Continue
else
FileAppend %A_LoopReadLine%`n
FileMove, D:\Arcade\Emulators\epsxe\plugins\crypt_killer\nuvee_psx_guncon1_.ini, D:\Arcade\Emulators\epsxe\plugins\crypt_killer\nuvee_psx_guncon1.ini, 1
return

wizshaw
Posts: 14
Joined: 21 Oct 2021, 20:57

Re: Copy one line from one file, Completely replace Text on another specific line in another file

Post by wizshaw » 21 Oct 2021, 22:59

I see that there is a function called.

TF_InsertLine

Which was referenced years ago... I cant find any information on it though. So I guess maybe it was old function that isnt used anymore?

Also I think I should start it off like this..

Code: Select all

Loop
{
    FileReadLine, 3, D:\Arcade\Tools\Demul Shooter\config.ini, %A_Index%
        return
}
return
Then after. I would like to paste %A_Index% into another text file. Maybe use the Fileappend function? However that file, at the specific line already has a text in it. Would I have to run something first to delete everything in that line leaving it empty? And then paste in the variable content above?

Any help would be appreciated. I can even send somebody a bit of paypal for the help.

Thank you

wizshaw
Posts: 14
Joined: 21 Oct 2021, 20:57

Re: Copy one line from one file, Completely replace Text on another specific line in another file

Post by wizshaw » 21 Oct 2021, 23:54

I think I can actually do this in parts.

Ill first delete the line I originally wanted replaced in the second file. Resave over the previous file.

Ill use the replace ​StringReplace to create the line that I want in the first file. Then save the file to a new file...

Then copy that entire line to ether a variable or the clipboard from the first file

Then use fileappend to copy it to the end of the second file.

(I believe since the file is an ini file.. the one that needs to be read... It doesnt matter where the line of text is located. (I could be wrong on that... but a simple test will confirm it.)

Im going to try and write the scripting for it now.

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Copy one line from one file, Completely replace Text on another specific line in another file

Post by flyingDman » 22 Oct 2021, 00:44

try:

Code: Select all

sourceline := 2
targetline := 3

File1 = 									; fileread, file1, %path2file1%
(
Birds
Yoyo blanket keyboard
Water
)

File2 = 									; fileread, file2, %path2file2%
(
Computer
Screen harddrive hub
Speakers
)

tmp := strsplit(file1,"`n","`r")[sourceline]

for x,y in strsplit(file2,"`n","`r")
	nFile2 .= (a_index = targetline ? tmp : y) "`n"

msgbox % nFile2								; filedelete, %path2file2% ; fileappend, %file2%, %path2file2%
14.3 & 1.3.7

wizshaw
Posts: 14
Joined: 21 Oct 2021, 20:57

Re: Copy one line from one file, Completely replace Text on another specific line in another file

Post by wizshaw » 22 Oct 2021, 01:30

Code: Select all

sourceline := 3
targetline := 10

File1 = 									
(
Fileread, config.ini, D:\Arcade\Tools\Demul Shooter ; fileread, file1, %path2file1%
)

File2 = 									
(
Fileread, nuvee_psx_guncon1.ini, D:\Arcade\Emulators\epsxe\plugins\crypt_killer ; fileread, file2, %path2file2%
)

tmp := strsplit(file1,"`n","`r")[sourceline]

for x,y in strsplit(file2,"`n","`r")
	nFile2 .= (a_index = targetline ? tmp : y) "`n"

msgbox % nFile2								; filedelete, %path2file2% ; fileappend, %file2%, %path2file1%

FileDelete, %path2file2%
FileAppend, %file2%, D:\Arcade\Emulators\epsxe\plugins\crypt_killer
[Mod edit: [code][/code] tags added.]

Thank you for taking the time to respond.

I think this isnt quite correct.. as it isnt working. But its close. Please take a look when you have a min.

Chris

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Copy one line from one file, Completely replace Text on another specific line in another file

Post by flyingDman » 22 Oct 2021, 08:52

My code is working alright (did you run the example?). The file1 = and file2 are simulating the result of fileread. See here: https://www.autohotkey.com/docs/commands/FileRead.htm
14.3 & 1.3.7

wizshaw
Posts: 14
Joined: 21 Oct 2021, 20:57

Re: Copy one line from one file, Completely replace Text on another specific line in another file

Post by wizshaw » 22 Oct 2021, 12:15

I mean what I posted isnt quite right as im unfamiliar with the format of the example. I think I got the last part wrong. I was looking for help wiht the corrections.

Ill continue to play with it.

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Copy one line from one file, Completely replace Text on another specific line in another file

Post by flyingDman » 22 Oct 2021, 14:12

See if these explanations help: (please, practice with dummy files file1.ini and file2.ini in the script's folder)

Code: Select all

sourceline := 2
targetline := 3
fileread, file1, file1.ini									;reads the content of file1.ini into the variable file1
fileread, file2, file2.ini									;reads the content of file2.ini into the variable file2	
tmp := strsplit(file1,"`n","`r")[sourceline]				;reads the second line of file1 into variable tmp
for x,y in strsplit(file2,"`n","`r")						;loops through each line of file2 
	nFile2 .= (x= targetline ? tmp : y) "`n"			;reconstructs file2 but when targetline is reached replaces the existing line with tmp
filedelete, file2.ini										;delete the old file2.ini 
fileappend, %nfile2%, file2.ini								;creates a new file2.ini with the new content
14.3 & 1.3.7

ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: Copy one line from one file, Completely replace Text on another specific line in another file

Post by ahk7 » 23 Oct 2021, 03:07

wizshaw wrote:
21 Oct 2021, 22:59
I see that there is a function called.

TF_InsertLine

I cant find any information on it though.
I'm guessing you didn't Google for it, within the first 5 results, but you can find it here:
https://github.com/hi5/TF
I haven't looked at your questions/script but when dealing with INI in general you might be better off using AutoHotkey's INI related commands to read, write and delete entries.

wizshaw
Posts: 14
Joined: 21 Oct 2021, 20:57

Re: Copy one line from one file, Completely replace Text on another specific line in another file

Post by wizshaw » 25 Oct 2021, 08:07

Ill give this a shot today, been busy all weekend. And worked all night... off to sleep

Again thank you for the help

Post Reply

Return to “Ask for Help (v1)”