I need to rename png files using a string contained in a txt file.
I have png files named like (the only words that change are the sizes):
Event_Post_260x200_CITY.png
and 1 txt file in which I have written:
Place_Event,Date_Event
Test,10 September 2018.
The txt file has this formatting because I use a script in Photoshop.
How can I rename the png files using the txt file like:
Event_Post_260x200_Test.png
I tried different ways but I am not able to achieve that.
I'm working on Windows 10.
Thank you for your help.
@powershell -command get-childitem *.png | foreach { rename-item $_ $_.Name.Replace("CITY", "TEST") }
This should do the job for you when you got python installed. Inline there are comments for you where you can change things. There is also explenation what the code does for you at that particular point.
Copy the code to a filename.py and run it from the command-line.
...from command-line: python remane_filenames.py
- File can be opened with notepad and from an editor like IDLE or Komodo edit.
- Variables can be edited by you to change city, date, event_place/name, etc.
- Select my answer and vote on it so my effort is not pure getting the dust from sunk-away-code .
Enjoy conversion ;-)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# remane_filenames.py
import os, shutil
print os.path.realpath(__file__) # path where file.py is executed from.
myfilelist = os.listdir('.') # creates filelist. The '.' can be another folder.
destfolder = "D:\output" # target folder (may created automatically.
ftype = 'png'
# variables you can change yourself
Place_event = 'Windmils'
Date_event = '2018-01-26'
City = None
# main code:
for scr in myfilelist: # steps through a list of files.
if os.path.isfile(scr) == True: # checks if file is file and not a folder.
# print item # shows the filename on which we work.
myfile = scr.split('.')
if myfile[1] == ftype: # correct filetype?
try:
event, post, size, city = myfile[0].split("_")
print event, post, size, city
if City == None:
City = city #use default cityname
filename = Place_event + '_' + post + '_' + Date_event + '_' + size + '_' + City + "." + ftype
print filename
dest = os.path.join(destfolder, filename)
print dest
# if the folder doesn't exist..create it.
if os.path.isdir(destfolder) == False:
os.mkdir(destfolder)
shutil.copy2(scr, dest)
except Exception as e:
# print e # print line for debugging code to see if something is not working.
continue