Link to home
Start Free TrialLog in
Avatar of BalaSee
BalaSee

asked on

Need to get input from text file and replace in another

Hi,

In one of the server, I have a file called set.env and it need to searched ...then the name need to replaced in a batch file which is located in my local desktop.

or

I can copy the file to my local desktop and the file name with .env should be replaced in a batch file
Avatar of Bill Prew
Bill Prew

It's not clear what you are really after here.  Can you provide some examples of the ENV file, and the BAT file, before and after the desired changes are made?

~bp
Avatar of BalaSee

ASKER

OK fine....

I have a copy in q:\ drive
q:\test\1.txt
q:\test\2.txt
q:\test\3.txt
q:\test\set.env
I need to copy that set.env to my local d:\ drive

Then I have a batch file with a dos command exsist....
here: vlog -BL -<cc5d6mk15.env> *.*-arc

So I should not enter or feed any name. It should get the *.env file and replace it in my bat file....
Something like this? It searches for any .env file in the d:\ root folder, but you can change that to your needs easily:
@echo off
for %%F in (D:\*.env) do vlog -BL -<%%F> *.*-arc 

Open in new window

%%F might contain the drive and path portion. If you need to remove that, use %%~nxF instead of %%F in the vlog cmd.
Avatar of BalaSee

ASKER

@ Qlemo:

The file name need to fetched from the drive d:\test\set.env

this part is saved as: test.bat
vlog -BL -<cc5d6mk15.env> *.*-arc

Need to replace the <cc5d6mk15.env> with set.env in the batch file
Exactly that is done (if we remove the <> from my code line). If you have
   d:\set.env
   d:\noset.env
the batch executes
  vlog -BL -set.env *.*-arc
  vlog -BL -noset.env *.*-arc

Of course you should only have one .env file there; the above is just for demonstration purpose.
Here's a vbscript that will do the 2 parts you asked for:

1. Call SearchFile(strSearchFile, strDestinationFolder)
The above line searches your computer for "set.env" and copies it to D:\

2. Call ReplaceBatchLine(strBatchFile)
The above line replaced your test.bat file from the original "*.env" to "set.env" or whatever file you defaultl to search for.

You can change the settings by modifying these lines:
strDestinationFolder = "D:\"
strBatchFile = "D:\test.bat"
strSearchFile = "set.env"

sew

strDestinationFolder = "D:\"
strBatchFile = "D:\test.bat"
strSearchFile = "set.env"

Call SearchFile(strSearchFile, strDestinationFolder)
Call ReplaceBatchLine(strBatchFile)
msgbox "done"


Function SearchFile(strSearchFile, strDestinationFolder)
	Set objFSO = CreateObject("Scripting.FileSystemObject")

	strFileName = objFSO.GetBaseName(strSearchFile)
	strExtensionName = objFSO.GetExtensionName(strSearchFile)

	strComputer = "."
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colFiles = objWMIService.ExecQuery("Select * From CIM_Datafile where FileName = '" & strFileName & "' and Extension = '" & strExtensionName & "'")

	For Each objMatchFile in colFiles
		strOutput = objMatchFile.Name
		objFSO.CopyFile strOutput, strDestinationFolder
	Next
End Function


Function ReplaceBatchLine(strBatchFile)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(strBatchFile, 1, True)
	
	strData = objFile.ReadAll
	
	objFile.Close
	Set objFile = Nothing
	
	Set objRegEx = New RegExp

	objRegEx.Pattern = "<.*\.env>"
	objRegEx.IgnoreCase = True
	objRegEx.Global = True
	
	Set objMatches = objRegEx.Execute(strData)

	For Each objMatch in objMatches
		strOutput = objMatch.Value
		
		strData = Replace(strData, strOutput, "<" & strSearchFile & ">")

		Set objOutFile = objFSO.OpenTextFile(strBatchFile, 2, True)
		objOutFile.Write(strData)
		objOutFile.Close
	Next
End Function

Open in new window

Avatar of BalaSee

ASKER

Really Sorry Qlemo....I know its hard...

I have 2 files
1. in my drive one env file will be available

2. text file -- it will contain text\command called "vlog -BL -cc515.env *.*-arc"
First it should get the value of the env file from local drive and the replace the env file name inside the text file:

3. save the text as batch file command.txt
set.txt
Doesn't explain anything, and the set.txt file is empty :P
Avatar of BalaSee

ASKER

@ sungenwang:
---------------------------
Windows Script Host
---------------------------
Script:      D:\Balaji\16122011\test\searchenv.vbs
Line:      22
Char:      3
Error:      Permission denied
Code:      800A0046
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
Avatar of BalaSee

ASKER

@Qlemo: I dont want any name inside the file, only the file name should be placed in my batch file...


Note: File name can vary depends on migration... It can be set.env for next project it can set.env
what *.env file I am getting in my d drive that should be replaced in my batch file
Avatar of BalaSee

ASKER

I have 2 file attached.
1. batch file where the *.env should be replaced with set.env for this one (it might change for next project)
2. set.env file will vary depends on project

Need batch or vbscript to replace the *.env with set.env or any other env file....
batch-file.txt
set-env-file.txt
My code in http:#a37298657 (still) does that.
Avatar of BalaSee

ASKER

@QLemo I may be poor at coding...Can you pls help me know how to do that..SOrry for troubling

1. save one file as batch file with the content --  "vlog -BL -cc515.env *.*-arc"
2. create a batch / vbscript which can search for env file and and replace the above batch file "-cc515.env" with the current env filename....

can u pls save and explain more how to save and run the same....


thanks a lot ....

What I posted in http:#a37298657 is your batch file. Besides copying the .env file you have nothing else to do than to execute the batch file, it will execute with the appropriate file name.

Or are you really after getting a ready-to-use, filled-in batch file?
I think I understand what you're trying to do. The vbscript below will search a given path ("d:\test") for any occurance of a file with an ENV extension. When it finds it, it will replace the *.ENV name with the new file name inside the batch file (d:\test.bat)

Right now I assume the batch file is d:\test.bat. You can change the settings by modifying these two lines.

strSearchFolder = "d:\test"
strBatchFile = "d:\test.bat"

sew


strSearchFolder = "d:\test"
strBatchFile = "d:\test.bat"


strFoundFile = SearchFile(strSearchFolder, "ENV")

If strFoundFile <> "" Then
	Call ReplaceBatchLine(strBatchFile, strFoundFile)
End If
msgbox "done"


Function SearchFile(strSearchFolder, strExtension)
	Set objFSO = CreateObject("Scripting.FileSystemObject")

	Dim objCurrentFolder, objFile, objFolder
	Set objCurrentFolder = objFSO.GetFolder(strSearchFolder)

	For Each objFile In objCurrentFolder.Files
		strExtensionName = UCase(objFSO.GetExtensionName(objFile))
		
		If strExtensionName = strExtension Then
			SearchFile = objFSO.GetFileName(objFile)
			Exit Function
		End If
	Next

	For Each objFolder In objCurrentFolder.subFolders
		SearchFile = SearchFile(objFolder.ParentFolder & "\" & objFolder.name, strExtension)
	Next
End Function


Function ReplaceBatchLine(strBatchFile, strFoundFile)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(strBatchFile, 1, True)
	
	strData = objFile.ReadAll
	
	objFile.Close
	Set objFile = Nothing
	
	Set objRegEx = New RegExp

	objRegEx.Pattern = "vlog\s-BL\s-.*\.env"
	objRegEx.IgnoreCase = True
	objRegEx.Global = True
	
	Set objMatches = objRegEx.Execute(strData)
	For Each objMatch in objMatches
		strOutput = objMatch.Value
		strData = Replace(strData, strOutput, "vlog -BL -" & strFoundFile)

		Set objOutFile = objFSO.OpenTextFile(strBatchFile, 2, True)
		objOutFile.Write(strData)
		objOutFile.Close
	Next
End Function

Open in new window

Avatar of BalaSee

ASKER

Hi sungenwang:

Ur script is working good.... Instead of Vlog, I need search for *.env and replace all the *.env in a file...
I still don't get it. You do not need to replace the template file name in your batch file. My solution is dynamic. Just try it out, and you will see.
ASKER CERTIFIED SOLUTION
Avatar of sungenwang
sungenwang
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial