1

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!

user2717436
  • 775
  • 1
  • 10
  • 23

3 Answers3

2

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
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • It's seems like a good solution, thanks. but I found it hard to understand where exactly I should specify my path of the AssemblyInfo file. I tried to insert it where you state "file" and "newfile", but it didn't work. Could you help me with this too? – user2717436 Aug 26 '13 at 10:53
1

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
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • It's seems like a good solution, but I found it hard to understand where exactly I should specify my path of the AssemblyInfo file. I tried to insert it where you state "AssemblyInfo" with no tmp extension, but it didn't work. Could you help me with this too? – user2717436 Aug 26 '13 at 10:45
0

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328