I would like to add a feature in a script that I run monthly that will find a replace some text in the script.

2866
4
05-04-2016 08:51 AM
BobNoyes
New Contributor III

I would like to add a feature in a script that I run monthly that will find a replace some text in the script. In other words when I run the script the first thing that happens is a popup opens which prompts for the text to find and the text to replace. When finished and the popup closed the scripts runs through completion. The text that needs to be found and replaced is a .dbf file that has a name change each month. So, 16AprRes.DBF becomes 16MayRes.DBF.

In the attached script the goal is to add the find and replace popup that will cover steps 7 and 10.

Anyone have suggestions? Thanks!

0 Kudos
4 Replies
WesMiller
Regular Contributor III
BobNoyes
New Contributor III

Interesting ideas, I will experiment with these, thanks!

0 Kudos
RandyBurton
MVP Regular Contributor

If the script only needs to change the month text in a standardized file name, try using the datetime module.

import datetime

month = int(datetime.datetime.now().strftime("%m"))
year = int(datetime.datetime.now().strftime("%y"))

month_lst = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jly',
              'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

last_month = month -1
last_year = year

if last_month < 1 : last_month = 12; last_year = year -1

print month_lst[month-1], # array index starts at 0, subtract 1
print
print str(year) + month_lst[month-1] + 'Res.DBF'
print str(last_year) + month_lst[last_month-1] + 'Res.DBF'
print
print str(year*100 + month) + '_Res.DBF'

Just insert the month name where you need it in the file name.  Script would then adjust without user input.

Edit: As an afterthought, I tend to use numbers for months in file names as it helps with the sort order.

BobNoyes
New Contributor III

I took your original idea and made some adjustments. So far it seems to be working. Thanks for responding!

0 Kudos