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

Thanks for your response.

Let me give a bit more detail about that I'm trying to do. I have an app that allows the user to browse through directories and select files whose names are then passed to various other applications. Some of these apps can't handle filenames that contain unicode (non 7-bit ascii) characters). I want to modify my app so that if the user selects a file or directory containing unicode characters, my app can create the 8dot3 name on the fly. I can then run the filename through Win32::GetShortPathName to get a name consisting entirely of 7-bit ascii characters.

To address the points mentioned in your post:

1. 8dot3 names are already enabled on my system

2. understood

3.The perlmonks website converted the chinese characters to the notation that ended up in the post, apparently due to the fact that the filename was contained in a "<code>" section. Here's the real filename:

C:\Users\James\Music\國語懷念老歌 Vol. 2

So if I paste the following into a windows command shell

fsutil file setShortName "C:\Users\James\Music\國語懷念老歌 Vol. 2" "Vol~002"

the 8dot3 name is created as desired. Now I need to find a way to do this in my perl program.

4. not a problem in my case

5. I need to preserve the original file names, so renaming is not an option.

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

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

    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?

      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.