Vbscript 如何计算VB脚本的启动次数?
我想让我的VB脚本计算我启动它的次数,并将数字写入硬盘上的一个文件中——类似于批处理文件Vbscript 如何计算VB脚本的启动次数?,vbscript,Vbscript,我想让我的VB脚本计算我启动它的次数,并将数字写入硬盘上的一个文件中——类似于批处理文件 @echo off if not exist "C:\Users\User\Desktop\run.log" goto end if exist "C:\Users\User\Desktop\run.log" goto 123 :123 for /f "delims=" %%x in (C:\Users\User\Desktop\run.log) do set var=%%x Set /A result =
@echo off
if not exist "C:\Users\User\Desktop\run.log" goto end
if exist "C:\Users\User\Desktop\run.log" goto 123
:123
for /f "delims=" %%x in (C:\Users\User\Desktop\run.log) do set var=%%x
Set /A result = %var% + 1
echo %result% > "C:\Users\User\Desktop\run.log"
exit
:end
echo 1 > "C:\Users\User\Desktop\run.log"
exit
我试过下面的代码。第一次运行时,它工作正常,但之后:
你犯了很多错误。我已经用注释更新了代码,以使您了解流程。当然,一旦理解了流程,就可以改进代码 试试下面
Dim InputFile
Dim countText
Dim objFSO, oFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
InputFile = "C:\Users\User\Desktop\run.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
Set sum=“data+1”
不会做你认为它会做的事。是的,这很有效。我是VBS的新手,很抱歉我是个笨蛋!
Dim InputFile
Dim countText
Dim objFSO, oFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
InputFile = "C:\Users\User\Desktop\run.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