bat 脚本为什么总是打印出“系统找不到指定的驱动器。”?

125 天前
 amiwrong123

对 bat 脚本是一点不熟,在大模型的帮助下,写一个脚本。但是不知道为啥,总是会打印出来“系统找不到指定的驱动器。”。

这个脚本的目的就是,在一个相对目录里,复制一些 bin 文件到当前目录下,并显示他们的哈希值。用户先预定义好 bin 文件的名字,然后脚本提供选项让人选。

@echo off
setlocal enabledelayedexpansion

:: 定义文件数量和文件名
set count=2
set file1=type1.bin
set file2=type2.bin
set default_select=1

:PrintFiles
:: 打印文件信息
echo Available files:
for /l %%i in (1,1,%count%) do (
    set "filename=!file%%i!"
    echo %%i !filename!
)

:: 生成示例选择字符串
set example_selection=
for /l %%i in (1,1,%count%) do (
    set example_selection=!example_selection! %%i
)

:PromptUser
:: 提示用户选择文件
echo.
echo Please select the file numbers you want to copy (1-%count%).
echo For example:!example_selection! to select all files.
echo Press Enter to use the default selection: %default_select%.
set /p selection=Your selection [default: %default_select%]: 

:: 检查用户是否直接按下了 Enter
if "%selection%"=="" (
    set selection=%default_select%
)

:: 初始化检查状态
set valid_selection=true

:: 验证用户输入的选择是否在范围内
for %%i in (%selection%) do (
    :: 去除选择中的引号,确保数字比较正确
    set "num=%%i"
    set num=!num:"=!
    
    if !num! lss 1 (
        echo Invalid selection: !num! is less than 1.
        set valid_selection=false
    ) else if !num! gtr %count% (
        echo Invalid selection: !num! exceeds %count%.
        set valid_selection=false
    )
)

:: 如果选择无效,重新提示用户输入
if "%valid_selection%"=="false" (
    echo Invalid selection. Please try again.
    goto :PromptUser
)


:: 执行复制操作
for %%i in (%selection%) do (
    :: 获取选择的文件
    set "file=!file%%i!"

    echo.
    echo %%i !file!
    
    :: 复制文件,从 new 目录复制到当前目录
    echo Copying !file! from new to current folder...
    copy "..\AI\!file!" ".\"

@REM certutil 的输出如下
@REM SHA1 的 type1.bin 哈希:
@REM def8bca03fab3781b90158550d40d377ab57e906
@REM CertUtil: -hashfile 命令成功完成。

@REM 所以下面这个循环要这么获取
    set line_count=0
    for /f "tokens=*" %%j in ('certutil -hashfile ".\!file!"') do (
        set /a line_count+=1
        ::echo Inner loop: %%i
        :: 当循环到第二行时,赋值给 md5
        if !line_count! equ 2 (
            set md5="%%j"
        )
    )
    
    :: 输出文件和 MD5 哈希值
    echo !file! md5 is !md5!
)

:end
echo Done!
pause

:: 结束
endlocal

CertUtil 的输出是三行,而这个奇怪的打印也是三行。感觉跟这个循环有关系。

另外,也注释了 @echo off 来看执行过程,看没看懂。

有会的大佬帮忙看看咋回事吧,感谢

PS:复制的功能和打印哈希的功能倒是实现了。。

753 次点击
所在节点    问与答
6 条回复
llxvs
124 天前
code block 中不能使用双冒号注释,改用 rem 试试。
amiwrong123
124 天前
@llxvs #1
Available files:
1 type1.bin
2 type2.bin

Please select the file numbers you want to copy (1-2).
For example: 1 2 to select all files.
Press Enter to use the default selection: 1.
Your selection [default: 1]: 1 2

1 type1.bin
Copying type1.bin from new to current folder...
已复制 1 个文件。
type1.bin md5 is "def8bca03fab3781b90158550d40d377ab57e906"

2 type2.bin
Copying type2.bin from new to current folder...
已复制 1 个文件。
type2.bin md5 is "2137abf34c69627d936177dbd5d80e6540bc3cfd"
Done
请按任意键继续. . .

按你说的。我干脆把 最后那个双层循环里的 注释都删掉了,然后居然好了,神奇
amiwrong123
124 天前
@llxvs #1
所以,这全是 注释的锅了呗?
llxvs
124 天前
@amiwrong123 是的,双冒号开头的注释不能用在括号中
amiwrong123
124 天前
@llxvs
OK ,感谢解答。

:: 验证用户输入的选择是否在范围内
for %%i in (%selection%) do

其实我这个循环里也有 注释,但这个循环没有问题,,看来是出问题是有概率的呗
llxvs
124 天前
@amiwrong123 在某些条件下会出问题

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1080266

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX