By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,176 Members | 984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,176 IT Pros & Developers. It's quick & easy.

How to open and modify files in a subdirectory?

P: 6
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:
Expand|Select|Wrap|Line Numbers
  1. for($i=0; $i<10; $i++){
  2.    opendir(MYDIR, $path[$i]); #where the paths are in the form "/dir1/dir2/"
  3.    $the_infile = $filename_list[$i]; #where the filename_lists are in the form "filename.str"
  4.    $the_outfile = 'fixed_'.$filename_list[$i];
  5.    #write corrections to output fIle...
  6.    closedir(MYDIR);
  7. }
  8.  
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!
Aug 9 '10 #1

✓ answered by Oralloy

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

Expand|Select|Wrap|Line Numbers
  1. for($i=0; $i<10; $i++){ 
  2.    #opendir(MYDIR, $path[$i]); #where the paths are in the form "/dir1/dir2/" 
  3.    $the_infile = $path[$i].$filename_list[$i]; #where the filename_lists are in the form "filename.str" 
  4.    $the_outfile = $path[$i].'fixed_'.$filename_list[$i]; 
  5.    #write corrections to output fIle... 
  6.    #closedir(MYDIR); 
  7. }

Share this Question
Share on Google+
4 Replies


Oralloy
Expert 100+
P: 983
I'd suggest that you prefix the filenames with the appropriate path, and don't waste time with opendir.

Expand|Select|Wrap|Line Numbers
  1. for($i=0; $i<10; $i++){ 
  2.    #opendir(MYDIR, $path[$i]); #where the paths are in the form "/dir1/dir2/" 
  3.    $the_infile = $path[$i].$filename_list[$i]; #where the filename_lists are in the form "filename.str" 
  4.    $the_outfile = $path[$i].'fixed_'.$filename_list[$i]; 
  5.    #write corrections to output fIle... 
  6.    #closedir(MYDIR); 
  7. }
Aug 9 '10 #2

Expert
P: 70
I suggest you show us your missing code: how you open your input and output files, and how you print to your output files.
Aug 10 '10 #3

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

Oralloy
Expert 100+
P: 983
You're welcome.
Aug 10 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.