如何制作VB脚本计数它启动了多少次



我想使我的VB脚本计数我启动了多少次并将数字写入硬盘驱动器上的文件 - 该批处理文件类似的东西。

@echo off
if not exist "C:UsersUserDesktoprun.log" goto end
if exist "C:UsersUserDesktoprun.log" goto 123
:123
for /f "delims=" %%x in (C:UsersUserDesktoprun.log) do set var=%%x
Set /A result = %var% + 1
echo %result% > "C:UsersUserDesktoprun.log"
exit
:end
echo 1 > "C:UsersUserDesktoprun.log"
exit

我在下面尝试了此代码。在第一次运行时,它可以正常工作,但是之后却没有:

Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists("C:UsersUserDesktoprun.log") Then
        Dim InputFile
        Dim oFile
        Dim ReadAll
        Dim data
        InputFile = "C:UsersUserDesktoprun.log"
        Set oFile = objFSO.OpenTextFile(InputFile)
        data = oFile.ReadAll
        Set sum = "data + 1"
        oFile.Close
        objFSO.DeleteFile("C:UsersUserDesktoprun.log")
        outFile="C:UsersUserDesktoprun.log"
        Set objFile = objFSO.CreateTextFile(outFile,True)
        objFile.Write "sum"
        objFile.Close
    Else
        outFile="C:UsersUserDesktoprun.log"
        Set objFile = objFSO.CreateTextFile(outFile,True)
        objFile.Write "1"
        objFile.Close
    End If

您犯了多个错误。我已经使用注释更新了代码,以使您了解流程。当然,一旦了解流程,您就可以改进代码。

尝试以下

Dim InputFile
Dim countText
Dim objFSO, oFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
InputFile = "C:UsersUserDesktoprun.log"
If objFSO.FileExists(InputFile) Then
    'Read the file
    Set oFile = objFSO.OpenTextFile(InputFile, 1, True)
    countText = oFile.ReadLine
    If IsNumeric(countText) Then
        MsgBox "Numeric"
        Count = CInt(countText) + 1
    Else
        Count = 1
    End If
    oFile.Close
    Set oFile = Nothing
    'Overwrite the file
    Set oFile = objFSO.OpenTextFile(InputFile, 2, True)
    oFile.Write (CStr(Count))
Else
    Set oFile = objFSO.CreateTextFile(InputFile, True)
    oFile.Write "1"
End If
'Close and Remove from memory 
oFile.Close
Set oFile = Nothing
Set objFSO = Nothing

最新更新