How to remove string (saved to var) with RegEx or something else

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

How to remove string (saved to var) with RegEx or something else

02 Apr 2022, 13:55

What I'm trying to do all told is this: I have video files that aren't in folders. I want to highlight the file(s) and split the name if they are a show, otherwise use file name as is, to use for creating a folder for it then move the file(s). I have that part working fine for files that aren't TV shows.

Below is a piece that I'm trying to incorporate into that piece. What I want the below to do is look at the clipboard or variable text that looks like clipboard in code below. If it contains S01 or S02 up to say S20 i.e., it should be able to create a folder with just the name part previous to the season number. If it has a season number, i.e. is a tv show file, create a folder for the show itself and then create a subdirectory with the name of the season (S1, S2 etc, no zero) (unless either or both the show name folder and/or the season name folder already exist, in which case it just goes on to the moving part) and move the whole file(s) to the directory. The moving part shouldn't be a problem. I can probably finagle it into my non-TV show hotkey.

I've found below to the be closest of the 10 or so options I found (written by Mikeyww I believe, thank you). It does remove the name part. I don't know where that part goes or if this is the best way to deal with this. If you are needing or interested in the whole thing (it's long), I can attach later.

Here is a needle/haystack piece of another way I tried just to see my thought process

haystack := "TV Show Name S01E01 Episode Name"
Needle := S01,S02,S03,S04,S05,S06,S07,S08,S09,S10,S11,S12,S13,S14,S15,S16,S17,S18,S19,S20,S21,S22,S23,S24

Clear as mud, I'm sorry. :crazy: Thanks for any help in advance.

Code: Select all

clipboard := "TV Show Name S01E01 Episode Name"
clipboard := removeBeforeword(str, "S01")
clipboard := removeAfterword2(str2, "S01") ;  was removeBeforeWord

;  this before part works most, other than I don't know how to save the removed part to a variable to use later
removeBeforeWord(str, word) {
 RegExMatch(str, "\b\Q" word "\E\b.*", m)
; MsgBox, 4164, Result, 2 Removed BEFORE word with str variable = %Clipboard%
 MsgBox, 4164, Result, 2 with m Removed BEFORE word with str variable = %Clipboard%
 Return m ;  This works with this M result, but not the msgbox version
  }
  
 ;  this doesn't work, I looked at RegEx page and don't see how what is here affects even where it decides to cut forward or back so couldn't figure out what to change.
 
removeAfterWord2(str2, word) {
; RegExMatch(str, "\b\Q" word "\E\b.*", m)
 RegExMatch(str2, "\b\Q" word "\E\b.*", m) 
 MsgBox, 4164, Result, 4 Removed AFTER word with str2 variable = %Clipboard%
 Return m
 }

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: How to remove string (saved to var) with RegEx or something else

02 Apr 2022, 14:29

Code: Select all

; This script moves files into directories according to the file name
dir      := A_ScriptDir
haystack := "TV Show Name S01E01 Episode Name"
; haystack := "TV Show Name"
If !FileExist(file := dir "\" haystack ".mp4") {
 MsgBox, 48, Error, File not found. Aborting.`n`n%file%
 Return
} Else If !RegExMatch(haystack, "(.+)\h+S(\d\d)", m) ; If not a season,
 m1 := haystack                                      ;  use the whole filename
If !FileExist(subdir := dir "\" m1 (m2 ? "\S" m2 + 0 : "")) {
 FileCreateDir, %subdir%
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred while creating the directory. Aborting.`n`n%subdir%
  Return
 }
}
FileMove, %file%, %subdir%
If ErrorLevel
     MsgBox, 48, Failure, An error occurred while moving the file.`n`n%file%
Else MsgBox, 64, Success, Done!
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: How to remove string (saved to var) with RegEx or something else

02 Apr 2022, 15:07

Thank you Mikeyww. To give you a frame of reference, this is the code I already have that works. I made it so that I can click on a file that is in the base folder I want, split the file I'm on's path and creates the new file there. That way I don't have to go open explorer, etc. That's what the below does.

What my original question was how to extract that the file on the clipboard contains a season number, S01, S02 etc. I wanted to use only the show's name previous to where the S01 etc starts to create the dir and then depending on what season it is, make a subdirectory of S1, S2, S3 or whatever it happens to be. This bit would be incorporated into what's below here. What's below works fine for files that don't need anything extracted from them. I hope that makes it clearer. I really appreciate your fast response and code. Regex is so hard, I appreciate that you can seem to think in it. :)

Kelly

Code: Select all

F1:: ;  What this does is save a file or files I intend to move.  I then click on another file that is in the folder I want to move the original file to.  For example first file(s) is\are in C:\downloads.  The second file is in C:\TV Shows.  I click on the first file (not necessarily in Explorer), it saves the file to the clipboard, splits its path into variables.  The clipboard is then saved and cleared.  I then move the mouse to the file that is in the folder I want the first File(s) to go into, tap LControl which then splits that file's info.  A folder is then created in the second file's directory for the new file(s) (if necessary) and the original file(s) pasted into that newly created folder.  Hopefully, if the folder already exists, it will not attempt to create that folder and just paste into it.  This all works, albeit seemingly a little slow, but I'm wondering if there is any way to be more efficient with this?  

Send, ^c ; copy the selected file
	ClipWait, 2 ; wait for the clipboard to contain data.
	loop, parse, clipboard, `n, `r
	Send ^c ; copy the selected file
		;Msgbox, 4100, , %max%, 2
	if (!ErrorLevel) ; If NOT ErrorLevel, ClipWait found data on the clipboard
	SplitPath, clipboard, Oname, Odir, Oext, Oname_no_ext, Odrive 	; get the split name of the original selected file (without extension) to be moved to new or preexisting folder and saved to variables.
	msgbox, 4100, , Drive %Odrive% `n`nDir %Odir%`n`nName without extension %Oname_no_ext%`n`n`n`nDirectory and File Name - %Odir%\%Oname_no_ext%, 5
	Sleep 1000

ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
	clipboard := "" ; empty the clipboard (start off empty to allow ClipWait to detect when the data has arrived)
		CoordMode, Mouse, Screen
		x := (A_ScreenWidth / 2)
		y := (A_ScreenHeight / 2)
		CoordMode, Caret, Screen ; new
		x := A_CaretX
		y := A_CaretY
		MouseGetPos, x, y
		Sleep 500
		
		
		Msgbox, 4100, What Am I Waiting for?, Enter LControl to continue., 1
		KeyWait, LControl, D
		if ErrorLevel = 1
			Return
			
		Else
		
	Send, ^c ; copy the selected file that I need to get 
	ClipWait, 2 ; wait for the clipboard to contain data.
	loop, parse, clipboard, `n, `r
	Send ^c ; copy the selected file
		;Msgbox, 4100, , %max%, 2
	if (!ErrorLevel) ; If NOT ErrorLevel, ClipWait found data on the clipboard
	
				
	{
		clipboard = %clipboard% ; convert clipboard to plain text (= copy the path of the selected file)
		Sleep, 300 	; needs a little time to convert the clipboard
		;MsgBox, 0, , %clipboard% ,2 ; display the path, .7 sec
		SplitPath, clipboard, name, dir, ext, name_no_ext, drive 	; get the name of the selected file (without extension) 
		msgbox, 4100, , Drive %drive% `n`nDir %dir%`n`nName without extension %name_no_ext%`n`n`n`n%dir%\%name_no_ext%, 5
		Sleep 1000
		}
clipboard := ClipSaved       ;restore original clipboard
Msgbox, 4100, , %clipboard%, 1
Msgbox, 4100, , What are the variables saved say that I need?`n`nDirectory - %dir%`n`nName without extention - %Oname_no_ext%`n`nFolder that should be created SHOULD BE %dir%\%Oname_no_ext%\%clipboard%
FileCreateDir, %dir%\%Oname_no_ext%
FileMove, %clipboard%, %dir%\%Oname_no_ext%
Return

MsgBox % Explorer_GetSelection()

ClipSaved := ClipboardAll       ;save clipboard
Send, ^c
ClipWait, 2
clipboard = %clipboard%       ; convert to text
Sleep, 100
variable = %clipboard%
clipboard := ClipSaved       ;restore original clipboard
MsgBox, %variable%
return
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: How to remove string (saved to var) with RegEx or something else

02 Apr 2022, 15:30

Here is what my script does.
Look at the clipboard or variable text that looks like clipboard. If it contains S01 or S02 up to say S20 i.e., it should be able to create a folder with just the name part previous to the season number. If it has a season number, i.e. is a tv show file, create a folder for the show itself and then create a subdirectory with the name of the season (S1, S2 etc, no zero) (unless either or both the show name folder and/or the season name folder already exist, in which case it just goes on to the moving part) and move the whole file(s) to the directory.
You can test to confirm.
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: How to remove string (saved to var) with RegEx or something else

02 Apr 2022, 15:37

mikeyww wrote:
02 Apr 2022, 15:30
Here is what my script does.
Look at the clipboard or variable text that looks like clipboard. If it contains S01 or S02 up to say S20 i.e., it should be able to create a folder with just the name part previous to the season number. If it has a season number, i.e. is a tv show file, create a folder for the show itself and then create a subdirectory with the name of the season (S1, S2 etc, no zero) (unless either or both the show name folder and/or the season name folder already exist, in which case it just goes on to the moving part) and move the whole file(s) to the directory.
You can test to confirm.
Thank you very much, I'll try it some more, I appreciate your time.

Kelly
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: How to remove string (saved to var) with RegEx or something else

02 Apr 2022, 20:37

I wasn't able to understand your code Mikeww. I have very little idea of what it's doing. I got an error trying to run it that the file is not found. I know I asked for regex, because the snippet I added was written in regex. I don't understand how to incorporate it into my original code it has to fit into. I don't want to alter that because it's written in a way I can follow. I just needed to figure out how to read that one line into the clipboard and save the name of the show to a variable and save the season to know what to make for a folder name.

I do appreciate your time, I'm sorry I'm dimwitted with this, it took me literally a year and a half of trying numerous codes to figure out that longer code I posted because I didn't want to bug people with a lot of questions so I'm fairly stupid, lol. :headwall: :oops: :cry:
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: How to remove string (saved to var) with RegEx or something else

02 Apr 2022, 20:43

Below is a simplification in case it helps.

Code: Select all

haystack := "TV Show Name S01E01 Episode Name"
; haystack := "TV Show Name"
If RegExMatch(haystack, "(.+)\h+S(\d\d)", m)
     name := m1      , season := m2 + 0
Else name := haystack, season := ""
MsgBox, 64, Show, Name: %name%`n`nSeason: %season%
You can try this script by itself, to verify that it works.
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: How to remove string (saved to var) with RegEx or something else

02 Apr 2022, 22:01

OMG Mikeyww, I think this is the ticket. It's small enough that I can relatively follow it and it grabs the info I need. Thank you so much, I didn't think I'd ever figure this out. :dance:

Kelly
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: How to remove string (saved to var) with RegEx or something else

03 Apr 2022, 14:28

Any idea why this will only match on a s01 (actually S01) and e01 (E01) instead of any season or episode number? I did a ton of text manipulation on clipboard to capitalize because this wouldn't work without everything being capitalized and remove periods and put back periods for the extension to get the text the way I need it to be otherwise for this regex code to work, but I don't understand where or how it's getting the season or episode number so I can't alter this for that. I tested on a show name s06e11 (corrected in clipboard to S06 E11 then back to S06E11 for regex formatting the way it's written), and it doesn't recognize it. Thanks if you can explain. I've been working on this since 11 a.m. straight through to now, 3:30, tried regex buddy and regex magic but it's going to take me probably at least a few months to understand it enough to get this right. :headwall: Thank you.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: How to remove string (saved to var) with RegEx or something else

03 Apr 2022, 14:37

Regex is case-sensitive by default, but you can change it with the "i" option.

Code: Select all

RegExMatch(haystack, "i)(.+)\h+S(\d\d)", m)
Explained: Regex options
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: How to remove string (saved to var) with RegEx or something else

03 Apr 2022, 19:25

mikeyww wrote:
03 Apr 2022, 14:37
Regex is case-sensitive by default, but you can change it with the "i" option.

Code: Select all

RegExMatch(haystack, "i)(.+)\h+S(\d\d)", m)
Explained: Regex options
Thank you.

Kelly

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], macromint, peter_ahk, Spawnova, wineguy and 262 guests