Need help from someone with brains

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
drslim
Posts: 5
Joined: Wed Sep 24, 2003 3:48 pm

Need help from someone with brains

Post by drslim »

Can anyone write a macro or script for me.
This is a brief explanation...

STEP 1:
======
I create new web pages and put them in Folder A.
I open all the pages in this folder with TextPad.
These web pages can be .html or .htm or .shtml and the names could be random e.g.
- doc1.html
- doc2.html
- document3.html
- files.html etc.

STEP 2:
=======
I have a number of previously created web pages in Folder B and these have specific names e.g.
- herbal.html
- low-cal.html
- chrom-picol.html
- ibsherbals.html etc.

What I need the script to do is for it to copy the new pages from Folder A and place them into either Folder B where they overwrite the existing web pages or better still, places these web pages from Folder A into a new folder (Folder C) but changes the names to those of the web pages found in Folder B.

This is a challange - can anyone do this?
I am willing to pay a fee.

Thanks

Craig :?:
User avatar
BriAnn
Posts: 13
Joined: Thu Sep 18, 2003 1:58 am

Post by BriAnn »

I'm puzzled. How is the macro/script supposed to know which old name to attach to which new file? Otherwise, it sounds like you've got a typical folder synchronization problem. There are many excellent tools out there such as Total Commander that might be able to solve it for you.

HTH

--BriAnn
User avatar
talleyrand
Posts: 625
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post by talleyrand »

Well, here's a solution based on what you've explained. It will get the directory listing for two directories and match based on alphabetical match. Unless you have a mapping (maybe something inside the file1.html to indicate what it should be?) this is the best I can guess at.

D1
file1.html
file2.html
file3.html
file4.html
file5.html
file6.html

D2
annoy.html
fastFurious.html
fastFurious2.html
index.html
postinfo.html
_vti_inf.html

Step 1, Install Python. Reboot if neccesary.

Step 2, Copy the following code and save it out (use textpad or notepad) as C:\directoryFixer.py

Step 3, Change the locations in the following script to match your file structure.

Step 4, At the command prompt (either cmd or command depending on your version of Windows) C:\>python c:\directoryFixer.py
Manually adjust files based on any warnings

Code: Select all

import os
import glob
import shutil


def getFilesInDirectory(directory, pattern = "*.*"):
   """
   Get a listing for a particular directory
   """
   return glob.glob1(directory, pattern)


def mkdir(path):
   """
   Make the directory structure if needed.
   """
   #test to see if the path exists
   if (not os.path.exists(path)):
      # if not, try and make it.
      try:
         os.makedirs(path)
      except os.error, value:
         #If fail (due to permission or whatever, bomb out I guess)
         print value[0], value[1]
      except:
         print "Bad mojo - failed to make directories and such"
         return


def main():
   #these lists will hold the name of the files
   directory1 = []
   directory2 = []

   #the r mean raw string which is needed to allow the backslashes (\)
   #   to be used as is
   d1Location = r"C:\Inetpub\wwwroot\directory1"
   d2Location = r"C:\Inetpub\wwwroot\directory2"
   d3Location = r"C:\Inetpub\wwwroot\directory3"

   #if you need htm files as well, change the pattern
   directory1 = getFilesInDirectory(d1Location, "*.html")
   directory2 = getFilesInDirectory(d2Location, "*.html")

   mkdir(d3Location)

   #for each file in directory1, save it out to d3 under the name in directory2
   #this assumes the order of files in d1 and d2 match each other
   i = 0

   for f in directory1:
      try:
         print "copy " + f + " to " + directory2[i]
         shutil.copyfile(os.path.join(d1Location, f), os.path.join(d3Location, directory2[i]))
         i += 1
      except:
         print "failed to copy " + f + "\nProbably cause --- no matching file in second directory"


if __name__ == '__main__':
   main()
I choose to fight with a sack of angry cats.
drslim
Posts: 5
Joined: Wed Sep 24, 2003 3:48 pm

Post by drslim »

:shock: WOW

Hi talleyrand, thanks for that quick response.

I will try that asap and let you know the outcome.
Post Reply