• 0

Replacing text in a non txt file


Question

I have a file abc.x and i want to replace a certain string within the file by another string. For eg, the file contents are :

Hello user

I want the "user" string to be replaced by the windows user accountname of the current running user. Like if my windows account name was Jack, it would be :

Hello Jack

Now i have two queries:

1. Firstly, the file i am referring to isnt a normal .txt file. However, its contents can be seen by opening it in notepad. Is it still possible to replace the string? If so, how

2. How can i obtain the current user account name?

Kindly answer these queries. examples would be of great help

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

We really need to know if it is a simple text file or not; the file extension is irrelevant. Simple text replacement is quite easy. Doing binary replacement is far more difficult than simply replacing some text, but replacing text in a binary-format file can be tricky. Can you upload an example file?

Getting the current username is easy. In C#:

string username = Environment.UserName;

Edited by boogerjones
Link to comment
Share on other sites

  • 0

IF it's just a text file, then this should work:

			string inputFile = "input.x";
			string outputFile = "output.x";
			string strToReplace = "replaceMe";
			string replacement = "replacement";
			File.WriteAllText(outputFile, File.ReadAllText(inputFile).Replace(strToReplace, replacement));

Link to comment
Share on other sites

  • 0

Here is a sample of a VBS script replacing the word USER, User, user with the UserName.

This was the file I used to test the script TestReplace.zyz

Before Script

Hello user

Lets us see how many USER are replace by the User Name.

After Script

Hello Ed

Lets us see how many Ed are replace by the Ed Name.

Save As Replace_UserText.vbs

Const ForReading = 1
Const ForWriting = 2
'-> Objects To Work With The File
 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Path To File Varible
 Dim File
 File = "TestReplace.zyz"
'-> User Name Varible
 Dim User 
  User = Act.ExpandEnvironmentStrings("%UserName%")
'-> File Contents Varible 
 Dim StrText
'-> Check To See If The File Exists
 If Fso.FileExists(File) Then
'-> Object To Work With The Text In The File
 Dim Ts 
  Set Ts = Fso.OpenTextFile(File,ForReading)
  Do Until Ts.AtEndOfStream
   StrText = StrText & Ts.ReadLine & vbCrLf
  Loop 
  Ts.Close 
'-> Replace The Word User With The User Varible
  If InStr(StrText,"user") Then
	StrText = Replace(StrText,"user",User)
	StrText = Replace(StrText,"User",User)
	StrText = Replace(StrText,"USER",User)
  End If
'-> Rebuild The File With New Info
   Set Ts = Fso.OpenTextFile(File,ForWriting,True)
	Ts.WriteLine  StrText
   Ts.close
'-> Check To See If Contents Where Updated
  CreateObject("Wscript.Shell").Run("Notepad " & Chr(34) & File & Chr(34)),1,True 
 End If

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.