Solved

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

Posted on 2011-09-18
14
384 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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks but that did not search and replace the text to sTextNew or "newclient"
0
 
LVL 10

Expert Comment

by:plummet
Comment Utility
What did it do then?
0
 
LVL 10

Assisted Solution

by:plummet
plummet earned 300 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 23

Expert Comment

by:Kamaraj Subramanian
Comment Utility
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
Comment Utility
same thing - i just get a msgbox prior to the error
0
 

Author Comment

by:webdott
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
you found it the same time i did :)
0
 

Author Closing Comment

by:webdott
Comment Utility
thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
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…
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…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

771 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

10 Experts available now in Live!

Get 1:1 Help Now