|
Answer» Hello forum lovers,
I've searched the forums but could not find a way for this issue..
Can I use a batch file to replace a specific string in a bunch of one-line txt FILES that sits in the same directory?
I'd like to change position 25 (length 6) to today's date (YYMMDD) in each txt file. How do I do it?
If batch won't do - would a vb script fix it? In that CASE I'd like to CALL the vb script from a batch file.
Any help appreciated This uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
Put repl.bat in the same folder as the batch file.
Test it with some sample files first.
Code: [Select]@echo off for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a" set "YY=%dt:~2,2%" & 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=%YY%%MM%%DD%"
for /f "delims=" %%a in (' dir /b /a-d *.txt ') do ( type "%%a"|repl "(.{24})......(.*)" "$1%datestamp%$2" >"%%a.tmp" move "%%a.tmp" "%%a" ) Awesome foxidrive!
That fixed it. Thanks for your tireless efforts to help out.
The solution provided by foxidrive works great.
As I go along the learning and experience curve I've found two things I'd like to solve:
1) I'd like to avoid having the batch files in the same folder as the files I want to process. I tried to find a way to solve this but failed. Is there a way to TELL repl.bat to use another folder for input/output?
2) I only want to replace the string where it is not equal to today's date. Assume that I want to process a file today (on Nov 4th). I only want to replace the string to today's date if the date in pos 25 is less than today's date.
Any help appreciated! Place repl.bat anywhere on the PATH STATEMENT and it will work without changing the batch file.
c:\windows and c:\windows\system32 are already on the path and you can add a new folder on the path for your utility files. Like c:\util
type echo %path% and you will see the contents of the path variable - it is searched from start to finish for the executable you try to launch, after searching the current folder. If the executable is found then away it goes.
As for changing the files - if it's putting in todays date then also changing files with todays date doesn't seem to be an issue
|