Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read directories for vbscript from text (config/param) file

Status
Not open for further replies.

Gaffi1

Technical User
Apr 23, 2004
70
0
0
US
Hi, i know that you can read information in from a text file using the FSO methods/properties. I'm not sure however if I can read from a text file a directory and pass it into a script.

For example, I have a script that works perfectly but the powers that be would like to add in some more functionality. For example, places in the script that currently specify a directory would instead read the directory from this text file which would be affectionatly called a config or param file. The reason behind this is instead of having to change the script if anything changes, they would just change the config file and the script would keep on running perfectly. I have read and its associated links but I haven't really seen anything up this ally. Thanks everyone!
 
Yes you can do that, but may I suggest that perhaps you would be better off creating a set of CONSTANTS at the top of your existing script along with code coments explaining what effect can be gained by chaning each const.

This gives the advantage of not having 2 separate files to maintain while retaining the ability to be modified by someone other than the original programmer.
 
Thanks Sheco,

I agree that it would be easier for it all to be in one place, but you know how it goes... ask and they shall receive, lol. Any chance someone could point me in the direction of how to do it? Is it as simple as reading in the open text file? I figured you would have to call a specific line or something for it to work, or is that not the case?

Much obliged!
 
i like the idea of keeping data and computation seperate, so i would have to agree with the powers at be...

you can consider plain txt, csv, ini file, xml file, database etc etc
If FSO.FileExists(Wscript.Arguments.Item(0)) Then
Set objTS = FSO.OpenTextFile(Wscript.Arguments.Item(0), 1, False)
Do While Not objTS.AtEndOfStream
strLine = ""
strLine = objTS.ReadLine
strLine = WshShell.ExpandEnvironmentStrings(strLine)
Call DoSomethingWithFolder(strLine)
Loop
objTS.Close
Set objTS = Nothing
End If
 
Thanks, got this to work via the following:
Config File
Code:
Dim strFolder 		'Sound Folder Location Variable
Dim qaFolder 		'QA Folder Location Variable
Dim objFolder 		'Current FSO Folder for Conversion portion of script
Dim colFolders 		'objFolder.SubFolders
Dim objSubFolders	'Sub Folders under colFolders
Dim subobjFile		'objSubFolders.Files
Dim objFiles		'Files under subobjFile
Dim strFile		'objFiles.Name
Dim strPath		'objFiles.Path
Dim maxAge		'Time (in Days) Required to Pass before a File can be converted
Dim strNewFile		'Original File Name converted to .wav extension
Dim strExtension	'Original File extension type, used to skip over wav files in the conversion process
Dim newpath		'Path for QA Copy of Wav file
Dim objQaFolder		'Current FSO Folder for QA portion of Script
Dim colQaFolders	'objQAFolder.SubFolders
Dim objQaSubFolders	'Sub Folders under colQaFolders
Dim subQaobjFile	'objSubQaFolders.Files
Dim cleanup			'Path for Cleanup Script
strFolder = "C:\soundtest\"
qaFolder = "C:\soundtest\qa\"
maxAge = .02
cleanup = "C:\scripts\killprocess.vbs"

Script
Code:
Option Explicit
Const ForReading = 1
Dim wshShell
Dim objFSO
Dim configInc
Dim strInc
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
WshShell.CurrentDirectory="c:\ConvUtil"
Set configInc = objFSO.OpenTextFile("c:\scripts\Autoconvconfig.parm", ForReading)
strInc = configInc.ReadAll
configInc.Close
Set configInc = Nothing
Execute strInc
Set objFolder = objFSO.GetFolder(strFolder)


ScanSubFolders(objFolder)

Sub ScanSubFolders(objFolder)
Set colFolders = objFolder.SubFolders

For Each objSubFolders In colFolders

	Set subobjFile = objSubFolders.Files
	For Each objFiles in subobjFile
		strFile = objFiles.Name
		StrPath = objFiles.Path
		strNewFile = Left(strPath,InStrRev(strPath,".")) & "wav"
		strExtension = Right(strFile,Len(strFile)-InStrRev(strFile,"."))
		newpath = qaFolder & Right(objSubFolders,len(objSubFolders)-13) & "\"
        	If strExtension <> "wav" Then
			If NOT objFSO.FileExists(strNewFile) Then
				If objFiles.DateLastModified < (date()-maxAge) Then
					if ObjFiles.size >= 5120 Then
						WshShell.Run "conv32 /it:rhet32 /ot:wav8 " & "" & strPath & "" & " " & "" & strNewFile & "",0,false
						wshshell.Run ("xcopy " & "" & strNewFile & "" & " " & "" & newpath & "" & " /Y")
					End If
				End If
			End If
		End If
	Next
Next
End sub

Set objQAFolder = objFSO.GetFolder(qaFolder)
Set colQAFolders = objQAFolder.SubFolders

For Each objQASubFolders In colQAFolders

	Set subqaobjFile = objQASubFolders.Files
	If subqaobjFile.Count = 0 then 
		wscript.echo "Empty QA Folder " & "" & objQaSubFolders & "" & "Being Deleted."
		objfso.DeleteFolder objQASubFolders, True
	End If
Next

WScript.Sleep 1800000
 
No offense, but that is crazy. You can tell the power's that be that I said so. At most I could see possibly dimming and setting the 4 variables in the config file. But dimming the vars that are used in the main script is just nuts. You have not seperated the data and computation layers at all. If anything this would be a candidate for a blatant attempt to outright confuse the situation.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
have to agree with EBGreen. i cant see that you have gained anything from your implementation, other than making things much harder to understand and support
 
If you want to "include" the "config file" (config.file, say) and any other files for that matter like named constants for ado or office etc, you should stuff them into a .wsf file.

The structure for this wsf file (conv.wsf, say) would be something like this.
[tt]
<job id="conv">
<script language="vbscript" src="config.file" />
<script language="vbscript">
'your script proper as shown
</script>
</job>
[/tt]
Then you can issue command at run or commandprompt window (supplement it with paths if needed) with host wscript or cscript.
[tt]
wscript //job:conv conv.wsf
[/tt]
 
Further note:
By "your script proper as shown", I certainly meant without the execute statement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top