problem with spaces in directory name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ezechiel
    New Member
    • Jul 2009
    • 53

    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:
    Code:
    #!/usr/local/bin/perl
    #
    use warnings;
    
    $homedir = "C:/SCRIPT/CJ0249A.1/\"Project Information\"";
    $file = "$homedir/exist.txt";
    
    if (-e "$file")
    {
            print ("it exists");
    } 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:
    Code:
    $hide = "attrib -h $file";
    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

    Code:
    if (-e "c:/blabala/Project\ Information/exist.txt") {
          print("yes");
    }
    ?
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    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.
    Code:
    #!/usr/local/bin/perl
    
    [B]use strict;[/B]
    use warnings;
    
    [B]my[/B] $homedir = 'C:/SCRIPT/CJ0249A.1/Project Information';
    [B]my[/B] $file = "$homedir/exist.txt";
     
    if (-e $file)
    {
        print 'it exists';
    }
    else {
        print 'boooo';
    }

    Comment

    • ezechiel
      New Member
      • Jul 2009
      • 53

      #3
      Well,

      Code:
      #!/usr/local/bin/perl
      #
      use strict;
      use warnings;
      my $homedir = "c:/SCRIPT";
      opendir (IMD, $homedir) or die "Couldn't find dir: IMD ($!)";
      my @thefiles= readdir(IMD);
      closedir(IMD);
      
      my $x=0;
      #create a list of ".1" directories
      foreach my $f (@thefiles){    
           if($f =~ /(.+)(\.1$)/i && $f !~ /^\./i){
                   my $h_dir = '$homedir/$f/Analyst-$1/Projects/$1/Project Information';
                   my $file = "$h_dir/Project.atd";
                   # check if dir "analyst-studyID"  exists
                   # $1 refers to first parameter of /(.+)(\.1$)/i -> (.+)
                   if (-d $h_dir && -e $file){
                           my @res= qx{attrib $h_dir/Project.atd};
                           my @dir_1;
                           
                           if ($res[0] !~ /A\s{3}(H)\s{1,}.+(C\:\\.+)/){
                                   $dir_1[$x] = $f;
                                   $x++;
                                   print ("test x \n $x \n");
                                   my $h_file = "attrib +h $h_dir/Project.atd";
                                   system($h_file);
                                   my $h_xcacls = "xcacls $h_dir/Project.atd /E /P DOMAIN\\User:A";
                                   system($h_xcacls);
                           } else {print (" $file is allready hidden\n");}
                   } else { print(" directory $h_dir or file Project.atd does not exist \n");}
           }
      }
      print("\n******************************************************************");
      print("\n*                            FINISHED                            *");
      print("\n******************************************************************\n\n\a");
      
      #list ".1" directories that have been impacted.
      foreach my $d (my @dir_1)
      {
              print ("Processed Folders:\n");
              print ($d."\n");
      }
      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

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        In the below line from your code:
        Code:
        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.

        Comment

        • ezechiel
          New Member
          • Jul 2009
          • 53

          #5
          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?

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            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

            or
            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).

            Comment

            • ezechiel
              New Member
              • Jul 2009
              • 53

              #7
              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).

              Comment

              Working...