Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can i bulk rename music files in reverse order from artist - song name to songname - artist

How can I bulk rename music files in reverse order from artist - song name to songname - artist.

I actually dont need to rename the actual files.


Its just i have a huge txt list of 100,000 music names that are listed as so..



Artist - Song Name

Artist - Song Name

Artist - Song Name

Artist - Song Name

Artist - Song Name

Artist - Song Name


and i need this list to reverse it self so it is listed in the list as


Song Name - Artist

Song Name - Artist

Song Name - Artist

Song Name - Artist

Song Name - Artist

Song Name - Artist

Song Name - Artist


Im sure there is some kinda way to auto a mix of applescript/automator/terminal to do something like: all char before the "-" replace with the chars after the "-".


Please some one help me if you know how I can pull this off

Mac OS X (10.7.1)

Posted on Sep 25, 2011 11:27 PM

Reply
6 replies

Sep 26, 2011 1:28 AM in response to travisfrommiami

assuming that you have the big list stored in a plain-text file, you can use this:


set filePath to "/path/to/textfile.txt"

set bigList to readfilePath

set pGraphs to paragraphs of bigList

set outputText to ""

repeat with thisLine in pGraphs

set firstArray to tid(thisLine, " - ")

set secondArray to {item 2 of firstArray, item 1 of firstArray}

set outputText to outputText & tid(secondArray, " - ") & return

end repeat

set fp to open for access "/path/to/textfile2.txt" with write permission

writeoutputTexttofp

close accessfp


on tid(input, delim)

set {oldTID, my text item delimiters} to {my text item delimiters, delim}

if class of input is list then

set output to input as text

else

set output to text items of input

end if

set my text item delimiters to oldTID

return output

end tid

The first path in line 1 is to the file, the second one in line 10 is where you want to write the output.

Sep 26, 2011 4:57 AM in response to travisfrommiami

Assuming your shell is bash, ksh or zsh:

cat song.txt

Artist - Song Name
Artist - Song Name
Artist - Song Name
Artist - Song Name
Artist - Song Name
Artist - Song Name


while IFS=" - " read -r ARTIST SONG; do
echo "$SONG - $ARTIST"
done < song.txt > newsong.txt

cat newsong.txt

Song Name - Artist
Song Name - Artist
Song Name - Artist
Song Name - Artist
Song Name - Artist
Song Name - Artist


Message was edited by: Mark Jalbert

Sep 26, 2011 2:11 PM in response to travisfrommiami

Another one in AppleScript…


The following script assumes your list is contained in file "Original file.txt" on the Desktop, and should return the new list in file "New file.txt" also on the Desktop. The script takes about 30 seconds to reverse 100,000 music names on my MacBook Pro under OS X 10.7.1 (except the first time it is run, when it is much slower, so that I suggest to run it first on a very short list).


set pathToDesktop to POSIX path of (path to desktop) as text

set theOriginalFile to POSIX file (pathToDesktop & "Original file.txt")

set theNewFile to POSIX file (pathToDesktop & "New file.txt")


set t0 to current date


set F1 to open for accesstheOriginalFile

set theText to read F1

close accessF1


set theParagraphs to paragraphs of theText

set bigList to a reference to theParagraphs


set theNewParagraphs to {}

set bigNewList to a reference to theNewParagraphs


repeat with X in bigList

set P to offset of " - " in X

if P = 0 then

set Y to X

else

set Y to text (P + 3) through -1 of X & " - " & text 1 through (P - 1) of X

end if

copy Y & return to the end of bigNewList

end repeat

set theNewText to text 1 through -2 of (bigNewList as text)


set F2 to open for accesstheNewFile with write permission

set eofF2to 0

writetheNewTexttoF2

close accessF2


display dialog "Done in " & (current date) - t0 & " seconds." buttons {"OK"} default button 1 with icon 1

Sep 26, 2011 5:30 PM in response to travisfrommiami

How can I bulk rename music files in reverse order from artist - song name to songname - artist.

I actually dont need to rename the actual files.

Its just i have a huge txt list of 100,000 music names that are listed as so.


I wonder if we haven't misunderstood what you were asking for. Could it be that you have not a huge file, but just a huge list of names in many small files. If that's the case, you should rather use the following script instead of the previous one.


Since this script does not rename the files, make a backup of your files before running it.



set theFolder to choose folder-- the folder containing your text files


tell application "Finder"

set theNames to name of files of theFolder whose name extension is "txt"

end tell

set theTextFiles to {}

set theFolderPath to POSIX path of theFolder

repeat with thisName in theNames

copy theFolderPath & thisName to the end of theTextFiles

end repeat


repeat with thisFile in theTextFiles

set F1 to open for accessthisFile

set theText to read F1

close accessF1


set theParagraphs to paragraphs of theText

set bigList to (a reference to theParagraphs)

set theNewParagraphs to {}

set bigNewList to (a reference to theNewParagraphs)


repeat with X in bigList

set P to offset of " - " in X

if P = 0 then

set Y to X

else

set Y to text (P + 3) through -1 of X & " - " & text 1 through (P - 1) of X

end if

copy Y & return to the end of bigNewList

end repeat

set theNewText to text 1 through -2 of (bigNewList as text)


set F2 to open for accessthisFile with write permission

set eofF2to 0

writetheNewTexttoF2

close accessF2

end repeat


display dialog "Done!" buttons {"OK"} default button 1 with icon 1


Message was edited by: Pierre L.

How can i bulk rename music files in reverse order from artist - song name to songname - artist

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.