Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6980903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:05:12+00:00 2026-05-27T18:05:12+00:00

I have a file on which I want to replace the dates but I

  • 0

I have a file on which I want to replace the dates but I don’t know what dates are on it and I want to do generic code for me to use the same executable for other files.

I put all content of the file in a string and I want to replace all dates with format dd/mm/yyyy (ex: 19/12/2011) with the actual date (20/12/2011).


How can I do that?


Code so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ReplaceDates
{
    class Program
    {
        static int Main(string[] args)
        {
            string FileIn = retreiveArgument(args, "i");
            int AddDays = Int32.Parse(retreiveArgument(args, "d"));
            string date = (System.DateTime.Now).AddDays(AddDays).ToString("dd/MM/yyyy");

            string content = "", date2replace = "";

            if (File.Exists(FileIn))
            {
                File.Copy(FileIn, FileIn + ".bkp", true);
                try
                {
                    content = File.ReadAllText(FileIn);

                    // here is what I need to do
                }
                catch (Exception)
                {
                    Console.WriteLine("Error replacing dates in " + FileIn + ".");
                    return 0;
                }
                try
                {
                    File.WriteAllText(FileIn, content);
                    Console.WriteLine("Dates replaced in " + FileIn + ".");
                    return 0;
                }
                catch (Exception)
                {
                    Console.WriteLine("Couldn't write the file " + FileIn);
                    return 2;
                }
            }
            else
            {
                Console.WriteLine("File " + FileIn + " does not exist.");
                return 1;
            }
        }



        private static string retreiveArgument(string[] argument, string argumentName)
        {
            for (int i = 0; i < argument.Length; i++)
            {
                if (argument[i].ToLower().Equals("-h") || argument[i].ToLower().Equals("help") || argument[i].ToLower().Equals("-help") || argument[i].Equals("?") || argument[i].Equals("-?"))
                {
                    Console.WriteLine("Usage : ");
                    Console.WriteLine("ReplaceDates.exe -i [Input File] -d [Addition]");
                    Console.WriteLine("[Input File] -> Complete path to the file.");
                    Console.WriteLine("[Addition] -> Adds the specified value in days to the actual date.");
                    Console.WriteLine();
                    Console.WriteLine("This executable replaces the data from the input file.");
                }
                else
                {
                    if (argument[i].Equals("-" + argumentName))
                    {
                        return argument[i + 1].Trim();
                    }
                }
            }
            return "";
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:05:13+00:00Added an answer on May 27, 2026 at 6:05 pm

    Here’s a solution using a MatchEvaluator. Tweak the regular expression if desired;

    string UpdateDates(string input)
    {
        return Regex.Replace(input, @"\d{2}/\d{2}/\d{4}", m => DateTime.Now.ToString("dd/MM/yyyy"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a file txt which contains music lyrics. I want to replace
I have a text file which I want to filter using awk. The text
I have a MySQL file which I want to import via PHP 5. In
I have this file file.txt which I want to split into many smaller ones.
I have a binary file to which I want to append a chunk of
I have a huge tab separated file which I want to sort on its
I have an array of file names which I want to download. The array
I have a jad file for a BB app which I want to run
I have an AVI file, which I want to re-encode to fit on a
I have an ActiveX control (an OCX file) which raises an event. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.