Solved

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

Posted on 2011-09-18
14
385 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now