kb2487 kb2487 - 3 months ago 9
PowerShell Question

Update part of text in document

I have a

.xal
document which can be opened with notepad just fine. Below is the part of it that is important to me:


...

magV=1

invert=0

text=01000000237843141114052693183714

[Pri1]

enabled=0

...


I would like to create a script, which would auto replace part of text
140526
in line 128 (it is some old date - 26.05.2014) and replace it with the current date.

For example: today it should be
160816
, tommorrow there should be
160817
, etc.

As said before text is in line 128, characters are from 24 to 30.

If searched around (SED for WIN, VBA, Powershell,...) but i didn't find anything that could work.

Thank you all in advance!

Answer

Here a solution using PowerShell:

$content = Get-Content 'your_file_path'
$content[127] = $content[127] -replace '(.{24}).{6}', ('${{1}}{0}' -f (Get-Date).ToString('yyMMdd'))
$content | Set-Content 'your_file_path'