如何编写批处理文件/ vbscript以将当前日期和时间插入AssemblyInfo文件

时间:2013-08-26 09:07:11

标签: batch-file vbscript assemblyinfo

有人可以告诉我如何编写批处理文件/ vbscript以将当前日期和时间插入AssemblyInfo文件吗?

我有一个AssemblyInfo文件,我希望AssemblyDescription属性值是批处理文件的执行时间。

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

非常感谢提前!

3 个答案:

答案 0 :(得分:2)

经典方式:

@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

答案 1 :(得分:1)

这使用了来自 - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855

的名为repl.bat的帮助程序批处理文件

输入文件为"AssemblyInfo",并被新数据覆盖。

"c:\backup folder"更改为您的文件位置。

@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

答案 2 :(得分:0)

在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