Windows 启动文件检查失败 [英] File checking on Windows Startup is failing

查看:22
本文介绍了Windows 启动文件检查失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本,它允许我使用批处理文件每 5 分钟自动备份一次我的 MySQL 数据库.

Dim WshShell昏暗的 FSO昏暗的停止备份停止备份 = 假' 如果在打开此文件时未注册,则在 Windows 启动时注册.RegisterOnWindowsStartUp()' 保持每 5 分钟备份一次数据库,循环即可.做真事Set FSO = CreateObject(Scripting.FileSystemObject")If fso.FileExists("auto_backup.bat") Then ' 检查用于备份数据库的 bat 文件是否存在.MsgBox备份消息测试".' 运行处理数据库自动备份的批处理文件,使其不可见.Set WshShell = CreateObject("WScript.Shell")WshShell.Run chr(34) &auto_backup.bat"&铬(34), 0设置 WshShell = 无WScript.Sleep 300000 ' 每 5 分钟延迟循环一次.Else ' 停止循环,当 bat 文件不存在时不再继续.WScript.Echo无法自动备份数据库,这将不再继续."停止备份 = 真RemoveFromRegistry() ' 在 Windows 启动时取消注册此文件,因为 bat 文件不再存在.万一If stopBackup Then ' 当 stopBackup 变为真时中断循环Exit Do '在这里打破循环.万一环形' 在 Windows 启动时从注册表中删除此脚本函数 RemoveFromRegistry()Set objShell = Wscript.CreateObject("Wscript.Shell")objShell.RegDeleteHKCU\Software\Microsoft\Windows\CurrentVersion\Run\autobackup_key"结束函数' 仅当未注册时才在 Windows 启动时注册此脚本.函数 RegisterOnWindowsStartUp()如果 IsRegistryExist = False 那么Set WshShell = CreateObject("WScript.Shell")keyNameLocation = "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\autobackup_key";valueFileLocation = WScript.ScriptFullNamekeyType = "REG_SZ";WshShell.RegWrite keyNameLocation, valueFileLocation, keyType设置 WshShell = 无万一结束函数' 检查 Windows 启动时是否存在注册表项.函数 IsRegistryExist()Dim sKey, bFoundskey = "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\autobackup_key";与 CreateObject(WScript.Shell")on error resume next ' 关闭错误捕获sValue = .regread(sKey) ' 读取尝试bFound = (err.number = 0) ' 测试成功on error goto 0 ' 恢复错误捕获以如果 bFound 那么IsRegistryExist = 真别的IsRegistryExist = False万一结束函数

允许我备份数据库的批处理文件的文件名是 auto_backup.bat 并且它工作正常并且没有问题,它位于上述脚本所在的同一目录中.>

问题是每次 Windows 启动时,它都无法检查 auto_backup.bat 是否存在,但是当我打开脚本并运行它时,它工作正常,没有问题.>

我的逻辑可能有问题,有人可以帮我解决吗?

解决方案

问题是因为当你通过注册表在 Windows Startup 上注册你的脚本时,脚本会在 windows 启动时从 Windows Startup 的目录中执行.

>

为了获得可以在其中查找 auto_backup.bat 的原始工作目录,您可以组合 FileSystemObjectWScript.ScriptFullName> 函数获取当前脚本的父目录.

CreateObject(Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) &\auto_backup.bat"

那么循环中的 IF-ELSE 条件将是

<代码>...做真事Set FSO = CreateObject(Scripting.FileSystemObject")' (1) 添加此代码.autobackup_bat_file = CreateObject(Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) &\auto_backup.bat"If fso.FileExists(autobackup_bat_file) Then ' (2) 改变这一行.MsgBox备份消息测试".Set WshShell = CreateObject("WScript.Shell")WshShell.Run chr(34) &autobackup_bat_file &Chr(34), 0 ' (3) 更改此行.' 在这里继续你的一些代码.........

I have this script that allows me to automatically backup my MySQL Database every 5 minutes using a batch file.

Dim WshShell
Dim FSO
Dim stopBackup

stopBackup = false

' Register on Windows Startup if not registered when this file is opened.
RegisterOnWindowsStartUp()

' Keep backing up the database every 5 minutes, loop will do.
Do While True 
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists("auto_backup.bat") Then ' Check if bat file for backing up the database exist.
        MsgBox "Backup Message Test."
        ' Run the batch file which handle the auto backup of database, keep it invisible.
        Set WshShell = CreateObject("WScript.Shell") 
        WshShell.Run chr(34) & "auto_backup.bat" & Chr(34), 0 
        Set WshShell = Nothing
        WScript.Sleep 300000 ' Delay loop every 5 minutes.
    Else ' Stop the loop and do not proceed anymore when the bat file is not exist.
        WScript.Echo "Failed to auto backup the database, this won't continue anymore."
        stopBackup = true
        RemoveFromRegistry() ' Unregister this file on Windows Startup since the bat file is no longer exist.
    End If
    
    If stopBackup Then ' Break the loop when stopBackup become true
        Exit Do ' Break the loop here.
    End If
Loop

' Remove this script from registry on Windows Startup
Function RemoveFromRegistry()
    Set objShell = Wscript.CreateObject("Wscript.Shell")
    objShell.RegDelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\autobackup_key"
End Function

' Register this script on Windows Startup only if not registered.
Function RegisterOnWindowsStartUp()
    If IsRegistryExist = False Then
        Set WshShell = CreateObject("WScript.Shell")
        keyNameLocation = "HKCU\Software\Microsoft\Windows\CurrentVersion\Run\autobackup_key"
        valueFileLocation = WScript.ScriptFullName
        keyType = "REG_SZ"
        WshShell.RegWrite keyNameLocation, valueFileLocation, keyType
        Set WshShell = Nothing
    End If
End Function

' Check if Registry Key Exist on Windows Startup.
Function IsRegistryExist()
    Dim sKey, bFound
    skey = "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\autobackup_key"

    with CreateObject("WScript.Shell")
      on error resume next            ' turn off error trapping
        sValue = .regread(sKey)       ' read attempt
        bFound = (err.number = 0)     ' test for success
      on error goto 0                 ' restore error trapping
    end with
    
    If bFound Then
      IsRegistryExist = True
    Else
      IsRegistryExist = False
    End If
End Function

The filename of the batch file that allows me to back up the database is auto_backup.bat and it's working fine and no problem, it's on the same directory where the script above is located.

The problem is every time the Windows Startup, it fails to check for the existence of auto_backup.bat, but when I open the script and run it, its working fine and no issue.

There might be some issue with my logic, can anybody help me fix it?

解决方案

The problem is because when you register your script on Windows Startup through the registry, the script will be executed from the directory of Windows Startup when windows started.

In order to get the original working directory where you can look for the auto_backup.bat, you can combine the FileSystemObject and WScript.ScriptFullName functions to get the parent directory of the current script.

CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\auto_backup.bat"

Then the IF-ELSE condition inside your loop would be

...
Do While True 
Set FSO = CreateObject("Scripting.FileSystemObject")
 
' (1) Add this code.
autobackup_bat_file = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\auto_backup.bat"

If fso.FileExists(autobackup_bat_file) Then ' (2) Change this line.
    MsgBox "Backup Message Test."
    Set WshShell = CreateObject("WScript.Shell") 
    WshShell.Run chr(34) & autobackup_bat_file  & Chr(34), 0 ' (3) Change this line.

' Continue some of your code here...
...

这篇关于Windows 启动文件检查失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆