Get full path and long file name from short file name

前端 未结 10 756
故里飘歌
故里飘歌 2020-12-15 09:13

I have an nice console file manager (eXtreme by Senh Liu), it passes short path/filenames as variables to a menu.bat.

How can I generate a full folder name + long fi

10条回答
  •  执笔经年
    2020-12-15 09:46

    Simple solution: use PowerShell.

    PS C:\> (Get-Item 'P:\MYPROG~1\SHELLS\ZBACKUP\REFSTO~1.BAL').FullName
    P:\MyPrograms\SHELLS\zBackup\RefsToMyData.bal

    You can incorporate a PowerShell call in a batch file like this:

    @echo off
    
    setlocal
    
    for /f "usebackq delims=" %%f in (
      `powershell.exe -Command "(Get-Item '%~1').FullName"`
    ) do @set "var=%%~f"
    
    echo %var%
    

    Output:

    C:\> test.cmd P:\MYPROG~1\SHELLS\ZBACKUP\REFSTO~1.BAL
    P:\MyPrograms\SHELLS\zBackup\RefsToMyData.bal

    PowerShell is available for all supported Windows versions:

    • Windows XP SP3 and Server 2003 SP2: PowerShell v2 available
    • Windows Vista and Server 2008: ship with PowerShell v1 (not installed by default), PowerShell v2 available
    • Windows 7 and Server 2008 R2: PowerShell v2 preinstalled, PowerShell v3 available (batteries not included)
    • Windows 8 and Server 2012: PowerShell v3 preinstalled

    If PowerShell can't be used for some reason (e.g. administrative restrictions), I'd use VBScript instead:

    name = WScript.Arguments(0)
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(name) Then
      Set f = fso.GetFile(name)
    ElseIf fso.FolderExists(name) Then
      Set f = fso.GetFolder(name)
      If f.IsRootFolder Then
        WScript.Echo f.Path
        WScript.Quit 0
      End If
    Else
      'path doesn't exist
      WScript.Quit 1
    End If
    
    Set app = CreateObject("Shell.Application")
    WScript.Echo app.NameSpace(f.ParentFolder.Path).ParseName(f.Name).Path
    

    A VBScript like the one above can be used in a batch file like this:

    @echo off & setlocal
    
    for /f "delims=" %%f in ('cscript //NoLogo script.vbs "%~1"') do @set "var=%%~f"
    
    echo %var%
    

    This does require an additional script file, though.

提交回复
热议问题