I have the following VB script How to write this VB script with case syntax? In order to perform professi开发者_JAVA百科onal writing in place if then…. yael
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists("C:\file1 ")) Then
Verification=ok
Else
WScript.Echo("file1")
Wscript.Quit(100)
End If
If (fso.FileExists("C:\file2 ")) Then
Verification=ok
Else
WScript.Echo("file2")
Wscript.Quit(100)
End If
If (fso.FileExists("C:\file3 ")) Then
Verification=ok
Else
WScript.Echo("file3")
Wscript.Quit(100)
End If
. . . .
There are alternatives such as with
clause in VB (or vbs) or switch
in other languages, however those are used for a single given condition/var and then their value is checked but because you don't have to check for a single thing eg multiple file names C:\file1
, C:\file2
, etc, so it is not applicable to use them in this case.
As an another alternative, you can use a loop instead because file name number seems to be consistent in your code:
For i 1 To 3
If (fso.FileExists("C:\file" & i)) Then
Verification = ok
Else
WScript.Echo("file" & i)
Wscript.Quit(100)
End If
Next
So in all, the above code is shorthand of your code.
You can't use select/case for this type of thing, but there are other ways to condense or simplify the code.
First, reverse the test condition:
If Not (fso.FileExists("C:\file1 ")) Then
WScript.Echo("file1")
Wscript.Quit(100)
End If
This avoids needing a "do nothing" command after if/then.
Next, you can wrap things up in functions and subroutines to reduce the repetitive bulk of the code:
function TestFile(sFileName)
TestFile = fso.FileExists(sFileName)
end function
sub ErrorExit(sMessage, nCode)
WScript.Echo sMessage
WScript.Quit nCode
end sub
Then your series of tests becomes:
if not TestFile("c:\file1") then
ErrorExit "file1 not found", 100
elseif not TestFile("c:\file2") then
ErrorExit "file2 not found", 100
elseif not TestFile("c:\file3") then
ErrorExit "file3 not found", 100
end if
Though it may take you a quite a few more lines of code, you could implement a simple finite state machine to do this. Then instead of using an if/then construct you would be using some sort of switch statement with an enumerated set of "states" that you could possibly be in.
This details FSMs http://en.wikipedia.org/wiki/Finite-state_machine and they're easy to implement using enumerations and switch statements.
I just grabbed this of Google, here is a simple FSM implementation in C:
#include <stdio.h>
main()
{
int c;
START:
switch(c = getchar()){
case 'f' : goto F;
case 'b' : goto B;
case EOF : goto FAIL;
default: goto START; }
F:
switch(c = getchar()){
case 'o' : goto FO;
case EOF : goto FAIL;
default : goto START;}
FO:
switch(c = getchar()){
case 'o' : goto SUCCESS;
case EOF : goto FAIL;
default : goto START;}
B:
switch(c = getchar()){
case 'a' : goto BA;
case EOF : goto FAIL;
default : goto START;}
BA:
switch(c = getchar()){
case 'r' : goto SUCCESS;
case EOF : goto FAIL;
default : goto START;}
FAIL:
printf("Does not match.\n");
return;
SUCCESS:
printf("Matches.\n");
return;
}
精彩评论