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

problem with spaces in directory name

P: 53
Hi everyone,

I have a question about spaces in directory names.
If I want to use mkdir something, everything works out, but in this case:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. #
  3. use warnings;
  4.  
  5. $homedir = "C:/SCRIPT/CJ0249A.1/\"Project Information\"";
  6. $file = "$homedir/exist.txt";
  7.  
  8. if (-e "$file")
  9. {
  10.         print ("it exists");
  11. } else {print ("boooo");}
I got "booooooo" ..

I tried -e "$file" and -e $file, it doesn't changes anything (what is best practice here?)
I know it has to do with the spaces. But then, this works:
Expand|Select|Wrap|Line Numbers
  1. $hide = "attrib -h $file";
  2. system($hide);
I know that this works because in DOS, directories with spaces are put between quotes. And since -e is a perl function and not DOS, I guess Perl doesn't handle directories with spaces, with quotes.

So how could I solve this problem??

Or am i obliged to type

Expand|Select|Wrap|Line Numbers
  1. if (-e "c:/blabala/Project\ Information/exist.txt") {
  2.       print("yes");
  3. }
?
Nov 5 '09 #1
Share this Question
Share on Google+
6 Replies


Expert Mod 100+
P: 589
Perl knows how to deal with the spaces, so there's no need to do any escaping.

If you plan on using this var in a system call, then we can look at the escaping issue, but based on what you're needing, there's no reason to use a system call.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $homedir = 'C:/SCRIPT/CJ0249A.1/Project Information';
  7. my $file = "$homedir/exist.txt";
  8.  
  9. if (-e $file)
  10. {
  11.     print 'it exists';
  12. }
  13. else {
  14.     print 'boooo';
  15. }
Nov 5 '09 #2

P: 53
Well,

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. #
  3. use strict;
  4. use warnings;
  5. my $homedir = "c:/SCRIPT";
  6. opendir (IMD, $homedir) or die "Couldn't find dir: IMD ($!)";
  7. my @thefiles= readdir(IMD);
  8. closedir(IMD);
  9.  
  10. my $x=0;
  11. #create a list of ".1" directories
  12. foreach my $f (@thefiles){    
  13.      if($f =~ /(.+)(\.1$)/i && $f !~ /^\./i){
  14.              my $h_dir = '$homedir/$f/Analyst-$1/Projects/$1/Project Information';
  15.              my $file = "$h_dir/Project.atd";
  16.              # check if dir "analyst-studyID"  exists
  17.              # $1 refers to first parameter of /(.+)(\.1$)/i -> (.+)
  18.              if (-d $h_dir && -e $file){
  19.                      my @res= qx{attrib $h_dir/Project.atd};
  20.                      my @dir_1;
  21.  
  22.                      if ($res[0] !~ /A\s{3}(H)\s{1,}.+(C\:\\.+)/){
  23.                              $dir_1[$x] = $f;
  24.                              $x++;
  25.                              print ("test x \n $x \n");
  26.                              my $h_file = "attrib +h $h_dir/Project.atd";
  27.                              system($h_file);
  28.                              my $h_xcacls = "xcacls $h_dir/Project.atd /E /P DOMAIN\\User:A";
  29.                              system($h_xcacls);
  30.                      } else {print (" $file is allready hidden\n");}
  31.              } else { print(" directory $h_dir or file Project.atd does not exist \n");}
  32.      }
  33. }
  34. print("\n******************************************************************");
  35. print("\n*                            FINISHED                            *");
  36. print("\n******************************************************************\n\n\a");
  37.  
  38. #list ".1" directories that have been impacted.
  39. foreach my $d (my @dir_1)
  40. {
  41.         print ("Processed Folders:\n");
  42.         print ($d."\n");
  43. }
no errors, but now, he isn't taking in account the $1 (from ($f =~ /(.+)(\.1$)/i && $f !~ /^\./i) )
anymore.. This is quite annoying :s

Thanks for any help
Nov 6 '09 #3

nithinpes
Expert 100+
P: 410
In the below line from your code:
Expand|Select|Wrap|Line Numbers
  1. my $h_dir = '$homedir/$f/Analyst-$1/Projects/$1/Project Information';
  2.  
use double quotes instead of single quotes. There will be no substitution of variables inside single quotes. Single quotes are used for literal strings.
Nov 6 '09 #4

P: 53
Ok,

using double quotes for this one solves the $1 problem,
but now I have the problem again with DOS not accepting the parameters.

For DOS, I have no other choice than using \"Project Information\"
or have I?
Nov 6 '09 #5

Expert Mod 100+
P: 589
You need to learn about Perl's "Quote and Quote-Like Operators".

http://perldoc.perl.org/perlop.html#...like-Operators

You also need to learn about the different ways of passing args to the system function, i.e. in this case you want to pass a list.

http://perldoc.perl.org/functions/system.html

And, you should search cpan for modules that can aide in accomplishing your task.

For example:
MSDOS::Attrib - Get or set MS-DOS file attributes
http://search.cpan.org/~cjm/MSDOS-At...SDOS/Attrib.pm
or
Win32::File - Manage file attributes in perl
http://search.cpan.org/~jdb/Win32-File-0.06/File.pm

A more advanced option would be to use the Win32::API module and make direct calls to the underlying C functions in the proper dll file(s).
Nov 7 '09 #6

P: 53
Hi,

thanks for the hints Ron!

Is there any advantage to using perl modules rather than system calls?
(-> does using modules asks more memory or something in order to load the module? Sorry 'bout the question, but I don't know very well how Perl interacts with the system and hardware).
Nov 9 '09 #7

Post your reply

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