How to open and modify files in a subdirectory?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zeebo17
    New Member
    • Jun 2010
    • 6

    How to open and modify files in a subdirectory?

    Hi,

    I have a list of file names and path names and I want to cycle through each file and modify it. I have tried something like:
    Code:
    for($i=0; $i<10; $i++){
       opendir(MYDIR, $path[$i]); #where the paths are in the form "/dir1/dir2/"
       $the_infile = $filename_list[$i]; #where the filename_lists are in the form "filename.str"
       $the_outfile = 'fixed_'.$filename_list[$i];
       #write corrections to output fIle...
       closedir(MYDIR);
    }
    but the modified output file is not showing up in the directory. The program works fine when writing files to the current directory, but I'm not sure how to do it for the sub directories.

    Any suggestions would be greatly appreciated!
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    I'd suggest that you prefix the filenames with the appropriate path, and don't waste time with opendir.

    Code:
    for($i=0; $i<10; $i++){ 
       #opendir(MYDIR, $path[$i]); #where the paths are in the form "/dir1/dir2/" 
       $the_infile = $path[$i].$filename_list[$i]; #where the filename_lists are in the form "filename.str" 
       $the_outfile = $path[$i].'fixed_'.$filename_list[$i]; 
       #write corrections to output fIle... 
       #closedir(MYDIR); 
    }

    Comment

    • toolic
      Recognized Expert New Member
      • Sep 2009
      • 70

      #3
      I suggest you show us your missing code: how you open your input and output files, and how you print to your output files.

      Comment

      • zeebo17
        New Member
        • Jun 2010
        • 6

        #4
        I got it to work by using the absolute path when opening the file instead of the relative path. Thanks for your help!

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          You're welcome.

          Comment

          Working...