用于增加文件中版本号的脚本

时间:2011-01-20 22:43:52

标签: windows automation

我已经调查了several methods来自动增加存储在文件中的版本号的过程,但没有一个是足够相关的(或者我没有足够的特定语言知识)来帮助

我有一个文件(这是一个NSIS安装程序脚本,如果你很好奇)的行

!define REVISION "10"

当我增加版本时,我需要一个用

替换该行的脚本
!define REVISION "11"

因此脚本需要找到该文件(它将与脚本位于同一目录中),找到现有的!define行,转换该数字并向其中添加一个,然后用新的行替换该行数。任何人都已经解决了这个问题并希望分享?

如果脚本只能在Windows上运行,那将会很棒,但Python或AutoHotKey或类似的语言也可以。

这是一个非常痛苦的问题之一,但找到解决方案的过程太长了。

1 个答案:

答案 0 :(得分:0)

我为学校写了这个perl脚本...它允许你在目录中的所有文件中搜索一个术语。它不会进行您想要的替换,但您可以使用正则表达式声明轻松实现。这对您的问题不是一个完整的解决方案,但它应该是一个很大的帮助。

`# calling the perl interpreter indirectly on the next line but executable file` `permissions must be set`
#! /usr/local/bin/perl

$searchTerm = $ARGV[0]; # this is the term we will search for and ARGV[0] represents first argument that is placed in the command line

$directory = $ARGV[1]; # this is the directory we would like to search for our term in
                   # ARGV[1] referes to the arguments that represents the sec-
                       # ond argument's place in the command line

$extension = $ARGV[2]; # allows the user to specify the extension

print "\n\nChecking all files in ".$directory." with ".$extension." extension for ".$searchTerm."...\n";

@fileInDir = <$directory*.$extension>; #takes the directory appends a * wildcard and the extension

foreach $fileInDir (@fileInDir) { 

open("IN", "<".$fileInDir) or die ("File " .$fileInDir." - Not Found.\n"); #open allows you to use readline

#print "Searching for \"$searchTerm\"\n\n";

$ctr = 0; # initialize the counter

    while ($searchLineOfText = readline(IN)) { #readline(IN) evaluates to false when the end of file, EOF, character is reached

        if ($searchLineOfText =~ /$searchTerm/) { # if the line we're searching contains the search term
        print $searchLineOfText;  # we're looking for than the line the term is found in is printed
        $ctr++;
        }
    }
    if ($ctr > 0) {
        print "\nTerm \"$searchTerm\" has been found $ctr time(s) in ".$fileInDir."\n----------------------------------------------------\n\n";
    }
}
print "program terminated\n\n";