Get DOS/Short Paths+Names From File List

Discuss and share scripts and script files...
Post Reply
naarcissus
Posts: 3
Joined: 06 Mar 2014 19:37

Get DOS/Short Paths+Names From File List

Post by naarcissus »

I'm trying to get the DOS name from a list of files. Right now, I'm using using foreach and then doing a curitem_dos to get the short paths/names, but I'd like to be able to get this without resorting to having to have each file selected individually for each query.

Specifically, what I'm doing is getting around the filename/commandline limitations :roll: of my media player (PotPlayer) by creating a playlist (XSPF) in %TEMP%, that includes both the Short Name for a target item (location) and the normal filename base as the Title (title), then opening that with the player.

Code: Select all

"Make XSPF Playlist and Open : xspf"
  $q = chr(34);
  $lt = chr(60);
  $gt = chr(62);
  $xspf = "$lt".'?xml version='."$q".'1.0'."$q".' encoding='."$q".'UTF-8'."$q".'?'."$gt".<crlf>."$lt".'playlist version='."$q".'1'."$q".' xmlns='."$q".'http://xspf.org/ns/0/'."$q"."$gt".<crlf>.'  '."$lt".'trackList'."$gt".<crlf>.'    ';
  
  foreach ($file, <selitems |>, "|") {
    selectitems $file;
    $xspf = $xspf."$lt".'track'."$gt".<crlf>.'      '."$lt".'title'."$gt".<curbase>."$lt".'/title'."$gt".<crlf>.'      '."$lt".'location'."$gt".<curitem_dos>."$lt".'/location'."$gt".<crlf>.'    '."$lt".'/track'."$gt".<crlf>.'    ';
    
  }
  $xspf = $xspf."$lt".'/trackList'."$gt"."$lt".'/playlist'."$gt".<crlf>;
  writefile("%temp%\xy\playlist.xspf", $xspf,"o", "utf8bom");
  open "%temp%\xy\playlist.xspf";
  end 1==1;
This works fine for small lists of files, but it would get really tedious for longer lists and trying to add items in selected folders. Thus my desire to get the Short Path/Name for each file without it having to be currently selected.

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Get DOS/Short Paths+Names From File List

Post by highend »

Unfortunately there is no easy way to get the short DOS path for unselected files...

<deleted>. See viewtopic.php?p=198845#p198845
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60558
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Get DOS/Short Paths+Names From File List

Post by admin »

Next beta:

Code: Select all

    + SC getpathcomponent(), gpc(): Added component value "dos" to convert a long
      path spec to DOS format (aka 8.3 format).
      Remarks:
        - Yes, that's a bit of an abuse of this function, but tolerable I think.
        - Only the valid (existing) part of the input path is returned.
        - The return is always without trailing backslash, even if the input had one.        
        - Note that in Windows the 8.3 file naming can be enabled/disabled per drive. If it is disabled 
          then this function returns the (valid part of the) input unchanged (apart from 
          removing any trailing backslash).
      Example:
        echo getpathcomponent("C:\Program Files", "dos"); //C:\PROGRA~1
    + SC getpathcomponent(), gpc(): Added component value "long" to convert a DOS/8.3 
      path spec into the long format.
      Remarks:
        - See above under "dos".
      Example: 
        echo getpathcomponent("C:\PROGRA~1", "long"); //C:\Program Files

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Get DOS/Short Paths+Names From File List

Post by highend »

Internally via Win32 API GetShortPathNameW()?
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60558
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Get DOS/Short Paths+Names From File List

Post by admin »

Actually no, GetShortPathNameW has some problems. Instead I use FindFirstFileW with the cAlternate return field.

highend
Posts: 13313
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Get DOS/Short Paths+Names From File List

Post by highend »

I see.

So, this script should work with the next beta...

Code: Select all

"Make XSPF Playlist and Open : xspf"
    $ver = "23.00.0208"; end compare(<xyver>, $ver, "v") == -1, "This script requires at least v$ver, terminating now!";
    $sel = <get SelectedItemsPathNames>;
    end (!$sel), "No item(s) selected, aborted!";

    $sp2  = "  ";
    $sp4  = $sp2 . $sp2;
    $sp6  = $sp4 . $sp2;
    $lb   = <crlf>;
    $xspf = <<<>>>
<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
  <trackList>

    >>>;

    // Build list of files (selected files and files in selected folders)
    $files = "";
    foreach($item, $sel, $lb, "e") {
        if     (exists($item) == 1) { $files .= $item . $lb; }
        elseif (exists($item) == 2) { $files .= quicksearch("/f", $item) . $lb; }
    }
    $files = formatlist($files, "se", $lb);

    // Build the playlist
    if ($files) {
        foreach ($item, $files, $lb) {
            $title = gpc($item, "base");
            $short = gpc($item, "full", 3:=8);
            $xspf .= $sp4 . "<track>" . $lb . $sp6 . "<title>" . $title . "</title>" . $lb . $sp6 . "<location>" . $short . "</location>" . $lb . $sp4 . "</track>" . $lb;
        }
        $xspf .= $sp2 . "</trackList>" . $lb . "</playlist>" . $lb;

        $plFile = "%TEMP%\xy\playlist.xspf";
        writefile($plFile, $xspf, "o", "utf8bom");
        open "$plFile";
    }
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60558
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Get DOS/Short Paths+Names From File List

Post by admin »

I added also this now to combine it with the full power of gpc():

Code: Select all

    + SC getpathcomponent(), gpc(): Added flag values to convert a path to and from 
      DOS/8.3 format, and a new component value "full".
      Syntax: getpathcomponent([path], [component], [index=1], [flags])
        component: 
          full:   Return all components of the input path.
        flags: (bit field)
          8 = Convert long format to DOS/8.3
         16 = Convert DOS/8.3 to long format
      Examples:
        echo getpathcomponent("C:\Program Files", "full", 3:=8);  //C:\PROGRA~1
        echo getpathcomponent("C:\PROGRA~1", "full", 3:=16); //C:\Program Files
        echo getpathcomponent("C:\Program Files\AutoHotkey\AutoHotkeyU64.exe", "file", 3:=8);  //AUTOHO~3.EXE

naarcissus
Posts: 3
Joined: 06 Mar 2014 19:37

Re: Get DOS/Short Paths+Names From File List

Post by naarcissus »

highend wrote: 06 May 2022 08:40 Unfortunately there is no easy way to get the short DOS path for unselected files...
This was what my searches for solution had revealed as well and I was afraid that had not changed…. Glad I asked though. :biggrin:
admin wrote: <Modifies function to fix problem>
Wow! Thank you, Don. :appl: I was rather surprised when I didn't see a windows property for Short Paths and my searches hadn't born much fruit. I was afraid I'd have to resort to running an external utility to—fighting quotes all day until I got it right—get the Short Paths, maybe something from NIr would do it. :ugeek: Can't wait to try out the new GPC command though!

And thanks, Highend, for taking the time to redo my script, I see I've much to learn…. :veryconfused:

On a side note, File Explorer and XY with CFA turned off seem to automagically supply the player with the short path for overlong file names, but with CFA enabled, it doesn't. :shrug:

Edit:
Works well and a bit faster than when each file had to be selected in turn. Had change the formatlist so it wouldn't reorder the files though. :naughty: Thanks again!

Post Reply