how can I check if a file exists?
Start with this: Set fso = CreateObject(“Scripting.FileSystemObject”) If (fso.FileExists(path)) Then msg = path & ” exists.” Else msg = path & ” doesn’t exist.” End If Taken from the documentation.
Start with this: Set fso = CreateObject(“Scripting.FileSystemObject”) If (fso.FileExists(path)) Then msg = path & ” exists.” Else msg = path & ” doesn’t exist.” End If Taken from the documentation.
Use the ‘And’ keyword for a logical and. Like this: If Not ((filename = testFileName) And (fileName <> “”)) Then
I’ve only ever seen CopyFile fail with a “permission denied” error in one of these 3 scenarios: An actual permission problem with either source or destination. Destination path is a folder, but does not have a trailing backslash. Source file is locked by an application.
Handling Errors A sort of an “older style” of error handling is available to us in VBScript, that does make use of On Error Resume Next. First we enable that (often at the top of a file; but you may use it in place of the first Err.Clear below for their combined effect), then before … Read more
Note – jump to the UPDATE 2014-04-27 section at the bottom of this answer for the best solution. I used to think the answer was no. But then DosTips user Liviu discovered that the <SUB> character (Ctrl-Z, 0x1A, decimal 26) has bizare effects when embedded within a batch file. If functions somewhat like a line … Read more
Try: WScript.Quit n Where n is the ERRORLEVEL you want to return
You can create a (relatively) small function in each file that you want to include other files into, as follows: sub includeFile (fSpec) dim fileSys, file, fileData set fileSys = createObject (“Scripting.FileSystemObject”) set file = fileSys.openTextFile (fSpec) fileData = file.readAll () file.close executeGlobal fileData set file = nothing set fileSys = nothing end sub and … Read more
Your suggestion would work, but using a Do loop might be a little more readable. This is actually an idiom in C – instead of using a goto, you can have a do { } while (0) loop with a break statement if you want to bail out of the construct early. Dim i For … Read more
Set objFSO=CreateObject(“Scripting.FileSystemObject”) ‘ How to write file outFile=”c:\test\autorun.inf” Set objFile = objFSO.CreateTextFile(outFile,True) objFile.Write “test string” & vbCrLf objFile.Close ‘How to read a file strFile = “c:\test\file” Set objFile = objFS.OpenTextFile(strFile) Do Until objFile.AtEndOfStream strLine= objFile.ReadLine Wscript.Echo strLine Loop objFile.Close ‘to get file path without drive letter, assuming drive letters are c:, d:, etc strFile=”c:\test\file” s … Read more
You can use WScript.ScriptFullName which will return the full path of the executing script. You can then use string manipulation (jscript example) : scriptdir = WScript.ScriptFullName.substring(0,WScript.ScriptFullName.lastIndexOf(WScript.ScriptName)-1) Or get help from FileSystemObject, (vbscript example) : scriptdir = CreateObject(“Scripting.FileSystemObject”).GetParentFolderName(WScript.ScriptFullName)