extract and save single words from txt files Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
way2000
Posts: 58
Joined: 15 Jul 2016, 08:57

extract and save single words from txt files

01 Dec 2018, 07:21

how can i extract words from text files, there's around 500 text files. i need to extract all words (no duplicates) and save them as a word list in another text file

and also how can i make it so it reads external .txt files no matter what their name is through folder path address, it has to ignore symbols and numbers

i have this code so far, it only writes a "." in a text file, it doesn't seem to work

Code: Select all

Loop, 500

     {

          Path := "C:\test2" . A_Index . ".txt"

          FileRead, OutputVar, %Path%

          OutputVar = %OutputVar% . %OutputVar%

     }

FileAppend % OutputVar, list.txt
return
User avatar
Scr1pter
Posts: 1217
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: extract and save single words from txt files

01 Dec 2018, 08:07

These 500 text files contain each 1 word?
I'm my opinion you should include the FileAppend within the loop.
(At least this is how I would make it.)

It reads a file and writes the word in a new file.
Then it reads the next file and adds it to the output file.

Also, add a 'n after OutputVar, so that there will be a line-break after each word.
(On the smartphone I don't know if it is ' or ` or another one.

Your path looks odd to me as well.
I would try it with one of these:
Path := "C:\test2\%A_Index%.txt"
Or
Path := C:\test2\%A_Index%.txt
Or
Path = C:\test2\%A_Index%.txt
Or
Path = “C:\test2\%A_Index%.txt“

Can't test it, that's why I added several examples.

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: G502 FE, Jelly Comb Touchpad - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.32.00
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: extract and save single words from txt files

01 Dec 2018, 10:52

Code: Select all

Loop, Files, C:\*.txt
{	

	If IsAlpha(StrReplace(StrSplit(A_LoopFileName,".")[1],A_Space)){	;if file name has no numbers or symbols...
		FileRead, thisFile, % A_LoopFileFullPath
		wordlist .= wordlist ? "`n" GetStringWordList(thisFile) : GetStringWordList(thisFile)
	}
}

;MsgBox % finalWordList := GetStringWordList(wordList)	;sanitise wordlist for any duplicates during combining & remove any blank lines.
FileAppend, %finalWordList%, finalWordList.txt

GetStringWordList(text){	;returns a list with no duplicates
	StringReplace, text, text, `,, `n, ALL
	StringReplace, text, text, `r`n, `n, ALL
	StringReplace, text, text, %A_Space%, `n, ALL
	StringReplace, text, text, %A_Tab%, `n, ALL
	;remove white spaces
	wordListText := RegExReplace(text , "(^|\R)\K\s+")
	;rebuild wordlist using only strings longer than 3 characters
	Wordlist:=""
	Loop, Parse, wordListText, `n
		If StrLen(A_LoopField) >= 1		;strings of over 1 characters
			IfNotInString, Wordlist, %A_Space%%A_LoopField%%A_Space%	;avoid repeated strings
				Wordlist.= " " A_LoopField " " "`n"	;add spaces on either side so that words in other word can easily be added to list
	StringReplace, Wordlist, Wordlist, %A_Space%, , ALL ;spaces are no longer needed as list is done building
	Return WordList
}

IsAlpha(ByRef string){
	If string is alpha
		Return True
}
Last edited by CyL0N on 02 Dec 2018, 03:18, edited 2 times in total.
live ? long & prosper : regards
way2000
Posts: 58
Joined: 15 Jul 2016, 08:57

Re: extract and save single words from txt files

01 Dec 2018, 21:47

@CyL0N

how can i make it save to a text file the result instead of getting a msgbox
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: extract and save single words from txt files

02 Dec 2018, 03:20

way2000 wrote:
01 Dec 2018, 21:47
@CyL0N

how can i make it save to a text file the result instead of getting a msgbox
I've updated my post. The documentation & google are your best friends.
live ? long & prosper : regards
garry
Posts: 2547
Joined: 22 Dec 2013, 12:50

Re: extract and save single words from txt files

02 Dec 2018, 05:38

just a short DOS example to copy text-files in one file

Code: Select all

transform,s,chr,34
f99=%a_scriptdir%\F99.txt
Loop, Files, C:\*.txt,
  e .= s . A_LoopFileFullPath . s . "+"   ;- copy not works if string too long
stringtrimright,e,e,1  
runwait, %comspec% /k copy /A /N %e% "%f99%"  
ifexist,%f99%
run,%f99%
exitapp
User avatar
Maestr0
Posts: 126
Joined: 05 Dec 2013, 17:43

Re: extract and save single words from txt files  Topic is solved

02 Dec 2018, 09:06

Kinda like this?

Code: Select all

Loop, Files, *.txt
{	
	FileRead, thisFile, % A_LoopFileFullPath
	StringReplace, contents, thisfile, ., %A_Space%, All
	StringReplace, contents, contents, %A_Tab%, %A_Space%, All
	StringReplace, contents, contents, `,, %A_Space%, All
	StringReplace, contents, contents, `r`n, %A_Space%, All
	StringReplace, contents, contents, %A_Space%%A_Space%, %A_Space%, All
	loop, parse, contents, %A_Space%
	{
		if A_LoopField
			wordlist .= A_LoopField "`n"
	}
}

FileAppend, %WordList%, WordList.txt
way2000
Posts: 58
Joined: 15 Jul 2016, 08:57

Re: extract and save single words from txt files

02 Dec 2018, 09:35

Maestr0 wrote:
02 Dec 2018, 09:06
Kinda like this?

Code: Select all

Loop, Files, *.txt
{	
	FileRead, thisFile, % A_LoopFileFullPath
	StringReplace, contents, thisfile, ., %A_Space%, All
	StringReplace, contents, contents, %A_Tab%, %A_Space%, All
	StringReplace, contents, contents, `,, %A_Space%, All
	StringReplace, contents, contents, `r`n, %A_Space%, All
	StringReplace, contents, contents, %A_Space%%A_Space%, %A_Space%, All
	loop, parse, contents, %A_Space%
	{
		if A_LoopField
			wordlist .= A_LoopField "`n"
	}
}

FileAppend, %WordList%, WordList.txt
this works, thanks a lot
User avatar
Maestr0
Posts: 126
Joined: 05 Dec 2013, 17:43

Re: extract and save single words from txt files

03 Dec 2018, 13:24

way2000 wrote:
02 Dec 2018, 09:35
this works, thanks a lot
You're welcome!
Kindly mark this thread as solved, by marking the post that solved it.
Read how to do that here: https://autohotkey.com/boards/viewtopic.php?t=29007
- If someone answers your question, you can put a tick by it, this marks the question as 'solved', you can subsequently remove the tick, or put a tick on a different post.

Return to “Ask For Help”

Who is online

Users browsing this forum: famko, Muzzy and 40 guests