使用 windows 批处理脚本更改文件名(保留扩展名)?
Change the name of file using windows batch script (keeping extension)?
我想更改保留扩展名的文件的名称。条件是如果时间戳失败可能是因为 TSA 不可用,那么我想将文件标记为 'unsigned'
已找到签名代码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%
这通过向原始文件添加字符串来保留文件扩展名。
有更好的方法吗?可能正在使用模式匹配?
您也可以使用 FOR /F
来实现这一点。看看这段代码:
@echo off
for /f %%f in ("something.txt") do (
echo %%~nf
echo %%~xf
)
这将输出:
something
.exe
所以在你的情况下你可以这样做:
echo Timestamping failed, marking the file as unsigned.
for /f %%f in ("%2") do (
ren %%f %%~nf_unsigned%%~xf
)
~n
returns 文件名和 ~x
文件扩展名。这个构造可能更长,但它对任何文件都非常有效,而不仅仅是 .exe
.
你可以直接做:
echo Timestamping failed , marking the file as unsigned.
echo ren "%2" "%~n2_unsigned%~x2"
我想更改保留扩展名的文件的名称。条件是如果时间戳失败可能是因为 TSA 不可用,那么我想将文件标记为 'unsigned'
已找到签名代码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%
这通过向原始文件添加字符串来保留文件扩展名。
有更好的方法吗?可能正在使用模式匹配?
您也可以使用 FOR /F
来实现这一点。看看这段代码:
@echo off
for /f %%f in ("something.txt") do (
echo %%~nf
echo %%~xf
)
这将输出:
something
.exe
所以在你的情况下你可以这样做:
echo Timestamping failed, marking the file as unsigned.
for /f %%f in ("%2") do (
ren %%f %%~nf_unsigned%%~xf
)
~n
returns 文件名和 ~x
文件扩展名。这个构造可能更长,但它对任何文件都非常有效,而不仅仅是 .exe
.
你可以直接做:
echo Timestamping failed , marking the file as unsigned.
echo ren "%2" "%~n2_unsigned%~x2"