Script to increment a version number in a file
I'开发者_开发知识库ve looked into several methods for automating the process of incrementing a version number stored in a file, but none are relevant enough (or I don't have enough knowledge of the particular language) to be of help.
I have a file (it's an NSIS installer script, if you're curious) with the line
!define REVISION "10"
When I increment the version, I need a script which replaces that line with
!define REVISION "11"
So the script needs to locate the file (it will be in the same directory as the script), find the existing !define
line, convert the number and add one to it, then replace the line with the new number. Anyone already solved this problem and want to share?
It would be great if the script could run just on windows, but Python or AutoHotKey or similar languages would be ok too.
This is one of those issues that's a huge pain, but finding a solution is taking me way too long.
I wrote this perl script for school... it allows you to search for a term in all of the files located in a directory. It doesn't do the replacement that you want but you could easily achieve that with a regex statement. This is a not a complete solution to your problem but it should be a big help.
`# 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";
精彩评论