0

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.

  1. 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.

  2. 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.

  1. 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.")

enter image description here

enter image description here

w j
  • 43
  • 1
  • 7
  • Instead of looping through 12 files at a time, you can use `%` (aka modulo) to loop through all the files and move your files when `file_count % 12 == 0`. It also prevents an out of index error in this code `for file_name in file_names[:12]:`, for the case where there aren't 12 files to read in the folder. – Marcelo Paco Apr 05 '23 at 03:58

2 Answers2

0

I think this would work ... it just starts counting until there is not a file with that name (and doesnt change the extension)

def copy_file(source_file_name):        
    basename = os.path.basename(source_file_name)
    fname_initial = fname = f"{destination_dir}/{basename}"
    if os.path.exists(fname):
       for i in count(1):
           fname = f" ({i})".join(fname_initial.splitext())
           if not os.path.exists(fname):
              break
    print(f"Fname = {fname}")
    # now actually copy the file
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

Thanks for the help. While I don't think I went with your guys exact answer. i definitely was close.

def rname():
    os.chdir(dir_path)
    print("Renaming Files.")
    #print(os.getcwd())
    for index, f in enumerate(os.listdir()):
        f_name = os.path.splitext(f)[0]
        ext_name = os.path.splitext(f)[1]
        br = f_name[0:7] + "{:02d}".format((index % 12) + 1)
        new_name = f'{br}{ext_name}'
        os.rename(f, new_name)
    print("Moving to folder create")
    folcreate()
   # move_file()
w j
  • 43
  • 1
  • 7