ykaganovich
ykaganovich

Reputation: 14964

In a batch script, how do I get the long name of a file?

How do I get the long path of a file or directory?

I need a path to the temporary directory that doesn't have a ~ in it.

%TEMP% resolves to C:\Users\YKAGAN~1\AppData\Local\Temp

How do I obtain C:\Users\ykaganovich\AppData\Local\Temp ?

Upvotes: 2

Views: 1122

Answers (2)

Patrick Cuff
Patrick Cuff

Reputation: 29786

EDIT: Updated solution using for /r /d as suggested by Joey:

This is a pretty greedy way to do it, but it works:

pushd c:\

for /r /d %%D in (.) do (
    if /i %%~sD equ %TMP% (
        @echo %%~dpfD
        popd
        exit /b
    )
)
  • First pushd c:\ to make c:\ the starting point of the search.

  • for /r /d (.) will walk the directory tree.

  • We loop through the list of directories comparing the short name (%%~sD) to the name in the variable TMP. If they're equal we print out the expanded long name, restore the original working directory, and exit the script.

  • Needed to use %%dpfD rather than %%D; for some reason %%D had \. tacked on to the end of the path name.

  • I tried using if %%~sD==%TMP% at first, but it didn't work.

I tested this in WindowsXP and Windows 7 and works on both. Takes a while to run, but eventually gets the job done.

Upvotes: 1

user541686
user541686

Reputation: 210427

Try this program I just wrote.

Source (D):

import core.stdc.wchar_, core.sys.windows.windows;
extern (C) int __wgetmainargs(out int pargc, out wchar** pargv, out wchar** penvp, int dowildcard, out int startinfo);
extern (Windows) BOOL Wow64DisableWow64FsRedirection(out void* OldValue);
extern (Windows) HMODULE LoadLibraryW(in LPCWSTR lpFileName);
extern (Windows) DWORD GetLongPathNameW(in LPCWSTR lpszShortPath, LPWSTR lpszLongPath, in DWORD cchBuffer);
pragma(startaddress, wmainCRTStartup);
pragma(lib, "msvcrt.lib");
void* disableWow64Redirection()
{
    auto pKernel32 = LoadLibraryW("kernel32.dll");
    void* old;
    auto fn = cast(typeof(&Wow64DisableWow64FsRedirection))GetProcAddress(cast(HMODULE)pKernel32, "Wow64DisableWow64FsRedirection");
    if (fn != null) { if (!fn(old)) { } }
    else { old = null; }
    FreeLibrary(pKernel32);
    return old;
}
int wmainCRTStartup()
{
    disableWow64Redirection();
    int argc, si; wchar** wargv, wenvp;
    __wgetmainargs(argc, wargv, wenvp, 0, si);
    wchar[32 * 1024] buffer = void; //big enough for all valid paths
    foreach (i; 1 .. argc)
    {
        auto len = GetLongPathNameW(wargv[i], buffer.ptr, buffer.length - 1);
        buffer.ptr[len] = '\0';
        if (i > 1) { wprintf(" "); }
        wprintf("%s", len > 0 ? buffer.ptr : wargv[i]);
    }
    return 0;
}

Usage:

name <short-name>

What the heck, here's a C version:

#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
typedef BOOL (WINAPI * PWow64DisableWow64FsRedirection)(void** OldValue);
int _tmain(int argc, TCHAR *wargv[])
{
    int i;
    wchar_t buffer[32 * 1024]; //big enough for all valid paths
    PWow64DisableWow64FsRedirection fn =
        (PWow64DisableWow64FsRedirection)GetProcAddress(
        GetModuleHandle(_T("kernel32.dll")), "Wow64DisableWow64FsRedirection");
    if (sizeof(size_t) > 4 && fn != NULL) //Remove if WOW64 wanted
    { void* old; Wow64DisableWow64FsRedirection(&old); }
    for (i = 1; i < argc; i++)
    {
        DWORD len = GetLongPathNameW(wargv[i], buffer, ARRAYSIZE(buffer) - 1);
        buffer[len] = _T('\0');
        if (i > 1) { wprintf(_T(" ")); }
        wprintf(_T("%s"), len > 0 ? buffer : wargv[i]);
    }
    return 0;
}

Upvotes: 1

Related Questions