【问题标题】:Change the name of file using windows batch script (keeping extension)?使用 Windows 批处理脚本更改文件名(保留扩展名)?
【发布时间】:2016-05-27 11:12:30
【问题描述】:

我想更改保留扩展名的文件的名称。条件是如果时间戳失败可能是因为 TSA 不可用,那么我想将文件标记为“未签名”

已找到签名的代码here

场景是:

批处理文件:sign.bat

arg2 : file1.exe

预期输出 如果时间戳失败,则需要将 'file1.exe' 标记为 'file1_unsigned.exe'
在我做的所有搜索之后,

echo Timestamping failed , marking the file as unsigned. 
set file_name=%2
set unsigned_file_name=%file_name:.exe=_unsigned.exe%
ren %file_name% %newfilename%

这通过向原始文件添加字符串来保留文件扩展名。

有没有更好的方法来做到这一点?可能正在使用模式匹配?

【问题讨论】:

    标签: windows batch-file rename file-extension


    【解决方案1】:

    你可以直接做:

     echo Timestamping failed , marking the file as unsigned.
     echo ren "%2" "%~n2_unsigned%~x2"
    

    【讨论】:

    • 是的,这有帮助。非常感谢!
    【解决方案2】:

    您也可以使用FOR /F 来实现此目的。看看这段代码:

    @echo off
    for /f %%f in ("something.txt") do (
        echo %%~nf
        echo %%~xf
    )
    

    这将输出:

    某事

    .exe

    所以在你的情况下你可以这样做:

    echo Timestamping failed, marking the file as unsigned. 
    for /f %%f in ("%2") do (
        ren %%f %%~nf_unsigned%%~xf
    )
    

    ~n 返回文件的名称和~x 的扩展名。这个结构可能会更长,但它可以完美地适用于任何文件,而不仅仅是.exe

    【讨论】:

      猜你喜欢
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 2017-03-12
      • 2021-04-11
      • 2023-02-23
      • 1970-01-01
      相关资源
      最近更新 更多