Solved

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

Posted on 2011-09-18
14
387 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

810 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