-
Batch help
Hi,
I need help with making a batch file.
It needs to:
1. Search for a string in a text document.
2. Make it become commented out.
3. Temporarily replace it with a new string.
4. Replace it with the old string.
Or else it could do:
1. Make a new command that does the same as another command.
Thank you for any help provided,
Rileyh
-
Would be extremely difficult in a batch file.
This could be easily done in a small program. Here is a sample in C#
I've actually adapted one of my programs on the fly so it's not guaranteed to be typo-free...
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace FixFile
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 4)
{
Console.Write("Invalid parameters count " + args.Length.ToString() + ", expected 4");
return;
}
string sInputFile;
string sOutputFile;
string sSearchString;
string sCommentString;
sInputFile = args[0];
sOutputFile = args[1];
sSearchString = args[2];
sCommentString = args[3];
TextReader reader;
TextWriter writer;
string sInputLine;
string sOutputLine;
try
{
writer=new StreamWriter(sOutputFile, false);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
try
{
reader=new StreamReader(sInputFile);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return;
}
while ((sInputLine = reader.ReadLine()) != null)
{
sOutputLine = sInputLine.Replace(sSearchString, sCommentString+sSearchString);
writer.WriteLine(sOutputLine);
}
writer.Flush();
writer.Close();
reader.Close();
Console.WriteLine("Program execution completed.");
}
}
}
Usage: FIXFILE.EXE source.txt destination.txt "search this string" "REM "