1.

Solve : VBScript: how to get the value from a function?

Answer»

Code: [Select]Dim datapath,filename,x,objFSO,myFile,objFile,strReadFile,outFile,objOutFile
datapath="C:\"
keyword="nsc"

Set objFSO = CreateObject("Scripting.FileSystemObject")

myFile = tempFilePath(datapath,keyword)

file = objFSO.GetFile(myFile)
path = file.Path

outFile = path&"nscnew.ini"
Set objFile=objFSO.OpenTextFile(myFile,1)
Set objoutFile=objFSO.OpenTextFile(outFile,2,True)
strReadFile=Replace(strReadFile, "10.23.96.46","10.23.96.98")
objoutFile.Write(strReadFile)
objoutFile.Close
file.Delete
objFSO.MoveFile outFile,myFile
Set objOutFile=Nothing
Set objFile=Nothing
Set objFSO=Nothing

Function tempFilePath(datapath,keyword)
On error resume next
  Set fso=createobject("scripting.filesystemobject")
  If fso.FolderExists(datapath) Then
     Set firstsub=fso.GetFolder(datapath)
     For each testfolder in firstsub.SubFolders
         If instr(1,testfolder.name,keyword,1)<>0 Then
            strpath=datapath&"\"&testfolder.name
            tempFilePath strpath,keyword
         End If
     Next
     For each testfile in firstsub.Files
         If instr(1,testfile.name,keyword,1)<>0 Then
            filetype=fso.GetExtensionName(testfile.path)
            If filetype="ini" Then
               tempFilePath=testfile.path
               wscript.echo tempFilePath
            End If
         End If
     Next
  End If
  Set firstsub=Nothing
  Set fso=Nothing
End Function

the function tempFilePath is for finding the path of an .ini file with the name started with 'nsc'

by the instruction wscript.echo tempFilePath, we know it return a correct result,

but no matter how many times i tried, i didn't get this value when I CALL this function in my "main" function..................

Can somebody find the problem?



[recovering disk space, attachment deleted by admin]This is the start of your script. Try it on your computer.

Code: [Select]Dim datapath,filename,x,objFSO,myFile,objFile,strReadFile,outFile,objOutFile
datapath="C:\"
keyword="nsc"

Set objFSO = CreateObject("Scripting.FileSystemObject")

myFile = tempFilePath(datapath,keyword)
Quote from: foxidrive on July 02, 2014, 04:36:15 AM

This is the start of your script. Try it on your computer.

Code: [Select]Dim datapath,filename,x,objFSO,myFile,objFile,strReadFile,outFile,objOutFile
datapath="C:\"
keyword="nsc"

Set objFSO = CreateObject("Scripting.FileSystemObject")

myFile = tempFilePath(datapath,keyword)

Excuse me?   What have  you changed??? How should i test, just with the codes you give?? Quote from: mangolzy on July 02, 2014, 06:28:22 AM
Excuse me?   What have  you changed??? How should i test, just with the codes you give??

I changed nothing.  It generates an error on my machine, which means your script won't even RUN past those lines.On line 32 tempFilePath is recursing on itself but doing nothing with the result. I suspect the line should be changed to:

Code: [Select]tempFilePath = tempFilePath(strpath,keyword)

It might also be desirable to have early exits within each if statement (Exit Function) IMMEDIATELY after setting the function result.





Discussion

No Comment Found