返回列表 发帖

【已解决】【30元红包】-生成UUID

本帖最后由 linfeng_321 于 2022-3-23 14:32 编辑

写两个bat文件
-------------------------
脚本里路径变量:
$txtfile=".\文档\文件.txt";
$uuidfile=".\随机UUID\uuid.txt";
-------------------------
pwsh脚本:[System.Guid]::NewGuid().ToString("N").toUpper()
结果格式:8A320F5EBB8944A1A2447D1448B7DABE
-------------------------
1.bat
查找".\随机UUID\uuid.txt"是否有值:
有值时,直接复制到“文件\文件.txt”里@uuid@=。
没值时,“文件\文件.txt”里@uuid@=,生成新的uuid。
结果为:@uuid@=8A320F5EBB8944A1A2447D1448B7DABE

2.bat
“文件\文件.txt”里@uuid@=,等于号后没值时,生成新的uuid。有值时,不改变现有的uuid值。
结果为:@uuid@=8A320F5EBB8944A1A2447D1448B7DABE

回复 1# linfeng_321


1.bat
@echo off
setlocal enabledelayedexpansion
cd /d "%~dp0"
set "txtfile=文档\文件.txt"
set "uuidfile=随机UUID\uuid.txt"
findstr "[0-9a-zA-Z]" "%uuidfile%" >nul
if errorlevel 1 (
    for /f %%i in ('powershell -c "[System.Guid]::NewGuid().ToString('N').toUpper()"') do (
        >"%txtfile%" echo @uuid@=%%i
    )
) else (
    set /p str=<"%uuidfile%"
    >"%txtfile%" echo @uuid@=!str!
)COPY
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 1# linfeng_321


2.bat
@echo off
cd /d "%~dp0"
set "txtfile=文档\文件.txt"
findstr "@uuid@=[0-9a-zA-Z]" "%txtfile%" >nul
if errorlevel 1 (
    for /f %%i in ('powershell -c "[System.Guid]::NewGuid().ToString('N').toUpper()"') do (
        >"%txtfile%" echo @uuid@=%%i
    )
)COPY
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 2# Batcher


大佬".\文档\文件.txt",保留现有txt文件格式
;编码ANSI/GB2312
@uuid@=
@2@=
@1@=
@33@=
@44@=COPY

TOP

本帖最后由 zaqmlp 于 2022-3-23 20:02 编辑

1
<# :
cls&echo off&cd /d "%~dp0"&rem 编码ANSI
powershell -NoProfile -ExecutionPolicy bypass "[IO.File]::ReadAllText(\"%~f0\",[Text.Encoding]::GetEncoding('GB2312'))|Invoke-Expression"
pause
exit
#>
$txtfile=".\文档\文件.txt";
$uuidfile=".\随机UUID\uuid.txt";
if(-not (test-path -literal $txtfile)){write-host ('"'+$txtfile+'" 未找到');exit;}
$enc=[Text.Encoding]::GetEncoding('GB2312');
$uuid='';
if(test-path -literal $uuidfile){
    $text=[IO.File]::ReadAllText($uuidfile, $enc);
    $m=[regex]::match($text, '(?i)[\dA-F]{10,}');
    if($m.Success){$uuid=$m.groups[0].value;}
}
if($uuid -eq ''){$uuid=[System.Guid]::NewGuid().ToString("N").toUpper();}
$text=[IO.File]::ReadAllText($txtfile, $enc);
$text=[regex]::replace($text, '(@uuid@=)[^\r\n]*', {
    param($m);
    $m.groups[1].value+$uuid;
});
[IO.File]::WriteAllText($txtfile, $text, $enc);COPY
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

2
<# :
cls&echo off&cd /d "%~dp0"&rem 编码ANSI
powershell -NoProfile -ExecutionPolicy bypass "[IO.File]::ReadAllText(\"%~f0\",[Text.Encoding]::GetEncoding('GB2312'))|Invoke-Expression"
pause
exit
#>
$txtfile=".\文档\文件.txt";
if(-not (test-path -literal $txtfile)){write-host ('"'+$txtfile+'" 未找到');exit;}
$enc=[Text.Encoding]::GetEncoding('GB2312');
$text=[IO.File]::ReadAllText($txtfile, $enc);
$m=[regex]::match($text, '(@uuid@=)[^\s]+');
if(-not $m.Success){
    $uuid=[System.Guid]::NewGuid().ToString("N").toUpper()
    $text=[regex]::replace($text, '@uuid@=', {
        param($m);
        $m.groups[0].value+$uuid;
    });
    [IO.File]::WriteAllText($txtfile, $text, $enc);
}COPY
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

返回列表