how to write a batch file / vbscript to insert the current date and time into an AssemblyInfo file

1.9k views Asked by At

Can anyone please show me how to write a batch file / vbscript to insert the current date and time into an AssemblyInfo file?

I have a AssemblyInfo file, and I want the AssemblyDescription attribute value to be the time that the batch file was executed.

[AssemblyTitle="MyFile"]
[AssemblyDescription=""]
[AssemblyVersion="1.1.0"]

Many thanks in advance!

3

There are 3 answers

0
Ansgar Wiechers On BEST ANSWER

It's easier to do this in VBScript:

filename = "..."

Set fso = CreateObject("Scripting.FileSystemObject")

Set re = New RegExp
re.Pattern = "\[AssemblyDescription="".*?""\]"
re.IgnoreCase = True

txt = fso.OpenTextFile(filename).ReadAll
txt = re.Replace(txt, "[AssemblyDescription=""" & Now & """]")
fso.OpenTextFile(filename, 2).Write txt
1
foxidrive On

This uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855

The input file is "AssemblyInfo" and it is overwritten with the new data.

Change "c:\backup folder" to your file location.

@echo off

pushd "c:\backup folder"

for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

set datestamp=%YYYY%%MM%%DD%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

type "AssemblyInfo" |repl "^(.AssemblyDescription=).*" "$1\x22%fullstamp%\x22]" mx >"AssemblyInfo.tmp"
move "AssemblyInfo.tmp" "AssemblyInfo" >nul
popd
1
Endoro On

the classic way:

@ECHO OFF &SETLOCAL
FOR /f %%a IN ('^<"file" find /c /v ""') DO SET "lines=%%a"

SETLOCAL ENABLEDELAYEDEXPANSION
<file (
FOR /l %%a IN (1,1,%lines%) DO (
    SET "line="
    SET /p "line="
    IF "!line:AssemblyDescription=!" neq "!line!" (
        SET "line=[AssemblyDescription="!date! !time!"]"
    )
    ECHO(!line!
))>newfile
ENDLOCAL