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
BalaSeeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
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
0
BalaSeeAuthor Commented:
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....
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

BalaSeeAuthor Commented:
@ 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
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
sungenwangCommented:
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

0
BalaSeeAuthor Commented:
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
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Doesn't explain anything, and the set.txt file is empty :P
0
BalaSeeAuthor Commented:
@ 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  
---------------------------
0
BalaSeeAuthor Commented:
@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
0
BalaSeeAuthor Commented:
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
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
My code in http:#a37298657 (still) does that.
0
BalaSeeAuthor Commented:
@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 ....

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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?
0
sungenwangCommented:
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

0
BalaSeeAuthor Commented:
Hi sungenwang:

Ur script is working good.... Instead of Vlog, I need search for *.env and replace all the *.env in a file...
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
0
sungenwangCommented:
Try the code below, which will replace only the *.env files with the new found env file.
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 = "\w+.env"
	objRegEx.IgnoreCase = True
	objRegEx.Global = True
	
	Set objMatches = objRegEx.Execute(strData)
	For Each objMatch in objMatches
		strOutput = objMatch.Value
		strData = Replace(strData, strOutput, strFoundFile)

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.