1

I wanted to change the name of the file keeping the extension. The condition was if timestamping failed may be because of unavailability of TSAs, then I wanted to mark the file as 'unsigned'

The code for signed was found here

Scenario is:

batch file : sign.bat

arg2 : file1.exe

expected output if timestamping fails then need to mark the 'file1.exe' as 'file1_unsigned.exe'
After all the searches what I did was,

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%

This kept the file extension by adding a string to original file .

Is there a better way to do this ? may be using pattern matching ?

Community
  • 1
  • 1
Sach106
  • 63
  • 6

2 Answers2

0

You can also use FOR /F to achieve this. Take a look at this code:

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

This will output:

something

.exe

So in your case you could do this:

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

~n returns the name and ~x the extension of a file. This construct might be longer but it will perfectly work for any file and not only for .exe.

Community
  • 1
  • 1
MichaelS
  • 5,941
  • 6
  • 31
  • 46
0

You can directely do :

 echo Timestamping failed , marking the file as unsigned.
 echo ren "%2" "%~n2_unsigned%~x2"
SachaDee
  • 9,245
  • 3
  • 23
  • 33