Reputation: 3
I am trying to write a script that renames multiple folders in a specific directory, using data from an XLSX sheet, based on their name and timestamp (which are a part of the name as well, in "COMXYZ_YYYYMMDDHHMMSS"
format).
Folders in my directory will be in format of "COM123_YYYYMMDDHHMMSS"
or "COM12_YYYYMMDDHHMMSS"
or "COM1_YYYYMMDDHHMMSS"
.
The XLSX sheet will have the fields as:
FIELD1 | FIELD2 | FIELD3 | FIELD4 | FIELD5 | FIELD6 | FIELD7
COM12 A B C D E F
COM23 A B C D E F
I want the folders in my directory with the name "COM12_YYYYMMDDHHMMSS"
to be renamed as:
FIELD3_FIELD4_FIELD2_MMDDYYYY_FIELD5_NUMBER_FIELD6_FIELD7
where NUMBER
(starting at 1) increases according to the timestamp in the name after the underscore "_"
, and the date is according to the timestamp in the name as well for that specific COM#.
I have multiple folders with the same COM#, but with different timestamps. So far, I am able to rename 1 folder for each COM#, but the remaining folders are not being renamed.
Appreciate any and all help.
The code I've written so far with help from Aquiles' answer is:
import os
import random
import string
import datetime
import re
import openpyxl
from collections import namedtuple
# Get Directories
directories = [name for name in os.listdir(r'.') if name.startswith('COM')]
# Load workbook
wb = openpyxl.load_workbook('database.xlsx')
ws = wb['Sheet1']
rows = 20
for i in xrange(2, rows + 2):
if ws['A%s' % i].value >= "":
folder_to_change = [x for x in directories if x.startswith((ws['A%s' % i].value) + '_')]
folder_to_change = folder_to_change[0] if len(folder_to_change) > 0 else None
date_used = {}
if folder_to_change:
directories.remove(folder_to_change)
# print folder_to_change
folder_parts = folder_to_change.split('_')
comport = folder_parts[0]
timestamp = folder_parts[1]
# print timestamp
ts = re.search('(....)(....)(......)', timestamp)
date = ts.group(2) + ts.group(1)
# print date
time = comport
# print time
date_used[comport] = 1 if comport not in date_used else date_used[comport] + 1
# print date_used
# print folder_to_change
os.rename(
folder_to_change,
'{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}'.format(
ws['B%s' % i].value,
ws['C%s' % i].value,
ws['G%s' % i].value,
date,
ws['D%s' % i].value,
date_used[comport],
ws['F%s' % i].value,
ws['H%s' % i].value,
ws['I%s' % i].value
)
)
Upvotes: 0
Views: 765
Reputation: 881
I haven't tested this and some of the indexes and stuff may not be the right ones, but you'll get the idea from this. I believe this could work:
import os
import openpyxl
import string
# Get directories
directories = [name for name in os.listdir('.')]
# Load Workbook
wb = openpyxl.load_workbook('file.xls')
# Load first work sheet
ws = wb[0]
rows = 10
dates_used = {}
for i in xrange(2, rows + 2):
folder_to_change = [x for x in directories if x.startswith(ws['A%s' % i])]
for y, folder in enumerate(folder_to_change):
date = folder[8:16]
os.rename(folder, '{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}'.format(
ws['C%s' % i], ws['D%s' % i], ws['B%s' % i], date, ws['E%s' % i],
y+1, ws['F%s' % i], ws['G%s' % i])
Upvotes: 1