簡単な解決策: PowerShell を使用します。
PS C:\> (Get-Item 'P:\MYPROG~1\SHELLS\ZBACKUP\REFSTO~1.BAL').FullName
P:\MyPrograms\SHELLS\zBackup\RefsToMyData.bal
次のように、バッチ ファイルに PowerShell 呼び出しを組み込むことができます。
@echo off
setlocal
for /f "usebackq delims=" %%f in (
`powershell.exe -Command "(Get-Item '%~1').FullName"`
) do @set "var=%%~f"
echo %var%
出力:
C:\> test.cmd P:\MYPROG~1\SHELLS\ZBACKUP\REFSTO~1.BAL
P:\MyPrograms\SHELLS\zBackup\RefsToMyData.bal
PowerShell は、サポートされているすべての Windows バージョンで利用できます。
- Windows XP SP3 および Server 2003 SP2: PowerShell v2が利用可能
- Windows Vista および Server 2008: PowerShell v1 が同梱されています (既定ではインストールされません)。PowerShell v2 は利用可能です
- Windows 7 および Server 2008 R2: PowerShell v2 がプリインストールされ、PowerShell v3が利用可能(バッテリーは含まれていません)
- Windows 8 および Server 2012: PowerShell v3 がプリインストールされています
何らかの理由 (管理上の制限など) で PowerShell を使用できない場合は、代わりに VBScript を使用します。
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
上記のような VBScript は、次のようなバッチ ファイルで使用できます。
@echo off & setlocal
for /f "delims=" %%f in ('cscript //NoLogo script.vbs "%~1"') do @set "var=%%~f"
echo %var%
ただし、これには追加のスクリプト ファイルが必要です。