I have a .txt file that includes a list of about 500 file names. I need to generate the actual files based on the list. I could use either CMD locally, or PHP. Which ever is best. Each file name is on a separate line within the .txt and there is no other punctuation or syntax. Let's say, for example, the .txt file is named colors.txt: and EACH LINE inside the text f开发者_如何学Cile contains a unique file name, like this:
red.php
blue.php
green.php
orange.php
yellow.php
magenta.php
light-yellow.php
purple.php
How would I go about actually turning these names in the list into files? And, better yet, is there a way to include content into the files as they are generated? For example, if I wanted to insert <?echo "hello world";?>
automatically within each and every new .php file as it was being created, how could I? Thanks
NOTE: This is a batch-script for windows.
To create these files from your colors.txt list and add the echo line to each file, use the following FOR /F
loop:
FOR /F "tokens=*" %%A IN (colors.txt) DO (
(ECHO ^<?echo "hello world";?^>)>"%%A"
)
Notice in the ECHO
command you must escape the <
and >
characters with ^
to make them into a literal string or else the batch script will think they are redirection commands.
I'm not exactly sure what you're trying to do here. My gut tells me there may be a better way to solve the bigger picture problem you are dealing with. Nonetheless, you could try something like the following:
foreach (file('colors.txt') as $filename) {
file_put_contents($filename, '<?php echo "hello world";');
}
You would probably also want to do some sanity checking on the filenames to make sure they are safe to use.
精彩评论