使用字符串加正则表达式搜索文件 - Javascript

时间:2016-01-29 11:10:19

标签: javascript regex vbscript hta

我一直在编写一个脚本,让用户按下按钮三次,一旦三次按下,按钮将变灰并且不可点击。我已经做到了这个按钮无法点击(使用VBScript)但我想在这一点上使用Javascript也灰显。

我想要做的是让javascript搜索用户本地驱动器"C:/sys/"上的文件,如果该文件存在则灰显按钮。

我遇到的问题是我知道文件名的唯一部分是正确的是最后两个字符“-3”,文件名的开头可以是任意数量的字母数字字符。我以为我可以使用正则表达式来查找文件,但我不知道如何使用目录路径+正则表达式来获取脚本来搜索文件。代码示例:

function checkfile()
  {
    var fileLocation = "C:/sys/"
    var regex = new RegExp('\w+\-3');
    if(fso.FileExists(fileLocation + regex)) {
      alert("File found successfully");
    } else {
      alert("File not found.");
    }
  }
  window.onload = checkfile;

我认为这可能有用,但考虑到这一点让我想知道该脚本是否实际上是在搜索一个名为“C:/ sys / \ w + -3”的文件,而不是使用正则表达式。

请记住,这是一个更大的脚本的一部分,因此已经使用VBScript中的Set fso = CreateObject("Scripting.FileSystemObject")定义了“fso”。

可以找到整个代码here

<script language="VBScript"> 
    Set objShell = CreateObject("WScript.Shell")    
    Set fso = CreateObject("Scripting.FileSystemObject")
    System = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System\"
    DisableTaskMgr
    dim currentDir : currentDir = objShell.CurrentDirectory 'Finds the scripts current directory'
    'TO DO: Check whether UserName method below can be used over network'
    UserName = objShell.ExpandEnvironmentStrings("%USERNAME%") 'Grabs the username of the current user'
    pathToConfig = currentDir & "/config.ini"
    pathToKeyFile = "C:/sys/"


    'Gets the current version number from config file'
    If fso.FileExists(pathToConfig) Then 'Makes sure that the config file exists'
      Set ConfigFile = fso.GetFile(pathToConfig) 'Config file contains only the version number'
      If ConfigFile.Size > 0 Then 'This makes sure that there is a version number set inside the config file'
        Set versionFile = fso.OpenTextFile(pathToConfig, 1)
        Version = versionFile.ReadAll
      Else
        EnableTaskMgr
        window.close 'If config file cannot be reached then the script will end prematurely and will be shown next time the user logs in once the config file can be reached.'
      End If
    Else
      window.close
    End If
    On Error Resume Next
    versionFile.close

    'Searches C:/sys/ for keyfile'
    If fso.FileExists(pathToKeyFile & Version) Then
      EnableTaskMgr
      window.close
    Else
    End If

    sub Postpone
    ChkOne = pathToKeyFile & Version & "-1"
    ChkTwo = pathToKeyFile & Version & "-2"
    ChkThree = pathToKeyFile & Version & "-3"

      If fso.FileExists(ChkThree) Then
        'Placeholder code'
        MsgBox "Maximum number of Postpone attempts used. You must now choose to either Accept or Decline the Policy."
      ElseIf fso.FileExists(ChkTwo) Then
        'Delete file ChkTwo - Create file ChkThree'
        fso.DeleteFile(ChkTwo)
        Set objFile = fso.CreateTextFile(ChkThree)
        window.close
      ElseIf fso.FileExists(ChkOne) Then
        'Delete file ChkOne - Create file ChkTwo'
        fso.DeleteFile(ChkOne)
        Set objFile = fso.CreateTextFile(ChkTwo)
        window.close
      Else
        'Create file ChkOne'
        Set objFile = fso.CreateTextFile(ChkOne)
        window.close
      End If
    end sub

    sub DisableTaskMgr
      objShell.RegWrite System, "REG_SZ"
      objShell.RegWrite System & "/DisableTaskMgr", 1, "REG_DWORD"
    end sub

    sub EnableTaskMgr
      objShell.RegWrite System, "REG_SZ"
      objShell.RegWrite System & "/DisableTaskMgr", 0, "REG_DWORD"
    end sub

    sub Logon 'Method only runs when user clicks "I Accept"'
      On Error Resume Next
      Call EnableTaskMgr
      If Not (fso.FileExists(currentDir & "/store/" & LCase(UserName) & ".csv")) Then
        On Error Resume Next
        Set objFile = fso.CreateTextFile(currentDir & "/store/" & LCase(UserName) & ".csv", True)
        objFile.close
      Else      
      End If
      Set objCSVFile = fso.OpenTextFile(currentDir & "/store/" & UserName & ".csv", 2, True)
      objCSVFile.Write(UserName & "," & Now & "," & Version)
      objCSVFile.Close 
      On Error Resume Next
      fso.CreateTextFile("C:/sys/" & Version)
      window.close   
    end sub

    sub Logoff 'Method only runs when user clicks "I Decline"'
      objShell.Run "shutdown /l"
    end sub
</script>

<script type="text/javascript">
  function checkfile()
  {
    var fileLocation = "C:/sys/"
    var regex = new RegExp('\w+\-3');
    if(fso.FileExists(fileLocation + regex)) {
      alert("File found successfully");
    } else {
      alert("File not found.");
    }
  }
  window.onload = checkfile;
</script>

0 个答案:

没有答案