Bulk rename files in one folder to match irregular list in another?
February 21, 2019 9:11 AM   Subscribe

I have two folders of several thousand files. One is a bunch of sequentially-named, identical text files (.txt). The other folder contains the same quantity of files of a different extension (lets say .mp3) that are NOT sequential nor ordered nor identical in any other way than they share the same extension. I need to rename all of the text files to have the same filenames as all of the audio files. Help? Possibly using Adobe Bridge? Other?

For example, I have these two folders:

FOLDER A
textfile001.txt
textfile002.txt
textfile003.txt
textfile004.txt

FOLDER B
j390rvh230f_8374hw.mp3
94858ere.mp3
SDHIOE32d_mw2-q.mp3
98764zxcvbn.mp3


The result i need is for FOLDER A to contain:

j390rvh230f_8374hw.txt
94858ere.txt
SDHIOE32d_mw2-q.txt
98764zxcvbn.txt


While leaving FOLDER B untouched.

I have Adobe Bridge which has a pretty decent batch renaming feature, but This is eluding me. I would prefer a software (free or paid) solution to this. I'd rather not delve into command line if at all possible.

Thank in advance!
posted by sandra_s to Computers & Internet (13 answers total) 1 user marked this as a favorite
 
How are you identifying which text file goes with which MP3?
posted by Doofus Magoo at 9:19 AM on February 21, 2019 [1 favorite]


Response by poster: @Doofus Magoo: It doesnt matter , all the txt files are identical. Thanks!
posted by sandra_s at 9:21 AM on February 21, 2019


Bulk Rename Utility should be able to do this pretty easily. You may need to make a list of what the current file names are, and what they should be, but that should be an easier problem to solve I think.
posted by matrixclown at 9:34 AM on February 21, 2019 [2 favorites]


If all the text files are identical, this is less a batch-rename job and more a batch-copy job - you could (in theory) delete all but one of the text files in Folder A, then for each "filename".mp3, copy textfile001.txt to "filename".txt.

I don't have any experience with Adobe Bridge, If I were to do this I'd use the command line (specifically, Linux Bash, but I'd be surprised of Windows Powershell didn't let you use almost the same syntax), but if you're googling for solutions as well, I'd broaden your parameters to look for batch-copy examples rather than just batch-rename.
posted by Nice Guy Mike at 9:35 AM on February 21, 2019 [1 favorite]


I know you don't want a command-line solution, but here's a fairly easy one (assuming a bash-like shell)...

If I start with files like this (just one text file in my folder-a - because they're all identical, right?):

$ ls ./folder*/*
./folder-a/text001.txt
./folder-b/first-randomly-named-file.mp3
./folder-b/second-randomly-named-file.mp3
./folder-b/third-randomly-named-file.mp3
./folder-b/fourth-randomly-named-file.mp3

I can do a for loop like this:

$ for i in ./folder-b/*
> do
> j=${i##*/}
> k=${j%.*}
> cp ./folder-a/text001.txt ./folder-a/${k}.txt
> done

(we start with a list of all the randomly-named files in folder-b - then the j={} part strips off everything up to & including the last forward slash - the k={} part strips off the dot and everything after it - leaving only the base filename - then we use that value in the copy command)

so I end up with this:

$ ls ./folder*/*
./folder-a/text001.txt
./folder-a/first-randomly-named-file.txt
./folder-a/second-randomly-named-file.txt
./folder-a/third-randomly-named-file.txt
./folder-a/fourth-randomly-named-file.txt
./folder-b/first-randomly-named-file.mp3
./folder-b/second-randomly-named-file.mp3
./folder-b/third-randomly-named-file.mp3
./folder-b/fourth-randomly-named-file.mp3

Which I think is what you want? You can delete ./folder-a/text001.txt when you're finished, because now you have a lot of copies of it.
posted by rd45 at 9:51 AM on February 21, 2019 [1 favorite]


Response by poster: @rd45 That sounds right, but I dont know what "bash-like shell" is :-)
posted by sandra_s at 9:59 AM on February 21, 2019


This is really the right way to do it. If you are on a Mac a "bash-like shell" is just the Terminal application - on Windows you'd need to install Powershell.

Make a backup copy of both folders somewhere else first.
posted by aspersioncast at 10:04 AM on February 21, 2019


zen@gaz:~/foldera$ ls ../folderb/
bar.mp3  bat.mp3  baz.mp3  foo.mp3

zen@gaz:~/foldera$ for i in ../folderb/*.mp3; do cp foo001.txt "$(basename $i .mp3).txt"; done

zen@gaz:~/foldera$ ls
bar.txt  bat.txt  baz.txt  foo001.txt  foo.txt
Yeah, you probably want PowerShell and somebody can come up with the 'easy' way
to do it using PowerShell.
posted by zengargoyle at 11:13 AM on February 21, 2019


Best answer: Here's a PowerShell script to do it. You most likely have PS installed if you are using a recent-ish Windows OS. If not, it's free from MS and pretty quick to install. This could be done on one line and with fewer words but it might be harder to understand.

• Do this in "PowerShell ISE" rather than just Powershell.
• Copy everything below the dashes below into the script pane *which you may have to click View -> Show Script Pane to see.
• Replace "e:\FolderA" below with the path to your MP3 files.
• Create a single copy of the text file that you'd like copies of and replace "E:\FolderB\TextDocTemplate.txt" below with the path to that file.
• Run the script with the green 'play' icon or by pressing F5. MeMail me if you want help making this go. Always work on a copy of the text file and the MP3s, if at all possible, so you don't hose the original copies with a typo.


#------------------
$MP3FileList = Get-ChildItem E:\folderA

ForEach($MP3 in $MP3FileList){
$TXTFileName = ($MP3.Name -replace '.mp3','.txt')
Copy E:\FolderB\TextDocTemplate.txt e:\FolderB\$TXTFileName
}
posted by Clinging to the Wreckage at 12:56 PM on February 21, 2019


I'm a perfectly good programmer, but I don't do this kind of thing often enough to be in practice.

So when I need to do something like this in Windows, I'd
  1. Open Excel
  2. Open folder A and folder B in Explorer
  3. Select all in folder A
  4. Shift + right click on the selected files
  5. Select Copy as path
  6. Go to Excel and paste that into column A
  7. Repeat for folder B and column B
  8. Type ="move "&A1&" "&B1 into cell C1, and fill down
  9. Copy column C into Notepad
  10. Open a cmd window, and paste from Notepad into the cmd window
I promise that's a lot easier than it looks.
posted by ambrosen at 1:07 PM on February 21, 2019


If you're someone (like me) who prefers having a launchable program to do this kind of thing, A Better Finder Rename has been my lifeline.

Create a text file with a list of all your filenames as they precisely map, line by line, with the files you want to rename. (For example, file #1 is called textfile001.txt and line #1 in your text doc would be j390rvh230f_8374hw.txt, and so forth.) You can then use the Advanced & Special > Rename From File List function in ABFR to load this text doc and rename those files, in sequence, as they appear in both your list and your folder. One click, ta da.
posted by mykescipark at 1:10 PM on February 21, 2019


Response by poster: Clinging to the Wreckage FTW!! But thanks, everyone!!!
posted by sandra_s at 1:19 PM on February 21, 2019


If you need to do this kind of thing fairly often, a little script can automate it conveniently. On Windows I like to use cmd scripts rather than PowerShell scripts for jobs this small, purely because cmd scripts can be launched by dragging and dropping stuff onto them without needing to fartarse about with system policy settings first; you don't need to go to the bother of opening up the PowerShell ISE and playing with its windows and buttons.

Given Folder B as above, I would use the Windows file explorer to

1. Make a new, empty Folder A.

2. Copy ~template.txt and ~make-text-files.cmd into Folder A.

3. Open Folder B, hit Ctrl-A to select all the files inside it, then drag one of those files and drop it onto the ~make-text-files icon inside folder A. Folder A would immediately fill with copies of ~template.txt, each one named for a file in the selection group.

4. Delete ~template.txt and ~make-text-files.cmd from Folder A (conveniently done because the ~ at the beginning of each of their names makes the file explorer sort them to the top of the list).



To construct the initial copy of ~make-text-files.cmd before doing all the above:

1. Open a new Notepad window.

2. Paste in the following line:
for %%F in (%*) do copy "%~dp0~template.txt" "%~dp0%%~nF.txt"
3. Choose File->Save, change the "Save as type" from "Text Documents (*.txt)" to "All files", and save as ~make-text-files.cmd. If done correctly, the result will appear to be named ~make-text-files and it will have a little gearwheels icon rather than something that looks like a blank piece of paper.



A rough translation of the script line above into English:

%* means "all of the items handed to the script when it was started". When you launch a script from the file explorer by dragging and dropping a bunch of files onto it, those items will be the names of the dropped files.

for %%F in (%*) do ... means "do ... once for each of the items from the list %*, replacing any occurrence of %%F inside ... with the item". So in this case, the command copy "%~dp0~template.txt" "%~dp0%%~nF.txt" would get repeated once for each of the files you drag and drop, with the name of the file substituted for each occurrence of %%F.

%0 means the name of the script file itself, and %~dp0 means just the drive letter and folder pathname parts of that name. So if the copy of the script you're dragging and dropping things onto is at C:\Users\sandra_s\Documents\Folder A\~make-text-files.cmd, then every occurrence of %~dp0 inside the script would be replaced with C:\Users\sandra_s\Documents\Folder A\.

The same kind of ~-based modifiers are allowed inside notations like %%F, and if %%F would expand to the name of a file, %%~nF means just the name itself - not including drive letter, folder pathname or filename extension. So if you dragged and dropped the file at C:\Users\sandra_s\Documents\Folder B\98764zxcvbn.mp3 onto the script, %%~nF would be replaced with 98764zxcvbn.

All of which means that if you drag and drop all four of the MP3 files you listed above from Folder B onto C:\Users\sandra_s\Documents\Folder A\~make-text-files.cmd, it would end up running the following commands:
copy "C:\Users\sandra_s\Documents\Folder A\~template.txt" "C:\Users\sandra_s\Documents\Folder A\j390rvh230f_8374hw.txt"
copy "C:\Users\sandra_s\Documents\Folder A\~template.txt" "C:\Users\sandra_s\Documents\Folder A\j94858ere.mp3.txt"
copy "C:\Users\sandra_s\Documents\Folder A\~template.txt" "C:\Users\sandra_s\Documents\Folder A\SDHIOE32d_mw2-q.mp3.txt"
copy "C:\Users\sandra_s\Documents\Folder A\~template.txt" "C:\Users\sandra_s\Documents\Folder A\98764zxcvbn.mp3.txt"


If ~template.txt is fairly small and simple and doesn't need editing all that often, it might be more convenient to incorporate its content inside the script itself rather than needing to keep track of a separate text file. Post back if that idea appeals and I'll show you how it's done.
posted by flabdablet at 7:13 PM on February 21, 2019


« Older Gmail won't load   |   Appropriate attire in Las Vegas? Newer »
This thread is closed to new comments.