Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<?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);
}
?>
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.
find . -name *.html -exec cp /dev/null {} \;
how so? It's a virtual copy-paste from the PHP manual. Saying "that's wrong" without a correction really doesn't benefit anybody.ekliptikz said:I hate to rain on your parade, but that code is pretty screwed up. Use with caution
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.
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()ekliptikz said:...setting yourself up for some infinite recursion I believe...
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()
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.
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.
dir /B /S *.HTML > list.txt
for /F %1 in (list.txt) do copy /Y blank.txt %1
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:
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.
Shadow2531 said:In copy /Y blank.txt %1, will %1 be considered in quotes? (for spaced out paths etc.)
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. ???
mikeblas said:I can see how it might loop forever, but where's the recursion?
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.
Shadow2531 said:I like to put arguments that might contain spaces etc. in quotes ( just in case). e.g.