Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get full path and long file name from short file name

Tags:

batch-file

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 file name?

Example:

  • input variable = "P:\MYPROG~1\SHELLS\ZBACKUP\REFSTO~1.BAL"
  • target variable = "P:\MyPrograms\SHELLS\zBackup\RefsToMyData.bal"

I have tried the following:

  • SET my_file=%~2:

    echo %my_file% produces: "P:\MYPROG~1\SHELLS\ZBACKUP\REFSTO~1.BAL"

  • FOR /F "tokens=* USEBACKQ" %%F IN (`dir /B %2`) DO SET my_file=%%~fF:

    echo %my_file% produces: "P:\MYPROG~1\SHELLS\zBackup\RefsToMyData.bal"

  • FOR /F "tokens=* USEBACKQ" %%F IN (`dir /B %2`) DO SET my_file=%%~dpnxF:

    echo %my_file% produces: "P:\MYPROG~1\SHELLS\zBackup\RefsToMyData.bal"

like image 311
Gavin Avatar asked Sep 06 '13 09:09

Gavin


People also ask

How do I get the full file name from the path?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null.

How can I copy a file name that is too long?

(if the path is too long) First copy the folder to upper levels in windows explorer and then move it to your local computer. (if file names are too long) First try to zip/rar/7z them with an archive application and then copy the archive file to your local computer and then extract the contents. Use third party apps.

How do I determine file path length?

To run the Path Length Checker using the GUI, run the PathLengthCheckerGUI.exe. Once the app is open, provide the Root Directory you want to search and press the large Get Path Lengths button. The PathLengthChecker.exe is the command-line alternative to the GUI and is included in the ZIP file.


1 Answers

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.

like image 188
Ansgar Wiechers Avatar answered Oct 05 '22 09:10

Ansgar Wiechers