打开简单文本文件时出错

时间:2015-12-28 12:03:17

标签: vbscript

我有以下代码:

 #!/home/ben/software/install/bin/perl
        use warnings;
        use strict;
        use Email::Send;
    use Email::Send::Gmail;
    use Email::Simple::Creator;

    my $email = Email::Simple->create (
        header => [
            From    => '@gmail.com',
                To      => '@gmail.com',
                Subject => 'Make it simple',
        ],

        body => 'buddy I got it ...',
    );

    my $sender = Email::Send->new 
    ({
            mailer      => 'Gmail',
            mailer_args => 
[
                username => '',
                password => '',
        ]
    });

    $sender->send ($email);

当我运行此脚本时,它工作正常。但是,当我把这段代码放在我的500行大VBScript中时,它在行上给我一个错误

Option Explicit
Dim objFSO, logfolder, ManualFile2, ManualCopyFiles, checkfile
Set objFSO = CreateObject("Scripting.FileSystemObject") 
logfolder = "C:\Users\TEMPPAHIR\LearnVB\Logfolder\"
ManualFile2 = objFSO.BuildPath(logfolder, "ManualFoldListReq.txt")
If objFSO.FileExists(ManualFile2) Then
    Set checkfile = objFSO.GetFile(ManualFile2) 
    If checkfile.Size > 0 Then
        Set ManualCopyFiles = objFSO.OpenTextFile(ManualFile2)
        Wscript.echo "error number is : " & Err.number
        Wscript.echo " File size is greater than zero "
    Else
        Wscript.echo " File size is zero "
    End IF
Else
    Wscript.echo " File does not exists " 
End If

如下。我写了一个sub,它以下面的格式将错误抛出到日志文件中。

Set ManualCopyFiles = objFSO.OpenTextFile(ManualFile2)

令人惊讶的是,当它是大脚本的一部分时,同一段代码无效。谁能帮助我找到这里到底出了什么问题?

1 个答案:

答案 0 :(得分:3)

为什么要在上面的代码块中插入这一行?在你的例子中根本没有使用它。

Set ManualCopyFiles = objFSO.OpenTextFile(ManualFile2)

如果有使用该文件的目的,请显示。

如果该用法遵循.GetFile方法,那么您可以使用.OpenAsTextStream方法打开文件进行读/写/或附加。

在这里,这更简单,并为变量使用更多标准名称。试试吧。

Option Explicit
Dim FSO, objFile, strLogFolder, strManualFile2

Set FSO = CreateObject("Scripting.FileSystemObject") 

strLogFolder = "C:\Users\TEMPPAHIR\LearnVB\Logfolder\"
strManualFile2 = strLogFolder & "ManualFoldListReq.txt"

If FSO.FileExists(strManualFile2) Then
    Set objFile = FSO.GetFile(strManualFile2) 
    If objFile.Size > 0 Then
        'Parameters ref  http://www.devguru.com/technologies/vbscript/14044
        objFile.OpenAsTextStream(1,-2)
        '... there should not be an error here
        Wscript.echo "error number is : " & Err.number
        Wscript.echo " File size is greater than zero "
    Else
        Wscript.echo " File size is zero "
    End IF
Else
    Wscript.echo " File does not exists " 
End If