I need to make it so when i click the Do IT botton the variables ive shown take the values of the text control box ive selected, and after an hour trolling the internet and trying different lines of code i still have no clue. Does anyone have any ideas? Thanks guys. (ive used comments for clarity)

#!/usr/local/bin/python

# Authour - Rhys
# Version - 1.00

import re, os, wx
from os.path import join as pjoin, isdir


class renamerMacro(wx.Frame):
    
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Renaming Macro', size=(300, 350))
        panel = wx.Panel(self)

        exitbotton = wx.Button(panel, label="Close", pos=(210, 230),size=(50, 30))
        self.Bind(wx.EVT_BUTTON, self.OnCloseMe, exitbotton)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

        doit = wx.Button(panel, label="Do it", pos=(20, 230),size=(50, 30))
        self.Bind(wx.EVT_BUTTON, self.OnDoIt)

        wx.StaticText(panel, -1, "Folder Location", (22, 20)) # Txt ctrl 1
        self.folderinput = wx.TextCtrl(panel, -1, size=(240,20), pos=(20,50))

        wx.StaticText(panel, -1, "New base name", (22, 90)) # Txt ctrl 2
        self.newnameinput = wx.TextCtrl(panel, -1, size=(240,20), pos=(20,110))
        
        wx.StaticText(panel, -1, "Please enter the file extension", (22, 150)) # Txt ctrl 3
        self.extensioninput = wx.TextCtrl(panel, -1, size=(240,20), pos=(20,170))

        statusBar = self.CreateStatusBar()
        menuBar = wx.MenuBar()
        menu1 = wx.Menu()
        menuBar.Append(menu1, "&File")
        self.SetMenuBar(menuBar)
        
        self.Show()
           
        
    def OnCloseMe(self, event):
        self.Close(True)
        
    def OnCloseWindow(self, event):
        self.Destroy()

    def OnDoIt(self, event):
        self.OnClick # here is where i think the missing code goes

app = wx.App()
frame = renamerMacro(parent=None, id=-1)
frame.Show()
app.MainLoop()


    targetfolder = # needs to be Txt Ctrl 1
os.chdir(targetfolder)



    newname=box = # needs to be Txt ctrl 2
newname = pjoin(targetfolder, newname)



    rxin = # needs to be Txt ctrl 3
allowed_name = re.compile(rxin).match


a = 0

for fname in os.listdir(targetfolder):
    if allowed_name(fname):
        a += 1
        c = os.path.splitext(fname)
        b = (newname + str(a) + c[1])
        os.rename(fname, b)

Recommended Answers

All 3 Replies

Ive gotten this far now, although none of my buttons work and my programme doesnt do anything at all, although im sure its a step in the right direction, can anyone help?

#!/usr/local/bin/python

# Authour - Rhys
# Version - 1.00

import re, os, wx
from os.path import join as pjoin, isdir


class renamerMacro(wx.Frame):
    
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Renaming Macro', size=(300, 350))
        panel = wx.Panel(self)

        exitbotton = wx.Button(panel, label="Close", pos=(210, 230),size=(50, 30))
        self.Bind(wx.EVT_BUTTON, self.OnCloseMe, exitbotton)

        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

        doit = wx.Button(panel, label="Do it", pos=(20, 230),size=(50, 30))
        self.Bind(wx.EVT_BUTTON, self.OnDoIt)

        wx.StaticText(panel, -1, "Folder Location", (22, 20)) # Txt ctrl 1
        self.folderinput = wx.TextCtrl(panel, -1, size=(240,20), pos=(20,50))

        wx.StaticText(panel, -1, "New base name", (22, 90)) # Txt ctrl 2
        self.newnameinput = wx.TextCtrl(panel, -1, size=(240,20), pos=(20,110))
        
        wx.StaticText(panel, -1, "Please enter the file extension", (22, 150)) # Txt ctrl 3
        self.extensioninput = wx.TextCtrl(panel, -1, size=(240,20), pos=(20,170))

        statusBar = self.CreateStatusBar()
        menuBar = wx.MenuBar()
        menu1 = wx.Menu()
        menuBar.Append(menu1, "&File")
        self.SetMenuBar(menuBar)
        
        self.Show()
           
        
    def OnCloseMe(self, event):
        self.Close(True)
        
    def OnCloseWindow(self, event):
        self.Destroy()

    def OnDoIt(self, event):
        self.targetfolder = self.folderinput.GetValue()
        self.newname = self.newnameinput.GetValue()
        self.rxin = self.extensioninput.GetValue()

app = wx.App(redirect = False)
frame = renamerMacro(parent=None, id=-1)
frame.Show()
app.MainLoop()


os.chdir(targetfolder)

newname = pjoin(targetfolder, newname)

allowed_name = re.compile(rxin).match

a = 0

for fname in os.listdir(targetfolder):
    if allowed_name(fname):
        a += 1
        c = os.path.splitext(fname)
        b = (newname + str(a) + c[1])
        os.rename(fname, b)

This is as far as i got, please only test this programme on a "trial file" and enter the extension as .*jpg for example, or .*doc and only test it on copy files as to not dmage your own (the programme overwrites all names of that extension in a file and appends them with numbers)

#!/usr/local/bin/python

# Authour - Rhys
# Version - 1.00

import re, os, wx
from os.path import join as pjoin, isdir


class renamerMacro(wx.Frame):
    
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Renaming Macro', size=(300, 350))
        panel = wx.Panel(self)

        exitbutton = wx.Button(panel, label="Close", pos=(210, 230),size=(50, 30))
        self.Bind(wx.EVT_BUTTON, self.oncloseme, exitbutton)

        self.Bind(wx.EVT_CLOSE, self.onclosewindow)

        doit = wx.Button(panel, label="Do it", pos=(20, 230),size=(50, 30))
        self.Bind(wx.EVT_BUTTON, self.ondoit, doit)

        wx.StaticText(panel, -1, "Folder Location", (22, 20)) # Txt ctrl 1
        self.folderinput = wx.TextCtrl(panel, -1, size=(240,20), pos=(20,50))

        wx.StaticText(panel, -1, "New base name", (22, 90)) # Txt ctrl 2
        self.newnameinput = wx.TextCtrl(panel, -1, size=(240,20), pos=(20,110))
        
        wx.StaticText(panel, -1, "Please enter the file extension", (22, 150)) # Txt ctrl 3
        self.extensioninput = wx.TextCtrl(panel, -1, size=(240,20), pos=(20,170))

        statusBar = self.CreateStatusBar()
        menuBar = wx.MenuBar()
        menu1 = wx.Menu()
        menuBar.Append(menu1, "&File")
        self.SetMenuBar(menuBar)
        
        self.Show()           
        
    def oncloseme(self, event):
        self.Close(True)
        
    def onclosewindow(self, event):
        self.Destroy()

    def ondoit(self, event):
        targetfolder = self.folderinput.GetValue()
        newname = self.newnameinput.GetValue()
        rxin = self.extensioninput.GetValue()


app = wx.App(redirect = False)
frame = renamerMacro(parent=None, id=-1)
frame.Show()
app.MainLoop()

os.chdir(targetfolder)
newname = pjoin(targetfolder, newname)
allowed_name = re.compile(rxin).match

a = 0

for fname in os.listdir(targetfolder):
    if allowed_name(fname):
        a += 1
        c = os.path.splitext(fname)
        b = (newname + str(a) + c[1])
        os.rename(fname, b)

Look through this modified code ...

#!/usr/local/bin/python

# Authour - Rhys
# Version - 1.00

import re, os, wx

class RenamerMacro(wx.Frame):
    
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Renaming Macro', 
            size=(300, 350))
        panel = wx.Panel(self)

        self.exitbotton = wx.Button(panel, label="Close", 
            pos=(210, 230),size=(50, 30))
        self.exitbotton.Bind(wx.EVT_BUTTON, self.onClose)

        self.doit = wx.Button(panel, label="Do it", 
            pos=(20, 230),size=(50, 30))
        self.doit.Bind(wx.EVT_BUTTON, self.onDoIt)

        wx.StaticText(panel, -1, "Folder Location", (22, 20))
        self.folderinput = wx.TextCtrl(panel, -1, size=(240,20), 
            pos=(20,50))

        wx.StaticText(panel, -1, "New base name", (22, 90))
        self.newnameinput = wx.TextCtrl(panel, -1, size=(240,20), 
            pos=(20,110))
        
        wx.StaticText(panel, -1, "Please enter the file extension", 
            (22, 150))
        self.extensioninput = wx.TextCtrl(panel, -1, size=(240,20), 
            pos=(20,170))

        statusBar = self.CreateStatusBar()
        menuBar = wx.MenuBar()
        menu1 = wx.Menu()
        menuBar.Append(menu1, "&File")
        self.SetMenuBar(menuBar)
        
        self.Show()
           
    def onClose(self, event):
        self.Close(True)
        
    def onDoIt(self, event):
        self.targetfolder = self.folderinput.GetValue()
        self.newname = self.newnameinput.GetValue()
        self.rxin = self.extensioninput.GetValue()
        # testing ...
        sf = "%s/%s.%s" 
        s = sf % (self.targetfolder, self.newname, self.rxin) 
        self.SetTitle(s)
        # put your renaming code here


app = wx.App(redirect = False)
frame = RenamerMacro(parent=None, id=-1)
frame.Show()
app.MainLoop()

By convention only class names are capitalized.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.