469,275 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

problem with spaces in directory name

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;
  5. $homedir = "C:/SCRIPT/CJ0249A.1/\"Project Information\"";
  6. $file = "$homedir/exist.txt";
  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
6 10132
589 Expert Mod 512MB
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
  3. use strict;
  4. use warnings;
  6. my $homedir = 'C:/SCRIPT/CJ0249A.1/Project Information';
  7. my $file = "$homedir/exist.txt";
  9. if (-e $file)
  10. {
  11.     print 'it exists';
  12. }
  13. else {
  14.     print 'boooo';
  15. }
Nov 5 '09 #2

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);
  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;
  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");
  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
410 Expert 256MB
In the below line from your code:
Expand|Select|Wrap|Line Numbers
  1. my $h_dir = '$homedir/$f/Analyst-$1/Projects/$1/Project Information';
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

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
589 Expert Mod 512MB
You need to learn about Perl's "Quote and Quote-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.


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
Win32::File - Manage file attributes in perl

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

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.

Similar topics

9 posts views Thread by hokiegal99 | last post: by
11 posts views Thread by Dyl | last post: by
4 posts views Thread by Steve Jorgensen | last post: by
4 posts views Thread by skavan | last post: by
11 posts views Thread by brianlanning | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.