Originally tried this with PowerShell and it kicked my butt.
So I switched to python since I know this better. There are still a few key issues in which I am having trouble.
What is supposed to happen is that the program checks if there are any files in the parent folder. If there is, they will remove them 12 at a time and place them in a new folder. I got them to remove 12 at a time. And even create a new folder.
How do I loop it so it creates ANOTHER folder with an incrementing folder name. Basically what you do in windows explorer when you our copying the same folder over and over.
How do I send it to the folder that was JUST created? My code has it going to a folder already created. Just to make sure it is somewhat working.
Then once the files are in the folder. These files are in a naming convention of T1R1C1L3 I would like to remove everything after the L then replace it with an incrementing number in a 2-digit format. So, the above should read T1R1C1L03.
- How do you rename the files in this way?
I have also placed all of my work, with test data in github.
github link My code is below:
import os
from pathlib import Path
import shutil
# folder path
dir_path = r'C:\Users\Admin\Desktop\TEST'
count = 0
directory = "FOLDER_{}".format(count + 1)
test2_path = r'C:\Users\Admin\Desktop\test2'
#checking the directory for files
for path in os.listdir(dir_path):
# check if current path is a file
if os.path.isfile(os.path.join(dir_path, path)):
count += 1
print('File count:', count)
#if there are files
if count > 0:
print("There are still files in this directory.")
os.mkdir(dir_path + directory)
print("Directory '% s' created")
#moving files
file_names = os.listdir(dir_path)
for file_name in file_names[:12]:
#need to figure out the proper way to move to just created folder
shutil.move(os.path.join(dir_path, file_name),test2_path)
#need to rename the files extension to incrimneting number in 2 digit format
os.chdir(test2_path)
print(os.getcwd())
for count, f in enumerate(os.listdir()):
f_name = os.path.splitext(f)
f_name = f + str(count + 1)
new_name = f'{f_name}'
os.rename(f, new_name)
else:
print ("There are no files in the directory.")