如何获取文件或目录的长路径?
我需要一个到临时目录的路径,该目录中没有~
。%TEMP%
解析为C:\Users\YKAGAN~1\AppData\Local\Temp
如何获得C:\Users\ykaganovich\AppData\Local\Temp
?
最佳答案
我刚写了封信。
来源(this program):
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;
}
用途:
name <short-name>
见鬼,这是C版本:
#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;
}
关于windows - 在批处理脚本中,如何获取文件的长名称?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6104395/