Clear HTML file

zeldan0

n00b
Joined
Apr 13, 2005
Messages
27
is there a file that will delete everything on a html file and just leave the file name? I have 14,759 files I need blank but would like to keep the file name. anyone can help let me know and I will get the program. thanks :)
 
Open the file with "notepad" and then chose "Edit -> Select All" and hit "Backspace" or "Delete" on your keyboard. Save changes.

You could also just create a new text file and change the extension to .htm or .html -- this will do the same thing.

Html files are human readable with any standard text editor and can be modified as such.
 
no i want to clear everything off the html files without doing anything...let program do it....and just keep the file name on them.
 
Any decent scripting language (and maybe even DOS) could pull this off.

You want to:
--Read the directory listing, storing the file names
--Delete the directory contents
--"touch" the stored file names to recreate them as blank files

PHP, for example, would use something like
Code:
<?php

if ($handle = opendir('/path/to/files')) {

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) { 
       if (stristr($file, ".htm")) { // only deal with .htm / .html extensions
          unlink($file); // delete
          $fhand = fopen($file, "x"); // recreate blank
          fclose($fhand); // close
       }
    }

    closedir($handle); 
}
?>

That example would nuke *all* files in a directory, you'd want to check $file for extensions or what have you if you've got a mixed-content directory.
 
ok they are html....havce no clue how to do that...can u explain and tell me how...they will be all in 1 folder or in different ones. please let me know
 
if you were on a unix system, it'd be a simple application of the 'find' utility
 
lomn75 said:
Any decent scripting language (and maybe even DOS) could pull this off.

You want to:
--Read the directory listing, storing the file names
--Delete the directory contents
--"touch" the stored file names to recreate them as blank files

PHP, for example, would use something like
Code:
<?php

if ($handle = opendir('/path/to/files')) {

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) { 
       unlink($file); // delete
       $fhand = fopen($file, "x"); // recreate blank
       fclose($fhand); // close
    }

    closedir($handle); 
}
?>

That example would nuke *all* files in a directory, you'd want to check $file for extensions or what have you if you've got a mixed-content directory.


I hate to rain on your parade, but that code is pretty screwed up. Use with caution :)
 
yeah...might need to pay someone to delete all files since i dont have time to delete about 15k of files
 
Code:
find . -name *.html -exec cp /dev/null {} \;

will do it on a unix system (or cygwin installed under windows). You could look into doing it with http://gnuwin32.sourceforge.net/ as well, but you might find yourself having to play around with escaping/quoting bits of it (as well as copying a 0-byte file instead of /dev/null). As always, remember to Read The Fine Manual.
 
ekliptikz said:
I hate to rain on your parade, but that code is pretty screwed up. Use with caution :)
how so? It's a virtual copy-paste from the PHP manual. Saying "that's wrong" without a correction really doesn't benefit anybody.

//edit: that said, it is constructed off the top of my head and should be first tested in a safe environment, etc, yadda, yadda, yadda.
 
lomn75 said:
how so? It's a virtual copy-paste from the PHP manual. Saying "that's wrong" without a correction really doesn't benefit anybody.

//edit: that said, it is constructed off the top of my head and should be first tested in a safe environment, etc, yadda, yadda, yadda.

Yea I just didnt want to dive into it's problems, you know, teach a man to fish or some crap like that. But you're setting yourself up for some infinite recursion I believe, because you're recreating the files your deleting, and you're gonna get the "." and ".." entries also for starters.
 
ekliptikz said:
...setting yourself up for some infinite recursion I believe...
Good point. Should have stuck to my list pseudocode of store; delete; create to be safe. As for . and .., they should be covered by the lack of a valid file handler coming back from readdir()
 
lomn75 said:
Good point. Should have stuck to my list pseudocode of store; delete; create to be safe. As for . and .., they should be covered by the lack of a valid file handler coming back from readdir()

Oh contrare mon frair, . and .. do return valid file handlers....
 
My ultimate reccomendation though? Skip town. Assuming a new identity is probably easier than taking responsibility.
 
PHP example above is happy now. amoeba's is certainly the cleaner solution if it's practical, but the PHP / etc is nice to know when you don't have shell access to a web server.

Coming back to that, since it doesn't look like the OP's got a real answer yet -- what is your server setup? Specifically:
Do you have shell/command-line access to the server?
What scripting do you have available for web pages (PHP, Perl, ASP, etc)?
 
If on a win box and the command line is available, you could probably do it with dos.

Here's how you'd get a list off all the files. (assuming none of the paths are too long)

dir /B /S | find ".html" > list.bat

You could then create blank.txt and prepend copy /Y blank.txt " and append " to each line in the batch file and then just run the batch file.

Depending on the text editor, the prepending and appending could be done quickly.

You could probably automate the prepending and appending when generating the file, but I don't use batch files enough to remember how you'd do that.

That should give some ideas to improve upon for a dos method.
 
ekliptikz said:
Yea I just didnt want to dive into it's problems, you know, teach a man to fish or some crap like that. But you're setting yourself up for some infinite recursion I believe, because you're recreating the files your deleting, and you're gonna get the "." and ".." entries also for starters.

I can see how it might loop forever, but where's the recursion?
 
Shadow2531 said:
If on a win box and the command line is available, you could probably do it with dos.
[...]
You could probably automate the prepending and appending when generating the file, but I don't use batch files enough to remember how you'd do that.

That should give some ideas to improve upon for a dos method.

You're not recommending DOS; it's the Windows command prompt. That's important to know because CMD.EXE has got a bunch of neat features that the DOS Command shell doesn't have. FOR /F, for example:

Code:
dir /B /S *.HTML > list.txt
for /F %1 in (list.txt) do copy /Y blank.txt %1

will do all the copying in one swell foop.

I'm not sure that will work for the original poster; he said he wanted to "just leave the file name". If he menas that the file name needs to be in the page (as the title, or some text in the page) then this approach needs some modification.
 
mikeblas said:
You're not recommending DOS; it's the Windows command prompt. That's important to know because CMD.EXE has got a bunch of neat features that the DOS Command shell doesn't have. FOR /F, for example:

Correct

Code:
dir /B /S *.HTML > list.txt
for /F %1 in (list.txt) do copy /Y blank.txt %1

will do all the copying in one swell foop.

There we go. That's basically what I was referring to. Thanks

In copy /Y blank.txt %1, will %1 be considered in quotes? (for spaced out paths etc.)



I'm not sure that will work for the original poster; he said he wanted to "just leave the file name". If he menas that the file name needs to be in the page (as the title, or some text in the page) then this approach needs some modification.

True. I assumed he just meant to blank out the files and "leave the filename" meant that the files would not contain any data and would just consist of their filenames. ???
 
Shadow2531 said:
In copy /Y blank.txt %1, will %1 be considered in quotes? (for spaced out paths etc.)

Yeah, I think you'll have to put it in quotes. Though, if these files are HTML, then I wouldn't expect them to have characters that would need to be in quotes because most characters that would require quotes aren't really great for use in URLs as they'd at least have to be escaped.

Shadow2531 said:
True. I assumed he just meant to blank out the files and "leave the filename" meant that the files would not contain any data and would just consist of their filenames. ???

Only the OP knows for sure!
 
mikeblas said:
Yeah, I think you'll have to put it in quotes. Though, if these files are HTML, then I wouldn't expect them to have characters that would need to be in quotes because most characters that would require quotes aren't really great for use in URLs as they'd at least have to be escaped.

Thanks. That's true.

I like to put arguments that might contain spaces etc. in quotes ( just in case). e.g.

copy /Y "source" "target"
 
Shadow2531 said:
I like to put arguments that might contain spaces etc. in quotes ( just in case). e.g.

Yeah, sure. It won't hurt anything, and I guess it's a good defensive thing to do.
 
Back
Top