Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 how to increment all file names in a folder
#1
how do i create a short script to change all file names in a folder with an increment?

is there a guide or a simple rule?

want to use it as a practice for coding with python i am a beginner
Quote
#2
You want the os module. It has a rename function for renaming files, and a listdir function for finding the files in a folder (or a walk function for doing it recursively). The re module could be helpful for finding the index in the file name, you could do it with the isdigit method of strings. Or if it's consistently the same number of digits at the end of a file, you could just use slicing.
SoulsKeeper and Mekire like this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
Can pick up some tips in this recent post.
You can also look into pathlib,it's a new way to handle filesystem paths ect...
ichabod801, Skaperen, SoulsKeeper like this post
Quote
#4
are the file names just numbers like "12345678" or do they partly have numbers like "data2018"?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#5
(Sep-09-2018, 06:43 AM)Skaperen Wrote: are the file names just numbers like "12345678" or do they partly have numbers like "data2018"?
All the files in the folder got the exact same name which is a pain to work with, there are thousands of them so I probably need to use a loop right?

I need it to automatically go from file to file and add a number to each of them like so:
file.

file1.

file2.

and so on...
Quote
#6
so you need to make a mass rename. yeah that obviously needs a loop. you could write a script to do it. you could do it in Python, but in a shell script might be easier if you know shell scripting. you could do it in Python as a learning exercise. me, personally, i'd do it in a command (in bash) in that directory like:

(ls -1|while read f;do mv $f ${f}1;done)

if you want the "1" to be before the "." it will be a slightly longer command. but maybe it is better for you to do this as a Python script. write something that outputs the commands to do it and when it makes all the right commands then pipe them into a shell.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  make a list of the file in the folder and change the name of file regarding to time go127a 4 167 Feb-11-2020, 12:23 PM
Last Post: DeaD_EyE
  How can I paste an entire file from clipboard to a folder? daverave1212 5 177 Feb-08-2020, 04:33 PM
Last Post: snippsat
  Python: possibilty ro make a list of the file in the folder & change the name of file go127a 0 88 Feb-05-2020, 01:16 PM
Last Post: go127a
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 417 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  splitstring file names a by hyphen steve22020 3 479 Oct-30-2019, 05:39 PM
Last Post: steve22020
  Copy a File & Increment File Count xingfang 1 227 Oct-29-2019, 03:58 PM
Last Post: DeaD_EyE
  How can I increment a List item with in a "for in" msteffes 4 551 Aug-14-2019, 08:55 AM
Last Post: DeaD_EyE
  Is there a more effecient way to do this ? File Names sumncguy 2 279 Jul-11-2019, 12:47 PM
Last Post: DeaD_EyE
  How to combine file names into a list from multiple directories? python_newbie09 3 626 Jul-09-2019, 07:38 PM
Last Post: python_newbie09
  How define iteration interval increment SriMekala 5 590 Jun-01-2019, 01:06 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)