Replace date string in text file in Windows

I get a text file that contains a date string, what I want to do is execute a string replacement on the string at specified time each day. If there is any general purpose programming language environment like Python or PHP available it will be super easy, scripting such a simple task in those language is a piece of cake. But I don't have this environment and it seems overkill to setup such an environment for such a simple task.

I want to use a simple bat file and fews lines of code to make it work. The first thing is to find a command line tool that can do the string replacement. It looks Windows has no such a built-in tool for such a simple task. I tried fart.exe but I never used it before, I failed to qiuckly pick it up and use it to do what I want to do. But I'm familiar with the unix tool sed. To use sed in Windows, get the following three files: sed.exe, cygintl-3.dll, cygiconv-2.dll, cygwin1.dll, save them to windows\system32 folder. And a few lines of bat code do the job

 
@echo off
set year=%date:~0,4%
set month=%date:~5,2%
set day=%date:~8,2%
 
sed -i -e "s/..\/node_2/%day%\/node_2/" D:\www\node\index.htm
 

It replace a string like the following

 
03/node_2
 

to string like this

 
04/node_2
 

Suppose today is the fourth day of the month. The option -i will perform the search and replace in file, other wise sed will just output the replaced string to console.

Create a new task schedule on Windows and set up the execution time and set the bat file as executable program and we're done.

You may encounter two problems when using this command, the first is

 
sed: cannot rename ./sedFZnkAn: Invalid cross-device link
 

The reason is the disk driver the file is located must be the same as the execution environment of sed. The solution is to switch to the disk driver the file belongs before execute sed command as below

 
d:
sed -i -e "s/..\/node_2/%day%\/node_2/" D:\www\node\index.htm
 

There is another problem when the file is in an IIS folder or a folder has file ownership requirement. Because with the -i option, sed actually is not doing the so called inplace replacement, it will create a temp file and overwrite the original file with the temp file, this lead to a hideous side effect, the file ownership information is lost.

If the file is in IIS folder, the change of the file ownership makes it unaccesible from browser. In my case I have to add the required file owner back to the file with the command icacls command, here is an example

 
icacls D:\www\node\index.htm /grant myblog:(D,WDAC,RX)
 

Check out the ownership information of other file in IIS folder to find the right owner, it seems the owner name is the same as the website name listed in IIS Admin panel.

This will be OK for execution in command line, however, when the bat file is invoked from Windows Task Scheduler, another problem arises: the sed command can cause the file to be write protected and read-only which not happens when executed in just command line window, the solution is add the owner "Everyone" to the file as below:

 
d:
sed -i -e "s/..\/node_2/%day%\/node_2/" D:\www\node\index.htm
attrib -r D:\www\node\index.htm
icacls D:\www\node\index.htm /grant myblog:(D,WDAC,RX)
icacls D:\www\node\index.htm /grant Everyone:(D,WDAC,RX)
attrib -r D:\www\node\index.htm