0

I have a directory of 500 files that I need to batch rename. The files are collected via a google drive form in which the applicant submits 5 individuals files labeled as such:

CPW541_PersonalInformationSheet
CPW541_Resume
CPW541_ShortResponses
CPW541_Essay
CPW541_AcademicSummary
SEL285_PersonalInformationSheet
SEL285_Resume
SEL285_ShortResponses
SEL285_Essay
SEL285_AcademicSummary

I would like to batch rename the above file names to (in alphabetical order from the top of the directory):

1_PersonalInformationSheet
1_Resume
1_ShortResponses
1_Essay
1_AcademicSummary
2_PersonalInformationSheet
2_Resume
2_ShortResponses
2_Essay
2_AcademicSummary

I tried using the question here as a reference, but I couldn't make sense of how to rename every five files. Any help would be greatly appreciated!

  • what have you tried so far? – usernamenotfound Jan 18 '18 at 19:03
  • I've tried using https://mrrsoftware.com/namechanger/ but it only works if you populate the characters you want replaced manually and attach a number to those characters. (E.g. CPW541 --> 1). I also tried using the question I referenced at the bottom but that just replaced the files if the change is specified or all hyphens to dashes. – Chris Whitehair Jan 18 '18 at 19:08
  • how fluent are you with python? – usernamenotfound Jan 18 '18 at 19:08
  • I've taken an introductory course in python that stopped at OOO. It's been a while since I've used the code. The code I am looking to make would be for a student organization of mine at UT Austin. – Chris Whitehair Jan 18 '18 at 19:11
  • Invest some time with [the Tutorial](https://docs.python.org/3/tutorial/index.html) practicing the examples. It will give you an idea of the tools Python offers to help you solve your problem. – wwii Jan 18 '18 at 20:14

2 Answers2

1

Assuming the ordering isn't important, the following might be enough - get the list of files, figure out all the prefixes and suffixes (split on the _), then for each prefix, rename each file from prefix_suffix to count_suffix:

from os import listdir, rename
from os.path import join
suffixes = ['PersonalInformationSheet', 'Resume', 'ShortResponses', 'Essay', 'AcademicSummary']
filedir = "/tmp/foo"

prefixes = set(prefix.split('_')[0] for prefix in listdir(filedir))
for count, prefix in enumerate(prefixes):
    for suffix in suffixes:
        rename(
            join(filedir, "{}_{}".format(prefix, suffix)),
            join(filedir, "{}_{}".format(count+1, suffix))
        ) 
match
  • 10,388
  • 3
  • 23
  • 41
  • I am getting "SyntaxError: multiple statements found while compiling a single statement". Also I really appreciate your help getting this far. – Chris Whitehair Jan 18 '18 at 19:31
  • That's usually caused by whitespace issues, or mis-pasting multiple lines into one prompt in the python shell. Have you tried writing this all to a file then running it? – match Jan 18 '18 at 19:34
0

*It has been brought up that the suffixes are human entered, and thus might be misspelled, not all capitalized identically, etc. This should do the trick:

from os import listdir, rename
from os.path import join, exists
filedir = "/tmp/foo/"

files = listdir(filedir)
prefixes = set(prefix.split('_')[0] for prefix in files)
suffixes = set(prefix.split('_')[1] for prefix in files)

for count, prefix in enumerate(prefixes):
    for suffix in suffixes:
     if exists(filedir+prefix+'_'+suffix):
      rename(filedir+prefix+'_'+suffix,filedir+str(count+1)+'_'+suffix)

That turned my test dir:

asdf_part1
asdf_part2
fdsa_pt1
fdsa_ptTwo

into:

1_part1
1_part2
2_pt1
2_ptTwo
Brian Ellis
  • 70
  • 1
  • 5