Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
1  
Are the dates the only place in the text which contains forward slashes "/" so a find and replace might work without messing up other formatting? –  CharlieRB Jul 31 '13 at 19:06
    
unfortunately no, there are other entries with forward slashes as well. But I guess if the only way to do it involves changing other slashes, I would say its ok, it is a log file after all...and the most important is the dates and a specific entry next to the date called "DATABASE SUCCESSFUL" –  DextrousDave Jul 31 '13 at 19:10
    
That's unfortunate. There are find and replace functions with PowerShell I thought might help. Here is a post on StackOverflow which may give you some inspiration in the right direction. –  CharlieRB Jul 31 '13 at 19:14

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
thank you. But cannot get this to work. I created the REPL.BAT with the code supplied in the link, and I but the code you provided here into another bacth file in the same folder. Running the batch file with your code inside, it only creates a new logfile which is empty...Why doesn't your code call or mention the REPL.BAT file? –  DextrousDave Aug 1 '13 at 14:06
    
@DextrousDave - It does - the results of TYPE are piped into REPL.BAT. The .BAT extension is implied. The script really should work as long as you modify the file environment variable to point to your actual file. –  dbenham Aug 1 '13 at 14:15
    
Well, you are absolutely right and I am an idiot. The whole time i was using yourfile.txt and it was yourfile.log...they both display as "TXT File". This works perfectly. My only concern is just, how accurate is it? Will there be any collateral damage? meaning won't it change any other unwanted text other than dates? –  DextrousDave Aug 1 '13 at 14:18
    
Also, is there anyway I can combine your script and the content of REPL.BAT ? For a more clean cut solution? –  DextrousDave Aug 1 '13 at 14:19
1  
As written, it will only modify strings anywhere in the file that look like "nnnn/nn/nn", where "n" is a digit. You could probably be more specific by incorporating "DATABASE SUCCESSFUL" into the search, but you haven't shown exactly what the line looks like. You should read up on regular expressions - there are loads of tutorials out there. You can make the search and replace very specific. I don't recommend combining the two scripts, since REPL.BAT is a powerful utility that could be used to solve many problems. If I were to combine, it would probably be pure JScript without all the options. –  dbenham Aug 1 '13 at 14:51

Use this code. It will step through each line of input.txt and output the updated lines to output.txt. It will look for lines with the current year and month (first of month, might have a problem if you're parsing last night's files). Only the lines that match will be altered so every / will become a -

@echo off
setlocal enabledelayedexpansion

    REM Works on any NT/2k machine independent of regional date settings
    REM courtesy of http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us
    SETLOCAL ENABLEEXTENSIONS
    if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
    for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
      for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
        set '%%a'=%%i
        set '%%b'=%%j
        set '%%c'=%%k))
    if %'yy'% LSS 100 set 'yy'=20%'yy'%
    set Today=%'yy'%-%'mm'%-%'dd'% 
    ENDLOCAL & SET v_year=%'yy'%& SET v_month=%'mm'%& SET v_day=%'dd'%

REM above stuff near beginning of code

REM Performs substitution only for strings that include this year and month.
    for /f "tokens=*" %%a in (input.txt) do (
        for /f "tokens=1" %%f in ('echo %%a ^| findstr %v_year% ^| find "%v_month%" /c') do set count=%%f
        if [!count!]==[1] (
            set outputline=%%a%
            set outputline=!outputline:/=-!
            echo.!outputline! >> output.txt
            REM output changed line
        ) else (
            echo.%%a >> output.txt
            REM output unchanged line
        )

    )  
share|improve this answer
    
how would you process this line by line? –  DextrousDave Jul 31 '13 at 19:59
    
Generally,, use the for command: ss64 for /f %%a in (input.txt) do ( ) –  Wally Jul 31 '13 at 20:35

If scripted solutions are not your thing (you're viewing the file in a decent text editor), try regular expressions in a find/replace operation:

Find ([1-2][0-9][0-9][0-9])/([0-1][0-9])/([0-3][0-9]) or
Find ([1-2]\d\d\d)/([0-1]\d)/([0-3]\d)

Replace with \1-\2-\3

For DD-MM-YYYY format, you can replace with \3-\2-\1.

Edit: This is a GUI solution. Example using Notepad2: Find/Replace using Regular Expression in Notepad2

Edit: Some text editors such as Notepad++ and UltraEdit have batch processing features where the above find/replace operation can be applied to many files with just one click.

share|improve this answer
    
OK cool. But with the code here, how do I call the text file from within the batchfile on which this code above should be executed on? –  DextrousDave Aug 1 '13 at 14:00
    
What code? There is no code in my answer. It's a GUI solution as I mentioned in it: you're viewing the file in a decent text editor. I will include a sample screenshot as an example in the answer. –  ADTC Aug 2 '13 at 3:20
    
Thank you...I see what you mean now, but now, I need an automated solution –  DextrousDave Aug 16 '13 at 9:39
    
Oh I see. Process a lot of files with similar data automatically? I suggest you mention it in the question. I would like to add that some text editors like Notepad++ have batch processing features where you can do find/replace for a whole bunch of files with one click. It's an almost-automated solution (you still need to setup the operation in the GUI). You can try it if it fits your bill. –  ADTC Aug 16 '13 at 10:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.