Wednesday, December 25, 2019

windows - Converting Date Format with Batch file


I have a text file containing many date entries of the following format:


YYYY/MM/DD

How would you convert that to the following format?


YYYY-MM-DD

This file contain many date entries, so it should change the date format of all the entries...


Thanks


Answer



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)" "$1-$2-$3" >"%file%.new"
move /y "%file%.new" "%file%" >nul

The regular expression can be adjusted as needed to make the search and replace as specific as is needed.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...