1

How do you convert the date-time format of an entry inside a logfile to be the same as the date-time format of the file itself?

Example:

logfile.txt has timestamp:

'date modified' 2013-07-31 03:15

has the following entry inside it

2013/07/31 03:15 DATABASE SUCCESSFULLY COMPLETED

Using a batch script, how do I convert the 2013/07/31 to 2013-07-31 inside the logfile?

And just out of curiosity, is it possible to change the format of the date-time of the Windows OS? Eg. the timestamp you see on files and folders that are either YYYY/MM-DD or YYYY-MM-DD ?

David Van Staden
  • 1,619
  • 7
  • 33
  • 50
  • where are you looking at the file date? I don't see the format you show from `dir`, from a file properties window, in Explorer or anywhere. Anyway, on other OSes you use sed from your script to do this. Windows doesn't have such tools built in but you can install them. While you're at it you can install a decent shell so you don't have to use bat scripts at all. – bames53 Jul 31 '13 at 18:47
  • Yes, that is the time format as seen using the dir command. decent shell? Any examples? – David Van Staden Jul 31 '13 at 18:49
  • Maybe it depends on the country or something, because the format I see from dir is MM/DD/YYYY, not YYYY/MM/DD. Converting from YYYY/MM/DD to YYYY-MM-DD is a simple matter of `s:/:-:g` with sed. To convert from MM/DD/YYYY you'd need a more complicated substitution: `s:(\d{2})/(\d{2})/(\d{4}):\3-\1-\2:`. As for a decent shell, I use bash despite its idiosyncrasies. I've also tried [fish](http://fishshell.com/) and it seems like a good attempt at rational shell syntax. – bames53 Jul 31 '13 at 19:53
  • Thank you. How do you execute s:/:-:g? Could you perhaps give me an example? – David Van Staden Jul 31 '13 at 20:05
  • What's with all the overly complex solutions? Am I missing something? – MDEV Aug 01 '13 at 10:16

3 Answers3

2

It's really quite a nice, easy syntax. If you've got the line in a variable, it needn't be a complex solution:

set theline=%theline:/=-%

This is of course as stated above, if you've already got the line in question into a variable. If this isn't just for one time use and needs to be written back into the original file, more code is of course required.

MDEV
  • 10,240
  • 1
  • 29
  • 49
  • But that doesn't entirely answer the question. You need to show how to get lines of a text file into a variable. And how to only replace `/` in dates and ignore other instances of `/`. Then there are potentially all sorts of complications like poison characters within the text file, etc. etc. etc. – dbenham Aug 01 '13 at 14:28
  • @dbenham As I said "if you've got the line in a variable" i.e. during the required for loop or whatever.. Plus the OP showed us the line `2013/07/31 03:15 DATABASE SUCCESSFULLY COMPLETED` has no other forward slashes in it - hence me not worrying about the implicit replacement in my answer – MDEV Aug 01 '13 at 14:30
  • But there may be other lines that have `/`. Plus you need to show how to get the modified value back into the file. Environment variable search and replace is a viable technique to use in developing a solution. But it is far from a complete solution in and of itself. Someone that doesn't know a lot about batch programming wouldn't have a clue how to apply it in this situation. – dbenham Aug 01 '13 at 14:38
  • @dbenham I know there could be other lines, as I've now said twice, this code is for "if you've got the line in a variable". I will update the answer to make it clearer that this is a partial solution. – MDEV Aug 01 '13 at 14:46
  • Well, despite the possible shortcomings of your code as dbenham pointed out, your solution 1. Works 2. Answers my direct question. So thank you for that. Also, @dbenham, thank you for the input, appreciate – David Van Staden Aug 01 '13 at 18:31
1

Very easily done using a hybrid JScript/batch utility called REPL.BAT that performs regex search and replace on stdin and writes result to stdout. The utility is pure script that runs on any Windows version from XP onward; no exe download required. REPL.BAT is available here. Full documentation is embedded within the script.

Assuming REPL.BAT is either in your current directory, or better yet, somewhere within your PATH:

@echo off
set "file=yourFile.txt"
type "%file%" | repl "^(\d{4})/(\d\d)/(\d\d .*DATABASE SUCCESSFULLY COMPLETED)" "$1-$2-$3" >"%file%.new"
move /y "%file%.new" "%file%" >nul
Community
  • 1
  • 1
dbenham
  • 119,153
  • 25
  • 226
  • 353
  • thank you very much. this works well especially for many entries. The reason why I gave Smokey the points is because he answered my question moe specifically - How do I change the single entry, especially if you look at it from a variable perspective. Your answer however, nontheless, is very useful and very good, so thatnk you for that. I am definitely going to use it as well – David Van Staden Aug 01 '13 at 18:34
0

The format used by the date command and in the %DATE% and %TIME% variables is governed by the system's regional settings.

You can convert a date YYYY/MM/DD to YYYY-MM-DD like this:

@echo off

setlocal EnableDelayedExpansion

set "logfile=C:\path\to\logfile.txt"

(for /f "tokens=1*" %%l in (%logfile%) do (
  set d=%%l
  echo.!d!|findstr /r "^[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]$" >nul && set d=!d:/=-!
  echo.!d! %%m
)) >"%logfile%.new"

del "%logfile%"
move "%logfile%.new" "%logfile%"
Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278