gpt4 book ai didi

windows - 在批处理脚本中,如何获取文件的长名称?

转载 作者:可可西里 更新时间:2023-11-01 12:04:48 27 4
gpt4 key购买 nike

如何获取文件或目录的长路径?

我需要一个没有 ~ 的临时目录的路径。

%TEMP% 解析为 C:\Users\YKAGAN~1\AppData\Local\Temp

如何获取 C:\Users\ykaganovich\AppData\Local\Temp

最佳答案

尝试 this program我刚刚写了。

来源(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;
}

用法:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com