Editing a large amount of files at once

  • Tabble
  • Born
  • Born
  • Tabble
  • Posts: 4

Post 3+ Months Ago

I have about 20,000 HTML files on my computer, but they just have raw text in them, no HTML formatting.

So my question is:

Is there a way to add the <html> tag's etc. to the beginning and end of each file? in an automated way?

Cause, you can imagine, opening each of the 20,000 files individually and adding the code, might take a while. :)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SecureITGroup
  • Proficient
  • Proficient
  • User avatar
  • Posts: 293

Post 3+ Months Ago

Awhile back i had wanted to rename all my family photos 1, 2, 3 and so on i talked to a x programmer in the military and he said that it would you would have to write a program to do this. So it might apply the same to this case. Might want to google rename all you files with "rename your files with the desired extension".
If you do come across a program to do this let us know.
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

You could do it with PHP or VB, it the only thing is crawling the directories, but I think you could either find a subroutine/function to do that or write your own.

Best bet would be a recursive function (is that the right word - a function that can call itself?) Although I don't know if either language can do this - I only know about it from programming theory I did two years ago...

Code: [ Select ]
function crawler($startpath){
  //detect all files in the directory $startpath
  //save to $FileArray
  foreach($FileArray as $TheFile){
    if(/*is html extension*/){
      //open file add code above and below
      //Save file
    }elseif(/*is directory*/){
       crawler($StartPath.$DirName);
    }
    //ignore any other file type
  }
}
  1. function crawler($startpath){
  2.   //detect all files in the directory $startpath
  3.   //save to $FileArray
  4.   foreach($FileArray as $TheFile){
  5.     if(/*is html extension*/){
  6.       //open file add code above and below
  7.       //Save file
  8.     }elseif(/*is directory*/){
  9.        crawler($StartPath.$DirName);
  10.     }
  11.     //ignore any other file type
  12.   }
  13. }


That should roughly do it. Just initiate crawler with the corect root directory and you're off :wink: of course it would probably help to edit that code a little.

I'm sure we have some scripting guru's here that could flesh out my pseudocode in about 5 minutes...
  • whos_wee_dug
  • Graduate
  • Graduate
  • User avatar
  • Posts: 121
  • Loc: Scotland

Post 3+ Months Ago

rtm223's pseudo looks good if you you want to write it in a scripting language. If that doesn't matter what language its written in, (I think a OO language would be best) I'll hack together a Delphi program that'll do it if you want. (It'd take < 10 mins for me)

whos_wee_dug?
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

told ya someone could do it easily. :)

It'd probably take me 30 mins to an hour cos I'd have to look up all the commands for messing with diectories and text files, which I rarely do.

Nice one dug/doug ( ? )
  • IH8Purple
  • Guru
  • Guru
  • User avatar
  • Posts: 1215
  • Loc: Somewhere on Google Earth

Post 3+ Months Ago

uhh IU think he wants to add
Code: [ Select ]
<HTML>
to the beginning and
Code: [ Select ]
</HTML>
to the end of all his files, not rename them...
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

rtm223 wrote:
Code: [ Select ]
//open file add code above and below



:wink:
  • whos_wee_dug
  • Graduate
  • Graduate
  • User avatar
  • Posts: 121
  • Loc: Scotland

Post 3+ Months Ago

rtm223 wrote:
Nice one dug/doug ( ? )


Ehh Dug is the normal shortenning ppl use! I'm going to write this prog starting from....NOW!

see how long it takes...(probably ages, lol)

whos_wee_dug?
  • Smokenjoe
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1573
  • Loc: Anchorage, AK

Post 3+ Months Ago

Nice. I dont write programs that much. Never have much use to do it. There are so many other people that do it, I can always seem to find what I need on the net. ;) (I know, Im lazy)
  • whos_wee_dug
  • Graduate
  • Graduate
  • User avatar
  • Posts: 121
  • Loc: Scotland

Post 3+ Months Ago

Hey, here we go...

Took a bit longer than expected because i didn't have Delphi installed and it took me 20mins trying to find the CD. Anyway, you can get it here:

http://cgi.maddug01.plus.com/multiplefileinserter.zip [234kb]

Its not got much error checking in it but it works fine on my computer.

To use, run it, select the directory were the 'RAW' files are.
Enter the text you want to put before the files contents.
Enter text you want to put at the end.
Click 'Go'. It'll save the updated files to the output directory under the same filenames.

NOTES:
-It doesn't work with subdirectories, let me know if you need it to and i can adapt the program.
-Hardly any error checking, so make sure the output folder exists and don't move the exe out its folder!


Enough of my ramblings...let me no if you have ne problems.

whos_wee_dug?
  • Smokenjoe
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1573
  • Loc: Anchorage, AK

Post 3+ Months Ago

Wow, way to go Weedug. You make it look too easy! :)
  • UNFLUX
  • Genius
  • Genius
  • User avatar
  • Posts: 6376
  • Loc: twitter.com/unflux

Post 3+ Months Ago

Dreamweaver has a mass find&replace you could use. As long as the files are able to be opened in dreamweaver at all that is. It works great, and easy to implement on an entire site, if you're interested.
  • Daemonguy
  • Moderator
  • Web Master
  • User avatar
  • Posts: 2700
  • Loc: Somewhere outside the box in Sarasota, FL.

Post 3+ Months Ago

234k?? :)

Here's some perl hack.

Code: [ Select ]
#!/usr/local/bin/perl

$dir = "<path/to/files>";
$prepend = "<HTML>";
$append = "</HTML>";
##############################
@dodir = `ls $dir`;
foreach $file(@dodir) {
 chomp $file;
  open (FILE, "$dir/$file");
   @lines = <FILE>;
  close (FILE);
  open (FILE, ">$dir/$file");
   print FILE $prepend;
   print FILE @lines;
   print FILE $append;
   print "@lines";
  close (FILE);
}
  1. #!/usr/local/bin/perl
  2. $dir = "<path/to/files>";
  3. $prepend = "<HTML>";
  4. $append = "</HTML>";
  5. ##############################
  6. @dodir = `ls $dir`;
  7. foreach $file(@dodir) {
  8.  chomp $file;
  9.   open (FILE, "$dir/$file");
  10.    @lines = <FILE>;
  11.   close (FILE);
  12.   open (FILE, ">$dir/$file");
  13.    print FILE $prepend;
  14.    print FILE @lines;
  15.    print FILE $append;
  16.    print "@lines";
  17.   close (FILE);
  18. }

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: spork and 44 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.