Loop all files and remove bracketed text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nathan323
Posts: 44
Joined: 31 Jan 2016, 02:53
Location: Australia

Loop all files and remove bracketed text

05 Sep 2018, 21:39

Hi all,

I've been searching and playing around for a while and I am a bit stuck. What I'm trying to do is relatively simple I think. I have the following:

Code: Select all

FileRead, var, Ozark (S01E01) - Sugarwood.srt
new_var := RegExReplace(var, "\[(.*?)\]")
FileAppend, %new_var%, %A_WorkingDir%\Ozark (S01E01) - Sugarwood.txt
This works fine. But I want to batch it across all episode subtitle files in the folder, so I figure a loop. Something like this?

Code: Select all

Loop, Files, *.srt
{
	SplitPath, A_LoopFileName,,,,finalname
	FileRead, var, "%A_LoopFileName%"
	new_var := RegExReplace(var, "\[(.*?)\]")
	FileAppend, %new_var%, %A_WorkingDir%\%finalname%.txt
}
This results in the output file being blank. I have the flu and I'd like to presume that my brain isn't working very well (if this is something ridiculously easy I'm missing :)

Cheers!
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Re: Loop all files and remove bracketed text

05 Sep 2018, 22:51

Try this:

Code: Select all

FileList:=""
Loop, Files, *.srt
	FileList = %FileList%%A_LoopFileName%`n
Loop, parse, FileList, `n
{
	FileRead, var, % A_LoopField
	new_var := RegExReplace(var, "\[(.*?)\]")
	FileAppend, %new_var%, %A_WorkingDir%\%A_LoopField%.txt
}
nathan323
Posts: 44
Joined: 31 Jan 2016, 02:53
Location: Australia

Re: Loop all files and remove bracketed text

05 Sep 2018, 23:13

Yes that works, thankyou! It looks like I need to do some reading about loop parse, I haven't come across that before.
Two little problems I have, it also creates an extra blank text file with no name, just ".txt"
Also, the resulting files are called (for eg.) "Ozark (S01E01) - Sugarwood.srt.txt" - I was hoping that I could just pass the name without extension to the final output, hence my using splitpath in my initial attempt. But, when I try to integrate it into your script, I get the same blank files..
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Re: Loop all files and remove bracketed text

05 Sep 2018, 23:15

Your code works too, with a little bug fix:

Code: Select all

Loop, Files, *.srt
{
	SplitPath, A_LoopFileName,,,,finalname
	FileRead, var, % A_LoopFileName
	new_var := RegExReplace(var, "\[(.*?)\]")
	FileAppend, %new_var%, %A_WorkingDir%\%finalname%.txt
}
nathan323
Posts: 44
Joined: 31 Jan 2016, 02:53
Location: Australia

Re: Loop all files and remove bracketed text

05 Sep 2018, 23:48

Ahh, I was so close! No idea why I had inverted commas around a variable :crazy: Thanks for your help mate :)
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Re: Loop all files and remove bracketed text

06 Sep 2018, 00:04

Commas (quotations) are legal for the sake of white spaces in file name, I believe. But it turns out, that for some characters e.g. ampersand (probably brackets too), don't work.
Edit: Legal for passing a variable/parameter to CMD!!!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 246 guests