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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable filename 1

Status
Not open for further replies.

RNF0528

MIS
Apr 4, 2006
115
US
I would like to set a variable in my script to = a file name in a folder.

for example.

C:\VBS\100.txt

I want to be able to change the filename to different names like 101.txt and 102.txt.

and have a variable be set to what ever the filename is in the folder c:\VBS.

Code for setting the variable i have.
ScreenVar = ("C:\VBS\*.txt")

End result would be setting the ScreenVar = 100.txt
 
The output in the log is:

PC: PCNAME, 10:23:03 AM, 2/20/2009, Screen Saver Version : C:\TEMP5\*


Here is my code below:

'Pictures go here : \\Server\Share$\ScreenSaver\Files

' Set Scripting Variables

Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")


ScreenVar = "C:\TEMP5\*"

'Log to server who is getting installed and what version

Set TextFile = FileSystem.OpenTextFile("\\Server\Share$\ScreenSaver\Log\ScreenSaver.log", 8,True)

Textfile.WriteBlankLines(1)

TextFile.Writeline "PC: " & WshNetwork.ComputerName & ", " & time & ", " & Date & ", Screen Saver Version : " & ScreenVar

'Reset it

Set FileSystem = Nothing
Set WshShell = Nothing
Set WshNetwork = Nothing
Set ScreenVar = Nothing

'End Script

Wscript.Quit


 
Would it be easier if i set the variable from the last line of a text file?
 
I would like to pull the variable from a filename.
 
I want to have the variable ScreenVar set to what ever file is in this folder. I will keep one file in the folder.

ScreenVar = "C:\TEMP5\*"
 
Something like this ?
For Each f In FileSystem.GetFolder("C:\TEMP5").Files
ScreenVar = f.Name
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi,
Here's my version:


ScreenVar = ""
if filesystem.folderexists("C:\TEMP5") then
files = filesystem.getfolder("C:\TEMP5").files
if files.count > 0 then
ScreenVar = files(0).name
end if
end if
if ScreenVar = "" then
ScreenVar = "default value"
end if
 
Ok so now the error message is wrong number of arguments or invalid property assignments:

line 12
char 5

here is the code

Code:
'Pictures go here : \\Server\Share$\ScreenSaver\Files 

' Set Scripting Variables

	Set FileSystem = CreateObject("Scripting.FileSystemObject")   
	Set WshShell = CreateObject("WScript.Shell")
	Set WshNetwork = CreateObject("WScript.Network")
	set files = CreateObject("Scripting.FileSystemObject")

ScreenVar = ""
if filesystem.folderexists("C:\TEMP6") then
    files = filesystem.getfolder("C:\TEMP6").files
    if files.count > 0 then
        ScreenVar = files(0).name
    end if
end if

if ScreenVar = "" then
    ScreenVar = "default value"
end if


'	ScreenVar = "C:\TEMP5\*"
'Log to server who is getting installed and what version
	
	Set TextFile = FileSystem.OpenTextFile("\\Server\Share$\ScreenSaver\Log\ScreenSaver.log", 8,True)
	
	Textfile.WriteBlankLines(1)
	
	TextFile.Writeline "PC: " & WshNetwork.ComputerName & ", " & time & ", " & Date & ", Screen Saver Version : " & ScreenVar

'Reset it

	Set FileSystem = Nothing
	Set WshShell = Nothing
	Set WshNetwork = Nothing
	Set ScreenVar = Nothing
         
'End Script
 
Seems like you didn't try my suggestion ...
What about this ?
Code:
Set FileSystem = CreateObject("Scripting.FileSystemObject")   
ScreenVar = "Not found"
For Each f In FileSystem.GetFolder("C:\TEMP6").Files
  ScreenVar = f.Name
Next
MsgBox ScreenVar

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you very much it worked! PHV once agian you answered another question for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top