Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

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

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
webdott
Asked:
webdott
  • 9
  • 3
  • 2
3 Solutions
 
webdottAuthor Commented:
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
 
plummetCommented:
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
 
webdottAuthor Commented:
thanks but that did not search and replace the text to sTextNew or "newclient"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
plummetCommented:
What did it do then?
0
 
plummetCommented:
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
 
webdottAuthor Commented:
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
 
webdottAuthor Commented:
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
 
Kamaraj SubramanianApplication Support AnalystCommented:
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
 
webdottAuthor Commented:
same thing - i just get a msgbox prior to the error
0
 
webdottAuthor Commented:
it copies everything, it just will not rewrite newclient text within clientinfo.txt with the new folder name
0
 
Kamaraj SubramanianApplication Support AnalystCommented:
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
 
webdottAuthor Commented:
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
 
webdottAuthor Commented:
you found it the same time i did :)
0
 
webdottAuthor Commented:
thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 9
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now