Is there a script for duplicate text file?

Discussion in 'Programming' started by Sxperm, May 29, 2007.

  1. #1
    I have text file and I want to save it in multiple file names with same content. Like "save as" and type different file names. However, do one by one is consume too much time and I need script that can save my text file with my prefered keywords as filename. Is there script that can do this job?
     
    Sxperm, May 29, 2007 IP
  2. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In PHP, put myfile.txt in a directory and upload a script with the following code in that same directory:

    <?php
    
    $content = file_get_contents('myfile.txt');
    
    $filenames = array('thisfile.txt', 'thatfile.txt', 'anothername.txt');
    
    foreach ($filenames as $var){
    	$fl=@fopen($var,'w+');
    	fwrite($fl,$content);
    	fclose($fl);
    }
    
    ?>
    Code (markup):
    Depending on your server settings you might need to change the permissions/CHMOD the directory accordingly.
     
    norfstar, May 29, 2007 IP
  3. Sxperm

    Sxperm Notable Member

    Messages:
    4,386
    Likes Received:
    142
    Best Answers:
    0
    Trophy Points:
    225
    #3
    Thank you very much for your help but I have 10,000 of keywords to be filename. I just want script to get content from text file that prepared for filename and place them as name for content file. Like this one
    
    CONTENT.TXT --> SAVE AS --> NAME1.TXT<----- GET CONTENT FROM FILENAMES.TXT
                                NAME2.TXT
                                NAME3.TXT
                                NAME4.TXT
    
    Code (markup):
    Is it possible to do that?
     
    Sxperm, May 29, 2007 IP
  4. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Replace the filenames array line with:

    $filenames = explode("\n", file_get_contents('filenames.txt'));
    Code (markup):
    This would be for a filenames.txt with the desired names being separated by a new line, if they were separated by something else, simply replace \n with the separator.
     
    norfstar, May 29, 2007 IP
    Sxperm likes this.
  5. Sxperm

    Sxperm Notable Member

    Messages:
    4,386
    Likes Received:
    142
    Best Answers:
    0
    Trophy Points:
    225
    #5
    Thanks for your help again. Green rep added.
     
    Sxperm, May 29, 2007 IP
  6. Sxperm

    Sxperm Notable Member

    Messages:
    4,386
    Likes Received:
    142
    Best Answers:
    0
    Trophy Points:
    225
    #6
    Sorry to ask you again, how can I replace space in generated filename with "-"

    file-name.txt instead of file name.txt

    cheers
     
    Sxperm, May 29, 2007 IP
  7. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No problem :):

    <?php
    
    $content = file_get_contents('myfile.txt');
    
    $filenames = explode("\n", file_get_contents('filenames.txt'));
    
    foreach ($filenames as $var){
    	$fl=@fopen(str_replace(' ','-',$var),'w+');
    	fwrite($fl,$content);
    	fclose($fl);
    }
    
    ?>
    Code (markup):
    str_replace(' ','-',$var) is the part that replaces a space with a hyphen.
     
    norfstar, May 29, 2007 IP
  8. Sxperm

    Sxperm Notable Member

    Messages:
    4,386
    Likes Received:
    142
    Best Answers:
    0
    Trophy Points:
    225
    #8
    Thanks again but I have problem when using them

    - Error message appeared
    
    
    Warning: fwrite(): supplied argument is not a valid stream resource in c:\appserv\www\duplicator\duplicator-v2.php on line 9
    
    Warning: fclose(): supplied argument is not a valid stream resource in c:\appserv\www\duplicator\duplicator-v2.php on line 10
    
    Code (markup):
    Here is the total code

    
    <?php
    
    $content = file_get_contents('real-estate-stem.txt');
    
    $filenames = explode("\n", file_get_contents('real-estate-subdomain.txt'));
    
    foreach ($filenames as $var){
    	$fl=@fopen(str_replace(' ','-',$var),'w+');
    	fwrite($fl,$content);
    	fclose($fl);
    }
    
    ?>
    
    Code (markup):
    in file "real-estate-subdomain.txt" each keyword has been separated by enter like this

    
    colorado-real-estate-license.txt
    oklahoma-real-estate-license.txt
    
    Code (markup):

    any suggestion? Thank you very much. :D
     
    Sxperm, May 29, 2007 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    
    <?php
    error_reporting( E_ALL );
    
    $content = file_get_contents( 'real-estate-stem.txt' );
    
    $filenames = split( "\n", file_get_contents('real-estate-subdomain.txt') );
    
    foreach ( $filenames as $keyword )
    {
    	if( !( $handle = fopen( str_replace( ' ', '-', $keyword ), 'w+') ) )
    	{
    		die( sprintf( 'Cannot open %s for writing', $keyword ) );
    	}
    	elseif( !fwrite( $handle, $content ) )
    	{
    		die( sprintf( 'Cannot write to %s', $keyword ) );
    	}
    	elseif( !fclose( $handle ) )
    	{
    		die( sprintf( 'Cannot close file handle %s', $keyword ) );
    	}
    }
    ?>
    
    PHP:
    Some more about where it goes wrong would be good
     
    krakjoe, May 29, 2007 IP
    Sxperm likes this.
  10. Sxperm

    Sxperm Notable Member

    Messages:
    4,386
    Likes Received:
    142
    Best Answers:
    0
    Trophy Points:
    225
    #10
    Thank you Krakjoe but some error still occured

    
    Warning: fopen(colorado-real-estate-license.txt ): failed to open stream: Invalid argument in c:\appserv\www\duplicator\duplicator-v2.php on line 10
    Cannot open colorado-real-estate-license.txt for writing
    
    Code (markup):
    However, green rep added :D Any suggestion?

    cheers
     
    Sxperm, May 29, 2007 IP
  11. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #11
    
    <?php
    error_reporting( E_ALL );
    
    $content = file_get_contents( 'real-estate-stem.txt' );
    
    $filenames = split( "\n", file_get_contents('real-estate-subdomain.txt') );
    
    foreach ( $filenames as $keyword )
    {
        if( !( $handle = fopen( trim( str_replace( ' ', '-', $keyword ) ), 'w+') ) )
        {
            die( sprintf( 'Cannot open %s for writing', $keyword ) );
        }
        elseif( !fwrite( $handle, $content ) )
        {
            die( sprintf( 'Cannot write to %s', $keyword ) );
        }
        elseif( !fclose( $handle ) )
        {
            die( sprintf( 'Cannot close file handle %s', $keyword ) );
        }
    }
    ?>
    
    PHP:
     
    krakjoe, May 30, 2007 IP
  12. raredev

    raredev Peon

    Messages:
    49
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    you might need to add write permission (chmod to 777) to your directory from ftp client, file manager, or control panel.
     
    raredev, Jun 2, 2007 IP