Page 1 of 1

Need help from someone with brains

Posted: Wed Sep 24, 2003 3:58 pm
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 :?:

Posted: Wed Sep 24, 2003 4:50 pm
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

Posted: Wed Sep 24, 2003 6:00 pm
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()

Posted: Wed Sep 24, 2003 6:05 pm
by drslim
:shock: WOW

Hi talleyrand, thanks for that quick response.

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