in reply to Re^2: Problem running shell command from Perl
in thread Problem running shell command from Perl

I have discovered that if the original filename does not contain unicode chars (ie all 7-bit ascii), my program can create an 8dot3 name. So the following works:

my $trans2 = `fsutil file setShortName \"C:\\Users\\James\\Music\\thewarrior16\" \"THEW~001\"`;

But if the filename contains unicode, the fsutil command fails. So the following does NOT work:

my $trans = `fsutil file setShortName \"C:\\Users\\James\\Music\\國語懷念老歌 Vol. 2\" \"Vol2~0UG\"`;

Is perl somehow mangling the unicode chars before it passes them to fsutil? There's got to be a simple solution to this. But then, considering what a mess perl's handling of unicode is, maybe not ...

Any more ideas?

  • Comment on Re^3: Problem running shell command from Perl

Replies are listed 'Best First'.
Re^4: Problem running shell command from Perl
by harangzsolt33 (Deacon) on Jun 20, 2023 at 16:59 UTC
    I am no expert, but here is what I would try next in this situation: I would open Notepad and create a BAT file. I would copy and paste whatever command you said works. Then I would save this as TEST.BAT and make sure to select Unicode format after you click on Save As. Okay. Now run the program. Does it still work? It should. But then the next step is to see if you can generate the same BAT file from Perl. Run it and see what happens. When you take the $trans string and save it to a text file in Unicode file format, the output file of your Perl script should match exactly the the file you saved with Notepad. You will be able to compare the content using a Hex Editor to see if the bytes match. They should. Because if the file generated by Notepad works and the file generated by Perl doesn't work, then you know for sure that Perl does something to the string before saving it to a file. That's what I am suspecting, but I don't know. So, I would try to save it to a file first from Perl to be able to catch exactly how and where things go wrong.

    Edit: Let's say that the Notepad version of BAT file works and the Perl version BAT file works also and they match. But then when you put the command between backticks, it fails. That would tell us that when perl sends the string to be executed by the shell, something happens to the string and it changes from Unicode to ASCII. Then I don't know what other option you have. But maybe try system("something > tempfile.txt"); and see what that does.

      We have a winner!

      Writing the command to a bat file then running the bat file with backticks gets the job done. I doubt I would ever have thought of that, so thanks!

      Seems like a crazy amount of overhead to accomplish what should be a simple task. But it works, so I'm happy.

      This issue is resolved.

        I'm glad it works.

        While I was going about my business, I kept thinking about this issue, and I had another idea: Create a FAT32 file system either on an external hard drive or on your main hard drive and move or copy your entire Music directory on that file system. The FAT32 file system automatically maintains two file names for every file -- one 8+3 short name and one long name. That way the short names would be created at the time you copy the files, and older programs would immediately see the short named files. This would bypass the need to rename each file.