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

Checking to see if file exsists

P: 67
I've looked around a bit already and from what I'm seeing, I'm doing it right but for whatever reason I'm not getting a true response even though the file does exist.

Expand|Select|Wrap|Line Numbers
  1. print "Enter file name: ";
  2. my $fileName = <>;
  3. my $editedFileName = '~/'.$fileName;
  4. print "File set to: $editedFileName";
  5. chomp($editedFileName);
  6. if(-e $editedFileName){
  7.     print "File exists! Would you like to load the file? \n";
  8. }
  9. else{
  10.     print "File does not exist.\n";
  11. }
  12.  
Jan 30 '09 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
You need to use the full path to the file or be in the directory where the file is stored. The '~' symbol has no meaning in perl in that context.
Jan 30 '09 #2

P: 67
Hey thanks so much! With that information I was able to figure out a solution.

Expand|Select|Wrap|Line Numbers
  1. print "Enter file name: ";
  2. my $editedFileName;
  3. my $currentWorkingDirectory;
  4. my $fileName = <>;
  5. &setFileName($fileName);
  6. &checkFileExistence($editedFileName);
  7.  
  8. sub setFileName
  9. {
  10.     $currentWorkingDirectory = getcwd;
  11.     $editedFileName = $currentWorkingDirectory."/".$fileName;
  12.     print "File set to: $editedFileName";
  13.     chomp($editedFileName);
  14. }
  15.  
  16. sub checkFileExistence
  17. {
  18.     if(-e $editedFileName)
  19.     {
  20.         print "File exists! Would you like to load the file? \n";
  21.     }
  22.     else
  23.     {
  24.         print "File does not exist.\n";
  25.     }
  26. }
  27.  
  28.  
Jan 31 '09 #3

KevinADC
Expert 2.5K+
P: 4,059
Very good :)
Jan 31 '09 #4

Post your reply

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