Python Forum
Rename files in a folder named using windows explorer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rename files in a folder named using windows explorer
#1
Hi everyone,

Many of us know we can rename multiple files with windows explorer following the steps here:

https://support.microsoft.com/en-us/topic/how-to-rename-multiple-files-in-windows-xp-with-windows-explorer-e0a7499f-6cd2-5147-3ce6-7748b6dd0dcb#:~:text=Select%20multiple%20files%20in%20a,name%2C%20and%20then%20press%20ENTER.

I have a folder that already contains many files named using the above way.

Now I need to delete some files in that folder. Instead of repeating the steps again manually, can I have a Python code to help me automatically update the names? For example, if there are 3 files named as: XYZ (1), XYZ (2) and XYZ (3). If XYZ (1) is deleted, I want the remaining two files to be renamed as XYZ (1) and XYZ (2) which reflects a new continuous order.
Reply
#2
You can rename files using the Python module os. Just look up os.rename().

You probably should first check whether or not a file with the new name exists.

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile(new_name):
    print("The file already exists")
else:
    # Rename the file
    os.rename(old_name, new_name)
The next part for you is trickier. I would think you should first delete all the files you don't want.

Then you can locate all file names containing XYZ( like below.

I have a lot of files that have the format correctAnswers_Week1.txt

import glob
path2files = '/home/pedro/summer2023/21BE/correctAnswersCW/correctAnswers_Week*'
myfiles = glob.glob(path2files)
for f in myfiles:
    print(f)
Let's say you have a file XYZ(2).txt as the first remaining XYZ file you could change the name like this

f = 'XYZ(2).txt'
part_name = f.split('(')
new_name = part_name + '(' + str(count) + ').txt'
os.rename(f, new_name)
Then you can use a loop change the names.

myfiles.sort()
count = 1
for f in myfiles:
    part_name = f.split('(')
    new_name = part_name + '(' + str(count) + ').txt'
    os.rename(f, new_name)
    count +=1
BUT, if your XYZ files have numbers greater that 9 the order will be wrong, because XYZ(10).txt will come before XYZ(2).txt

You could avoid that by counting the length of the string name.

If f = 'XYZ(2).txt' then len(f) = 10 so skip the file if len(f) > 10

Change the longer file names later in the loop.

But, if you don't have file names with numbers > 9, no problem.

I'm sure the experts here know better ways to do this, but this may give you some ideas!
Reply
#3
Hi Pedroski55, many thanks for your help. The folder contains files with various names instead of XYZ :) So the loop may need to go through each file name to check.

I am not familiar with Python - not sure how this interesting task can be accomplished in Python.
Reply
#4
What do you want the program to do? Should the program delete a selected file and rename any associated files? Should the program rename any files that end with (X) to remove any gaps in X? Do you want to provide a pattern it should use when searching for files (python resequence.py "XYZ.txt") or should it look at all files in a folder?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Deleting Windows temp folder Raysz 7 454 Apr-02-2024, 12:36 PM
Last Post: Raysz
  Variable Explorer in spyder driesdep 1 235 Apr-02-2024, 06:50 AM
Last Post: paul18fr
Question How to add Python folder in Windows Registry ? Touktouk 1 278 Feb-20-2024, 01:04 PM
Last Post: DeaD_EyE
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 566 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename all files in a folder hitoxman 9 1,513 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  Phyton Opening files on windows C: pc EddieG 3 994 Mar-29-2023, 03:19 PM
Last Post: buran
  How to loop through all excel files and sheets in folder jadelola 1 4,523 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python gzip all files from a folder mg24 3 4,034 Oct-28-2022, 03:59 PM
Last Post: mg24
  delete all files and subdirectory from a main folder mg24 7 1,637 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  Merge all json files in folder after filtering deneme2 10 2,373 Sep-18-2022, 10:32 AM
Last Post: deneme2

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020