1. You are viewing our forum as a guest. For full access please Register. WindowsBBS.com is completely free, paid for by advertisers and donations.

Resolved Extracting output from ASSOC and FTYPE in a CMD file

Discussion in 'Windows 10' started by Ludwig, 2019/07/16.

  1. 2019/07/16
    Ludwig

    Ludwig Well-Known Member Thread Starter

    Joined:
    2002/01/07
    Messages:
    146
    Likes Received:
    0
    I need to be able to invoke Excel with the /R parameter in certain situations to open the file read-only.

    I have found the ASSOC command and the FTYPE command which when invoked manually will tell me which exe the user would invoke. What I need to be able to do is know what Excel.exe the user's profile will invoke when they open (in this case) the filetype .xlsm.
    Code:
    ASSOC | find /i ".xlsm="
    
    will tell me the Excel text string that is associated with that extension. I cannot figure how to get that reply [in my case the text "Excel.SheetMacroEnabled.12" without the double quotes] into an environment variable say %ExcStr%. From there I can use -
    Code:
    FTYPE %ExcStr%  
    
    and extract the value after the equals sign which will be in double-quotes [in my case "C:\Program Files (x86)\Microsoft Office\Root\Office16\EXCEL.EXE"]. Then once I have that value in an environment variable I can use that in a START command to open Excel with /R and the excel filename to be opened read-only. I currently determine the locaiotn of Excel.exe using this command -
    Code:
    for /f "delims=" %%a in ('where /r "C:\Program Files (x86)" excel.exe') do set "myLocn=%%a"
    
    But this finds the first version installed (e.g. Excel 2013) and not the latest (Excel 2016, which the macro-enabled workbook requires) if there is more than one version available. I search for it because for some weird reason Excel is not installed in the same directory on every machine that uses it!

    The START command I currently have is
    Code:
    start "%myFN%" /MAX "%myLocn%" %1 "%CD%\%myFN%.XLSM"
    
    Sounds long winded, but this is where I've got to. Can someone help me with the CMD file coding required to get the results I need?

    ...................

    Or is there a much simpler way to get Exel to open the file read-only without having to resort to this complicated method of determining the full path & exe name to issue in a START command?

    Any help most appreciated.
     
  2. 2019/07/16
    TonyT

    TonyT SuperGeek Staff

    Joined:
    2002/01/18
    Messages:
    9,072
    Likes Received:
    400
    If you set a version of Excel as default on the machines then associate extensions with that default, then Windows should open files with that default version of Excel, regardless of multiple versions being installed. Then you just need the 'start' command.
     

  3. to hide this advert.

  4. 2019/07/16
    Ludwig

    Ludwig Well-Known Member Thread Starter

    Joined:
    2002/01/07
    Messages:
    146
    Likes Received:
    0
    Thanks for the feedback. Unfortunately that only works for files on the user's local drive, and if you don't want to invoke Excel with the /R option. In my rush to get some sleep last night I forgot to mention in my original post that this macro-enabled workbook is stored in sharepoint and file references starting "https://..." won't automatically open in the assigned default program, which means Windows says -
    Code:
    The filename, directory name, or volume label syntax is incorrect.
    
    Most of the users don't know Sharepoint, meaning a quicker shortcut method via a CMD file is much easier for them to use the system, and literally provides anyone access to the file with a one-click "open" method instead of having to browse through Sharepoint pages to get to it.

    This morning I have figured the basics of parsing the ASSOC and FTYPE outputs to extract the full path name including EXE name in a CMD file. Test CMD file to get that info follows ..
    Code:
    @echo OFF
    set txAssoc=
    for /f "tokens=2 delims==""" %%a in ('assoc .xlsm') do set "txAssoc=%%a"
    echo Assoc output is .%txAssoc%.
    set myLocn=
    for /f "tokens=2 delims==" %%a in ('ftype %txAssoc%') do call :StoreMe %%a
    echo.
    echo .%myLocn%.
    if     .%myLocn%.==.. echo Excel is NOT installed
    if not .%myLocn%.==.. echo Excel is installed
    goto :EOF
    
    REM  If I dont use this call process then the second parameter of %1 in double-quotes gets included
    :StoreMe
    set "myLocn=%1"
    goto :EOF
    
    I will now make it a little more robust, e.g. testing that Excel is installed from the ASSOC output rather than just FTYPE before replacing the existing CMD file contents which finds the first excel.exe (which could be an earlier version than 2016 or potentially even an update install!).
     
    Last edited: 2019/07/16

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.