Click here to Skip to main content
15,896,402 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have an intersting requirement , where i need to modify a batch file in the installtion folder
during setup for DB details , this batch file is part of installtion.
Can this be done anyway.


Thanks,
Shreyas
Posted
Comments
[no name] 25-Sep-13 10:21am    
Read the documentation for whatever software package you are using to create your setup and see if they have support for file writing operations.
Sergey Alexandrovich Kryukov 25-Sep-13 11:05am    
You are going to get mad skills. No, it won't make sense. But if you disagree, you need to explain the "requirement".
—SA

1 solution

i have done something similar before.
"get the value by using directoryinfo.getfiles(*.bat)" or something like this" u need the path to the file here
this is from memory so there may be little mistakes but this is how to do.
C#
string batlocation=".bat location";
string battxt = String.Empty;

if (File.Exists(batlocation))
{
using ( var reader = new StreamReader(batlocation))
{      
         battxt = reader.ReadToEnd();
         //now in battxt u have everything that was in bat file
}
}


now u can use some string replace or search or regex to change the bat file.

then if you would like to write changes over just

do like this
C#
if (File.Exists(batlocation))
{
//move the old .bat file for backup 
File.Move(batlocation)
//this move command is probably wrong google for move files.

using ( var writer = new StreamWriter(batlocation))
{      
         battxt = writer.Write(battxt)
         //now you write back to new bat file with your chagnes in there

}
}
 
Share this answer
 
v3
Comments
srastogi85 25-Sep-13 12:35pm    
Hi ,
thanks for reply ,
I wrote something similar here , but didnt work , can you point what can be the issue here

string filename="C:" + "\\x.bat";
string batlocation = @filename;

string battxt = String.Empty;


if (File.Exists(batlocation))
{
using (StreamReader reader = new StreamReader(batlocation))
{
battxt = reader.ReadToEnd();
}
}

battxt.Replace("UserName", dbUser);
battxt.Replace("Password", dbPassword);
battxt.Replace("Instance", dbInstance);



if (File.Exists(batlocation))
{
File.Delete(batlocation);

using (StreamWriter writer = new StreamWriter(batlocation,true))
{
writer.Write(battxt);
}
}
[no name] 25-Sep-13 13:46pm    
Because Replace returns a new string so you are just throwing the result away.
srastogi85 25-Sep-13 13:59pm    
Thanks it worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900