使用批处理文件在 Windows 中生成 GUID

发布于 2024-10-05 16:43:18 字数 42 浏览 10 评论 0原文

如何在 Windows 中使用命令行运行的批处理文件中生成 GUID?

How can I generate a GUID in a batch file running using the commandline in Windows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

岁月如刀 2024-10-12 16:43:18

Windows SDK 附带了一个名为 uuidgen 的工具(如果您有 Visual Studio,则您将拥有 Windows SDK,并且需要运行 Visual Studio 命令提示符 设置正确的路径)。

C:\>uuidgen

这将输出一个新的 GUID,例如

cc23b318-156e-473f-aa6e-517bf091a0f0

The Windows SDK comes with a tool called uuidgen (if you have Visual Studio, you'll have the Windows SDK, and you need to run the Visual Studio Command Prompt to set proper paths).

C:\>uuidgen

This will output a new GUID, e.g.

cc23b318-156e-473f-aa6e-517bf091a0f0

赏烟花じ飞满天 2024-10-12 16:43:18

如果您有 powershell 环境,请尝试此操作。

FOR /F %a IN ('POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"') DO ( SET NEWGUID=%a )

然后准备来自 %NEWGUID% 的 Guid 值

Try this if you have powershell environment.

FOR /F %a IN ('POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"') DO ( SET NEWGUID=%a )

Then ready Guid value from %NEWGUID%

深爱不及久伴 2024-10-12 16:43:18

在 powershell 中很容易做到

[guid]::NewGuid()

easy to do in powershell

[guid]::NewGuid()
深居我梦 2024-10-12 16:43:18

1.创建一个名为 myuuid.vbs 的文件,其内容为

set obj = CreateObject("Scriptlet.TypeLib")  
WScript.StdOut.WriteLine Mid(obj.GUID, 2, 36)

2.goto 命令提示符

cscript //NoLogo myuuid.vbs

使用 JAVA 代码

    UUID uuid = UUID.randomUUID();
    String randomUUIDString = uuid.toString();

1.Create a file named myuuid.vbs with the content

set obj = CreateObject("Scriptlet.TypeLib")  
WScript.StdOut.WriteLine Mid(obj.GUID, 2, 36)

2.goto command prompt

cscript //NoLogo myuuid.vbs

Using JAVA code

    UUID uuid = UUID.randomUUID();
    String randomUUIDString = uuid.toString();
长梦不多时 2024-10-12 16:43:18

这会将新的 GUID 复制到剪贴板:

POWERSHELL -c "[guid]::NewGuid().ToString().ToUpper()" | CLIP

最新版本具有如下所示的 cmdlet:

POWERSHELL -c "New-Guid | Set-Clipboard"

This will copy a new GUID to your clipboard:

POWERSHELL -c "[guid]::NewGuid().ToString().ToUpper()" | CLIP

Recent versions have cmdlets like this:

POWERSHELL -c "New-Guid | Set-Clipboard"
同尘 2024-10-12 16:43:18

没有可用的内置命令可以执行此操作。要么自己编写,要么获取现有的。

可以使用 C# 编写将 GUID 输出到控制台的简单程序:

class Program
{
    static void Main(string[] args)
    {
        System.Console.WriteLine(System.Guid.NewGuid().ToString());
    }
}

将上面的代码片段放在文件名 guidgen.cs 中,然后使用以下命令行进行编译(系统上必须安装 .NET Framework 2.0) :

%WINDIR%\Microsoft.NET\Framework\v2.0.50727\csc.exe guidgen.cs 

这将创建一个名为 guidgen.exe 的可执行文件。

There is no built-in command available that does that. Either write your own, or get an existing one.

A simple program that outputs a GUID to the console could be written using C#:

class Program
{
    static void Main(string[] args)
    {
        System.Console.WriteLine(System.Guid.NewGuid().ToString());
    }
}

Place the above snippet in a file name guidgen.cs and then compile it using the following command line (.NET Framework 2.0 would have to be installed on your system):

%WINDIR%\Microsoft.NET\Framework\v2.0.50727\csc.exe guidgen.cs 

This will create an executable named guidgen.exe.

热鲨 2024-10-12 16:43:18

如果您想使用纯 cmd 命令来执行此操作,您可以使用类似的东西(这不是真正的 GUID,但它可以根据您的上下文提供帮助):

@call :GetGuid NewGuid
@echo My new GUID : %NewGuid%

@goto :eof


:GetGuid
 @set _guid=%computername%%date%%time%
 @set _guid=%_guid:/=%
 @set _guid=%_guid:.=%
 @set _guid=%_guid: =%
 @set _guid=%_guid:,=%
 @set _guid=%_guid::=%
 @set _guid=%_guid:-=%
 @set %1=%_guid%
@goto :eof

If you want to do it with pure cmd commands, you can use something like that (this is not a true GUID but it can help depending on your context) :

@call :GetGuid NewGuid
@echo My new GUID : %NewGuid%

@goto :eof


:GetGuid
 @set _guid=%computername%%date%%time%
 @set _guid=%_guid:/=%
 @set _guid=%_guid:.=%
 @set _guid=%_guid: =%
 @set _guid=%_guid:,=%
 @set _guid=%_guid::=%
 @set _guid=%_guid:-=%
 @set %1=%_guid%
@goto :eof
榆西 2024-10-12 16:43:18

尝试使用 uuid.bat
如果没有参数,它只会回显生成的 uuid

c:\>uuid.bat

2186cb38-d649-4c99-b7cc-c505e4dae9c2

如果传递参数,它将存储在具有相同名称的变量中:

call uuid.bat my_uuid
echo %my_uuid%

94772f66-8bca-468e-9e9a-de0d9ee05cc1

对于 Windows 格式的 GUID,您可以使用 guid.bat

for /f %a in ('guid.bat') do set guid=%a
echo %guid%

try with uuid.bat
Without arguments it will just echo the generated uuid:

c:\>uuid.bat

2186cb38-d649-4c99-b7cc-c505e4dae9c2

If argument is passed it will be stored in a variable with the same name:

call uuid.bat my_uuid
echo %my_uuid%

94772f66-8bca-468e-9e9a-de0d9ee05cc1

For windows formatted GUIDs you can use guid.bat:

for /f %a in ('guid.bat') do set guid=%a
echo %guid%
蝶…霜飞 2024-10-12 16:43:18

如果您安装了 dotnet

$ dotnet tool install -g guid
$ guid
bf4be9d0-7d1b-485c-a435-d07fd7b892f0
$ guid help
Usage:

    guid [option]

Where [option] is an optional single character that controls formatting:

    N    B382C3F44E604D08B48A2D342A659B4E
    D    B382C3F4-4E60-4D08-B48A-2D342A659B4E
    B    {B382C3F4-4E60-4D08-B48A-2D342A659B4E}
    P    (B382C3F4-4E60-4D08-B48A-2D342A659B4E)

When unspecified, 'd' formatting is used. Use lowercase [option] for lowercase output.

If you have dotnet installed:

$ dotnet tool install -g guid
$ guid
bf4be9d0-7d1b-485c-a435-d07fd7b892f0
$ guid help
Usage:

    guid [option]

Where [option] is an optional single character that controls formatting:

    N    B382C3F44E604D08B48A2D342A659B4E
    D    B382C3F4-4E60-4D08-B48A-2D342A659B4E
    B    {B382C3F4-4E60-4D08-B48A-2D342A659B4E}
    P    (B382C3F4-4E60-4D08-B48A-2D342A659B4E)

When unspecified, 'd' formatting is used. Use lowercase [option] for lowercase output.
So尛奶瓶 2024-10-12 16:43:18

人们可以滥用 bitsadmin 来生成 GUID。这将比从批处理脚本调用 PowerShell 快得多。

@echo off & setlocal

for /f "delims={}" %%I in ('bitsadmin /rawreturn /create guid') do set "GUID=%%~I"
>NUL bitsadmin /cancel {%GUID%}

echo Your new GUID is %GUID%.  Neat.

如果您想保留周围的大括号,请删除 "delims={}" 并将 {%GUID%} 替换为 %GUID%

One could abuse bitsadmin to generate a GUID. This will be profoundly faster than calling PowerShell from a Batch script.

@echo off & setlocal

for /f "delims={}" %%I in ('bitsadmin /rawreturn /create guid') do set "GUID=%%~I"
>NUL bitsadmin /cancel {%GUID%}

echo Your new GUID is %GUID%.  Neat.

If you want to keep the surrounding braces, remove "delims={}" and replace {%GUID%} with %GUID%.

月亮坠入山谷 2024-10-12 16:43:18

如果系统操作系统没有 Windows SDK,但有带有 mingw-w64 工具链的 C 编译器,则编译这个小程序以生成随机 GUID。导入的函数是 UuidCreate (rpcrt4 .lib) 创建随机 UUID 和 StringFromCLSID< /a> (ole32.lib) 将 UUID 转换为宽字符串。

#include <Windows.h>
#include <stdio.h>

/*
 * int UuidCreate(GUID *id);
 * int StringFromCLSID(GUID *id, wchar_t **str);
 * Libraries: Rpcrt4.lib Ole32.lib
 */

int main(void)
{
    GUID id;
    wchar_t *str = NULL;

    UuidCreate(&id);
    StringFromCLSID(&id, &str);
    wprintf(L"%ls\n", str);
}

If the system OS does not have Windows SDK but does have a C compiler with mingw-w64 toolchain then compile this small program to generate random GUID. Imported functions are UuidCreate (rpcrt4.lib) to create random UUID and StringFromCLSID (ole32.lib) to convert UUID to wide string.

#include <Windows.h>
#include <stdio.h>

/*
 * int UuidCreate(GUID *id);
 * int StringFromCLSID(GUID *id, wchar_t **str);
 * Libraries: Rpcrt4.lib Ole32.lib
 */

int main(void)
{
    GUID id;
    wchar_t *str = NULL;

    UuidCreate(&id);
    StringFromCLSID(&id, &str);
    wprintf(L"%ls\n", str);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文