in reply to Problem running shell command from Perl

Were I to hazard a guess, I would say it is the fact that the Windows CMD prompt does not understand '/' as a path separator; instead, it uses '\' (which most things with a *nix background -- including C and perl -- use as an escape character). In order to include a '\' in a string, you would need to use "\\", or a '\' in a single-quoted string. You can also use the q or qq operators as a way to quote the string. ('q//' is a way to represent a single-quoted string, and 'qq//' a double-quoted string.)

# Replace assignment with this. # 'q//' treats as a single-quoted string, which does not try to # interpolate characters. As a result, the double-quotes in # the command do not require escaping. Here I used the '/' as # the delimiter for the single-quoting 'q', but could be other # characters depending on what is needed in the string. I also # used the '.' to concatenate the string, allowing it to be # written across multiple lines so the spaces between parameters # were obvious. my $cmd = q/fsutil/ # command . q/ / . q/file/ # parameter 1 . q/ / . q/setShortName/ # parameter 2 . q/ / . q/"C:\Users\James\Music\國語懷念&# +32769;歌 Vol. 2"/ # parameter 3 . q/ / . q/"Vol2~00A"/; # parameter 4 # The double-quotes in parameter 3 are necessary # due to the spaces within the file name. # Continue remainder of code as before

Hope that helps.

Replies are listed 'Best First'.
Re^2: Problem running shell command from Perl
by CrashBlossom (Beadle) on Jun 20, 2023 at 02:58 UTC

    Thanks for your response.

    I did try using "\\" as the separator, and also tried using 'q//' as described in your msg. In both cases I got the same result: "Error: The system cannot find the file specified"

      If memory serves, &#{value}; is an HTML representation; you might have to designate those characters using the \x{value} or \N{U+value}. So you might try writing that part of the string as something like:

      q/"C:\Users\James\Music\/ . qq/\N{U+22283}\N{U+35486}/ . qq/\N{U+25079}\N{U+24565}/ . qq/\N{U+32769}\N{U+27468}/ . q/ Vol. 2"/

      Does that help?

        I tried several variations of this, but could not get any of them to work. The characters still seem to get mangled on the way to fsutil.