Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Cannot read text in script  (Read 3862 times)

0 Members and 1 Guest are viewing this topic.

Blue

    Topic Starter


    Rookie

    • Experience: Experienced
    • OS: Windows 7
    Cannot read text in script
    « on: March 25, 2017, 07:08:40 AM »
    I don't think I have found a bug, but I have surely uncovered a problem.

    I am trying to capture a list of drive letters and the corresponding volume names. It's simple using the command:
    WMIC logicaldisk get deviceid, volumename
    What comes back looks like TEXT - but it ISN'T !!!

    I then try to process the output of the "WMIC" command using a cmd/bat "FOR" statement. I have coded and tested more than a dozen different attempts - tested for nearly half a day - on four different Windows computers (2 Win-7 and 2 Win-10) - and it simply does not work. The reason is that WMIC outputs something that looks like text --- BUT IT ISN'T --- and none of the WMIC format options work either. This is driving me crazy because I know the script works properly. I have included my script in the code below. You can run it "as-is" and check the output. Then, you can change the file being created by making the WMIC line a comment and UN-commenting the line following it. The "dir" command does produce text and the script will work correctly.

    I have tried editing the file produced by WMIC and that does not work either. I have tried creating a new file using NOTEPAD and just typing in the exact same information produced by the WMIC command. That creates real text and the script work perfectly.

    I have hex-dumped all the files and can see no significant difference between the output of the WMIC command the other text lists I have created/used. Still... nothing coming out of WMIC can be processed by a cmd "FOR" statement - either by creating a separate output file or by coding the command inside the "FOR" statement.

    I know some folks may offer different methods of getting the information I need. I appreciate that - but it will not solve the problem I have found. If anyone can tell me why the script I have created does not work I will be truly grateful. This is a challenge I have not been able to conquer - I hope someone else can.

    THANKS IN ADVANCE..... Blue

    Code: [Select]
    @echo off
    ::
    wmic logicaldisk get name,volumename > %temp%\disklist.txt
    ::dir c:\ >  %temp%\disklist.txt
    ::
    set _dlist=%temp%\disklist.txt
    ::
    echo This is the DiskList file generated by WMIC
    echo (it should display twice)
    echo File: %_dlist%
    echo. & echo. & echo. & echo.
    echo 1. typing DiskList file to console
    type %_dlist%
    ::
    ::
    echo. & echo. & echo. & echo.
    echo 2. listing DiskList file using -For- command
    For /f "tokens=1 delims=+" %%g in ( %_dlist% ) do (
      echo %%g
    )
    echo. & echo.
    echo Is the second list above this line?
    echo Not on my system
    echo. & echo.
    pause
    exit

    Hackoo



      Hopeful
    • Thanked: 42
    • Experience: Expert
    • OS: Windows 10
    Re: Cannot read text in script
    « Reply #1 on: March 25, 2017, 01:24:45 PM »
    http://stackoverflow.com/questions/43011329/capture-result-of-wmic-command-to-variable
    I dont know if this can give you more information about WMIC output

    Blue

      Topic Starter


      Rookie

      • Experience: Experienced
      • OS: Windows 7
      Re: Cannot read text in script
      « Reply #2 on: March 25, 2017, 01:52:19 PM »
      Hackoo - Great catch and outstanding research !!

      I knew that encoding had to be the answer, but this is the key I could not figure out for myself...
      "WMI results are encoded in a non-ANSI encoding (UCS-2 LE). Capturing the output of wmic also captures the output's encoding, resulting in the last character being moved to the beginning of the line or other unexpected behavior. The workaround for that is to use a second nested for /f to sanitize the value."

      I will try implementing his method and, hopefully, get it working.

      THANK YOU !!


      FWIW: Only some douchebag at Microsoft could come up with a convention like this. It is scary. Exactly these sorts of things are the principal reason I have been migrating to Linux more-and-more over the last couple of years.

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Cannot read text in script
      « Reply #3 on: March 25, 2017, 03:43:08 PM »
      WMIC is a thin wrapper around WMI which as a more "modern" API uses UTF-16 Unicode strings. Command Prompt, for backwards compatibility reasons, deals with ANSI.

      WMI was designed primarily to be used from VBScript, and more recently Powershell. WMIC is more of a hack that provides some of the WMI functionality some of the time to some Command Prompt users. But it doesn't really do a great job of actually transforming the data to a form that is more usable in Command Prompt- (again, thin wrapper, exercise for the user, etc.)

      Bash sometimes presents similar encoding issues, if a program outputs in a format that isn't the same as the setting for Bash in /etc/rc.conf. (not sure where it is with systemd). But it defaults to UTF-8 which tends to work better. (Of course UTF-8 wouldn't work so well for Command Prompt since it woudl break loads of batch files that rely on it being ANSI)
      I was trying to dereference Null Pointers before it was cool.