Jump to content

WINDOWS 11-add number prefix to filename script?

Carlos Rendon

Hey guy! Always grateful for the help here. 

 

Is there a way that I can batch add a prefix to file names in a folder? I tried PowerToys, does not let me add  prefix to different file names .....  

 

What would be Powershell script to do this ?  

 

FOR EXAMPLE: 

In one folder I have 100 files all with different names     

 

Original Filenames 

Dog Walking

Dog sitting

Mom at dinner 

 

CHANGE NAMES TO : 

001-Dog Walking

002-Dog sitting

003-Mom at dinner  

ETC....

 

How do I do this?

 

Thanks

 

Link to comment
Share on other sites

Link to post
Share on other sites

https://www.bulkrenameutility.co.uk/ might be able to do that. it's been ages since i've last used it though.. and it's not the most user friendly UI.

 

as for doing this with a batchfile, i assume this sort of setup:

https://stackoverflow.com/questions/180741/how-to-do-something-to-each-file-in-a-directory-with-a-batch-script

and then just have a counter increment on each file, and do a "move" from the original filename to %counter%-%filename%

 

the exact same method can be done in powershell as well, with different structures ofcourse.

 

unfortunately i'm really not that fluent with batch or powershell to just brabble out a working example.

 

having that said.. python is always an option too:

- you pull the list of files into a 'list' object.

https://pynative.com/python-list-files-in-a-directory/#:~:text=Use os.listdir() function,directory given by the path .

- do some form of 'for each' structure. you can cheat here by determining the list length, using that as the upper limit of a for loop, and using that loop's counter as the counter for your files.

- convert the counter to a 3-character string (iirc python has options for automatically padding there), new name = counter string + '-' + old name.

- os.rename the thing, rinse and repeat.

Link to comment
Share on other sites

Link to post
Share on other sites

Please only use the script if you understand the function.

Do not run it within another directory than your folder containing the files you want to rename.

Test it before using it one your important files.

 

If the file already starts with ex. 001- the script will replace it with the new numbering ($_.Name -replace '^[0-9]{3}-','').

Powershell:

$i=0;Get-ChildItem | Sort-Object | ForEach-Object{ $i++; Rename-Item -Path $_.FullName -NewName ($i.ToString("000")+"-"+($_.Name -replace '^[0-9]{3}-','') ) }

image.thumb.png.84a8f20bff9ad9f185998f7a5fff7b9f.png

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×