NaveeN Kumar S has asked for the wisdom of the Perl Monks concerning the following question:

facing problem in escaping space in file path

$unInstallExepath= "c:\program files\netbeans 7.0\uninstall.exe"; my $path = dirname($unInstallExepath); # $path contain c:\program + files\netbeans 7.0 my $exe = basename($unInstallExepath); # $exe contain uninstall.exe chdir ('$path') or print "XXXXXXXXXXXXXXXXXXXXXXXXXXXX $!"; System ("start $exe");

When I run it give error "no such file or directory files\netbeans " For abve 2 lines

what and all i tried as bellow to escaping spaces in path

#$path = quotemeta($path); #$path =~s /\\/\//g; #$path =~s / /\\/g; #$path =~s /(\s\w*)/\~1/; #$path =~s /(\s\d*)/\~1/;

Any idea how to get out off this error. How to escape this spaces in path.

Replies are listed 'Best First'.
Re: facing problem in escaping space in file path No such file or folder
by Jenda (Abbot) on Jun 22, 2011 at 11:14 UTC

    When the script doesn't work as you think it should, it's usually helpful to print the contents of some variables to see whether they contain the data you think they do!

    $unInstallExepath= "c:\program files\netbeans 7.0\uninstall.exe"; print $unInstallExepath, "\n";

    Does this print what you thought it will? Try to find out why!

    Then try to print what you pass to the chdir() function:

    print '$path', "\n";

    Again not what you seem to expect. Remember, the quotes matter!

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      issue in " itself, the value $unInstallExepath= "c:\program files\netbeans 7.0\uninstall.exe"; was getting from registery the registery value it self having/starting with " and then data. now i escaped " with $path =~s /\"//; now i am able to change to directory

      THANKS for ALL, helping me to solve this issue. THANKS for immediate replayes.

Re: facing problem in escaping space in file path No such file or folder
by rovf (Priest) on Jun 22, 2011 at 12:07 UTC
    First, single quotes suppress interpolation. Do

    chdir($path)
    instead. Second, there is no such function called System. I guess you mean system instead. Finally, having special characters such as spaces in the argument of system means that you have them to quote for the shell (nor for Perl - that's why quotemeta is of not much help here).

    Of course it would be best to avoid filenames with embedded spaces, but I know that this is not always our own choice. You have two possibilities: You can do your quoting manually, for example in case of Windoze: system('start "c:\\program files\\netbeans 7.0\\netbeans.exe"'). Or, and often easier, you use the list-form of system:

    system('start','c:\\program files\\netbeans 7.0\\netbeans.exe')
    (Note the double-quotes I have explicitly added to make CMD.EXE happy). This means of course that you don't have other shell features available.

    For completeness: You can also use the module IPC::Run. It is quite complex, but also very flexible.

    Update: Fixed typo when nesting single- and double-quotes (thanks, AnomalousMonk).
    -- 
    Ronald Fischer <ynnor@mm.st>
Re: facing problem in escaping space in file path No such file or folder
by Anonymous Monk on Jun 22, 2011 at 10:24 UTC
    add
    use autodie;
    and you'll get better error messages, observe
    $ perl -Mautodie -e " $asdf = 44; chdir '$asdf' " Can't chdir('$asdf'): No such file or directory at -e line 1 $ perl -Mautodie -e " $asdf = 44; chdir $asdf " Can't chdir('44'): No such file or directory at -e line 1
    moral of the story, single quotes do not interpolate

    Also, chdir does not care about spaces

      use autodie; my $unInstallExepath = "C:\Program Files\NetBeans 7.0\uninstall.exe"; my $path = dirname($unInstallExepath); # $path contain C:\Program + Files\NetBeans 7.0 $path = quotemeta($path); chdir($path) or print "XXXXXXXXXXXXXX $!";

      after quotemeta $path contain \"C\:\\Program\ Files\\NetBeans\ 7\.0 Error is Can't chdir('\"C\:\\Program\ Files\\NetBeans\ 7\.0'): No such file or directory at line 4

      in confusion why it was quoting \" in ----- > chdir('\"C\:\\Program\ Files\\NetBeans\ 7\.0')

        That's because you are not supposed to use quotemeta() here. quotemeta() is for escaping stuff from the regexp engine, not for the shell!

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.