Add Part of Folder Name to File Name Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cjsmile999
Posts: 42
Joined: 01 Feb 2017, 09:33

Add Part of Folder Name to File Name

Post by cjsmile999 » 05 Dec 2022, 14:04

Hello everyone! I'm trying to rename the files within each folder with part of the folder name they reside in. Example grab the artist last name from the folder and add that to end of each of the files within that folder: Waiting on the World to Change_Mayer.mp3, ect... Below is a simple example of the file structure.

Current Setup:
Music (folder)
Continuum_John_Mayer_2006 (Folder)
Waiting on the World to Change.mp3
Belief.mp3
Vultures.mp3
Just a Little Love_Reba_McEntire_1984 (Folder)
Just a little love.mp3
Poison Sugar.mp3
If Only.mp3
Ect....

Proposed Setup:
Music (folder)
Continuum_John_Mayer_2006 (Folder)
Waiting on the World to Chance_Mayer.mp3
Belief_Mayer.mp3
Vultures_Mayer.mp3
Just a Little Love_Reba_McEntire_1984 (Folder)
Just a little love_McEntire.mp3
Poison Sugar_McEntire.mp3
If Only_McEntire.mp3
Ect....

I've been able to work out creating a list of folders and parse out section (last name) that I need. What I can't figure out is how to loop through that list and take last name and add it to the end of each file within that corresponding folder.

Code: Select all

FileList := "" 
Path := "E:\Music\*"

;msgbox, %path%
Loop, Files, %Path%, D 
    FileList .= A_LoopFileFullPath "`n"
msgbox, % FileList

Loop, parse, FileList, `n
{

	if (A_LoopField = "")  ; Ignore the blank item at the end of the list.
        continue
	SplitName := StrSplit(A_LoopField, "_") ; Omits periods.
	MsgBox % SplitName[3]
    MsgBox, 4,, File number %A_Index% is %A_LoopField%.  Continue?
    IfMsgBox, No
        break
}

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

Re: Add Part of Folder Name to File Name  Topic is solved

Post by mikeyww » 05 Dec 2022, 14:18

Code: Select all

dir := StrReplace(A_Desktop, "Desktop", "Music")
Loop, Files, %dir%\*, D
{ If !Instr(sub := A_LoopFilePath, "_")
   Continue
  part := StrSplit(sub, "_"), artist := part[part.Count() - 1]
  Loop, Files, %sub%\*.*
  { SplitPath, A_LoopFilePath,,, ext, fnBare
    FileMove, %A_LoopFilePath%, %sub%\%fnBare%_%artist%.%ext%
  }
}
MsgBox, 64, Done, Done!

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

Re: Add Part of Folder Name to File Name

Post by flyingDman » 05 Dec 2022, 18:56

@mikeyww do you mean part := StrSplit(sub, "\")?
14.3 & 1.3.7

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

Re: Add Part of Folder Name to File Name

Post by mikeyww » 05 Dec 2022, 19:02

No. The folder example is Continuum_John_Mayer_2006. The file path given by sub is a directory path. After the split, the artist is found by looking for the part that is next to last. The array element number is the element count minus one.

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

Re: Add Part of Folder Name to File Name

Post by flyingDman » 05 Dec 2022, 22:14

I stand corrected. I misread.
14.3 & 1.3.7

cjsmile999
Posts: 42
Joined: 01 Feb 2017, 09:33

Re: Add Part of Folder Name to File Name

Post by cjsmile999 » 06 Dec 2022, 06:38

Thank you! Works like a champ!!! I'm amazed how helpful everyone is and taking the time to teach us! Hats off to you all, and thank you!

Post Reply

Return to “Ask for Help (v1)”