Solved

vb script that changes text within a .txt file when it creates the new folder and files

Posted on 2011-09-18
14
389 Views
Last Modified: 2012-05-12
i have a script that creates a new folder and copies in some .txt files
it allows me to name the new folder.

this is what it does:
create new folder - i name it C:\new\inamethisfolder
copy's folder and subfolders C:\clients\  example: has clientinfo.txt
inside the clientinfo.txt file contains 4 places that has the text - newclient

is there anyway to do the following:
when the new folder is created with the name i provide in the popup from the script,
and it copies over the .txt file from C:\clients\new\clientinfo.txt
it can search the clientinfo.txt file and replace any word that is "newclient" and
change it to the name i provide in the script?

it may not be possible, but thought i would ask.

thanks
0
Comment
Question by:webdott
  • 9
  • 3
  • 2
14 Comments
 

Author Comment

by:webdott
ID: 36556640
forgot the script i have
newclient.vbs

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

client = "C:\client\" + InputBox("instructions", "popup title", "prefill in box")

If Not objFSO.FolderExists(client) Then 
objFSO.CreateFolder (client)
objFSO.CopyFolder "c:\clients",(client)
End If

Open in new window

0
 
LVL 10

Accepted Solution

by:
plummet earned 300 total points
ID: 36556857
You could do this with the filesystemobject, something like this:

dim objFSO
dim sClient
dim sFullPath

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

sClient = InputBox("instructions", "popup title", "prefill in box")
sFullPath = "C:\client\" & sClient

If Not objFSO.FolderExists(sFullPath) Then 
    objFSO.CreateFolder (sFullPath)
    objFSO.CopyFolder "c:\clients",sFullPath
End If

dim objTxtFile
dim sTextOld
dim sTextNew

Set objTxtFile = objFSO.OpenTextFile(sFullPath & "\clientinfo.txt", ForReading)

sTextOld = objTxtFile.ReadAll
objTxtFile.Close
sTextNew = Replace(sTextOld, "newclient", sClient)

Set objTxtFile = objFSO.OpenTextFile(sFullPath, ForWriting)
objTxtFile.WriteLine sTextNew
objTxtFile.Close

Open in new window


I hope that helps.
0
 

Author Comment

by:webdott
ID: 36557361
thanks but that did not search and replace the text to sTextNew or "newclient"
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:plummet
ID: 36557382
What did it do then?
0
 
LVL 10

Assisted Solution

by:plummet
plummet earned 300 total points
ID: 36557456
Try adding these constants in at the beginning of the script (before the dim statements):

Const ForReading = 1
Const ForWriting = 2

Open in new window


Hopefully it will work this time...
0
 

Author Comment

by:webdott
ID: 36564471
i get this and error that says

Script: C:\new\newclient.vbs
Line: 28
Char: 1
Error: Permission denied
Code: 800A0046
Source: Microsoft vbscript runtime error

i made sure that the text.txt file gives all including admin rights to the .txt

:here is everything i have :
 
Const ForReading = 1
Const ForWriting = 2

dim objFSO
dim sClient
dim sFullPath

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

sClient = InputBox("instructions", "popup title", "prefill in box")
sFullPath = "C:\client\" & sClient

If Not objFSO.FolderExists(sFullPath) Then 
    objFSO.CreateFolder (sFullPath)
    objFSO.CopyFolder "c:\test\new",sFullPath
End If

dim objTxtFile
dim sTextOld
dim sTextNew

Set objTxtFile = objFSO.OpenTextFile(sFullPath & "\clientinfo.txt", ForReading)

sTextOld = objTxtFile.ReadAll
objTxtFile.Close
sTextNew = Replace(sTextOld, "newclient", sClient)

Set objTxtFile = objFSO.OpenTextFile(sFullPath, ForWriting)
objTxtFile.WriteLine sTextNew
objTxtFile.Close

Open in new window

0
 

Author Comment

by:webdott
ID: 36564475
i get the new folder and the file copy, but in the clientinfo.txt  anything with
"newclient" does not get replaced with the new folder name.

example: when the popup comes up to create the new folder, i type in john

everything in clientinfo.text that has newclient in it does not get changed to john.

thanks
0
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 36564488
can you add msgbox and check whether the file name is correct or not and path is correct or not.

sTextNew = Replace(sTextOld, "newclient", sClient)  
msgbox sFullPath
Set objTxtFile = objFSO.OpenTextFile(sFullPath, ForWriting)  

Open in new window

0
 

Author Comment

by:webdott
ID: 36564510
same thing - i just get a msgbox prior to the error
0
 

Author Comment

by:webdott
ID: 36564512
it copies everything, it just will not rewrite newclient text within clientinfo.txt with the new folder name
0
 
LVL 23

Assisted Solution

by:Kamaraj Subramanian
Kamaraj Subramanian earned 200 total points
ID: 36564519
when you are writing you are not giving the file name

Set objTxtFile = objFSO.OpenTextFile(sFullPath, ForWriting) 

change it to

Set objTxtFile = objFSO.OpenTextFile(sFullPath & "\clientinfo.txt", ForWriting) 

Open in new window

0
 

Author Comment

by:webdott
ID: 36564530
Found it :)

it was on line 28: the Path was missing  & "\clientinfo.txt",

Set objTxtFile = objFSO.OpenTextFile(sFullPath & "\clientinfo.txt", ForWriting)

it now works = awesome thanks for all your help!

this is it:
 
Const ForReading = 1
Const ForWriting = 2

dim objFSO
dim sClient
dim sFullPath

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

sClient = InputBox("instructions", "popup title", "prefill in box")
sFullPath = "C:\client\" & sClient

If Not objFSO.FolderExists(sFullPath) Then 
    objFSO.CreateFolder (sFullPath)
    objFSO.CopyFolder "c:\test\new",sFullPath
End If

dim objTxtFile
dim sTextOld
dim sTextNew

Set objTxtFile = objFSO.OpenTextFile(sFullPath & "\clientinfo.txt", ForReading)

sTextOld = objTxtFile.ReadAll
objTxtFile.Close
sTextNew = Replace(sTextOld, "newclient", sClient)
msgbox sFullPath  
Set objTxtFile = objFSO.OpenTextFile(sFullPath & "\clientinfo.txt", ForWriting)
objTxtFile.WriteLine sTextNew
objTxtFile.Close

Open in new window

0
 

Author Comment

by:webdott
ID: 36564532
you found it the same time i did :)
0
 

Author Closing Comment

by:webdott
ID: 36564536
thanks
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question