使用字符串加正则表达式-Javascript搜索文件
我一直在写一个脚本,让用户按下一个按钮三次,一旦这三次都到了,按钮就会变灰,不可点击。我这样做是为了使按钮不能被点击(使用VBScript),但我想使用Javascript也灰显在这一点上 我要做的是让javascript搜索用户本地驱动器使用字符串加正则表达式-Javascript搜索文件,javascript,regex,vbscript,hta,Javascript,Regex,Vbscript,Hta,我一直在写一个脚本,让用户按下一个按钮三次,一旦这三次都到了,按钮就会变灰,不可点击。我这样做是为了使按钮不能被点击(使用VBScript),但我想使用Javascript也灰显在这一点上 我要做的是让javascript搜索用户本地驱动器“C:/sys/”上的文件,如果该文件存在,则灰显按钮 我遇到的问题是,我知道文件名中唯一正确的部分是最后两个字符“-3”。文件名的开头可以是任意数量的字母数字字符。我想我可以使用正则表达式来查找文件,但我不知道如何让脚本使用目录路径+正则表达式来搜索文件。代
“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”的文件,而不是使用正则表达式
请记住,这是一个更大的脚本的一部分,因此“fso”已经使用VBScript中的Set fso=CreateObject(“Scripting.FileSystemObject”)
定义
整个代码都可以找到
设置objShell=CreateObject(“WScript.Shell”)
设置fso=CreateObject(“Scripting.FileSystemObject”)
System=“HKEY\U CURRENT\U USER\Software\Microsoft\Windows\CurrentVersion\Policys\System\”
禁用任务管理器
dim currentDir:currentDir=objShell.CurrentDirectory'查找脚本当前目录'
'要做的事情:检查下面的用户名方法是否可以通过网络使用'
UserName=objShell.ExpandEnvironmentStrings(“%UserName%”)获取当前用户的用户名
pathToConfig=currentDir&“/config.ini”
pathToKeyFile=“C:/sys/”
'从配置文件获取当前版本号'
如果fso.FileExists(pathToConfig),则“确保配置文件存在”
设置ConfigFile=fso.GetFile(pathToConfig)'配置文件仅包含版本号'
如果ConfigFile.Size>0,则“这确保配置文件中设置了版本号”
设置versionFile=fso.OpenTextFile(pathToConfig,1)
Version=versionFile.ReadAll
其他的
EnableTaskMgr
window.close“如果无法访问配置文件,则脚本将提前结束,并在下次访问配置文件后用户登录时显示。”
如果结束
其他的
窗户关上
如果结束
出错时继续下一步
versionFile.close
'在C:/sys/中搜索密钥文件'
如果存在fso.files(pathToKeyFile&Version),则
EnableTaskMgr
窗户关上
其他的
如果结束
延期付款
ChkOne=pathToKeyFile&Version&“1”
ChkTwo=pathToKeyFile&Version&“2”
ChkThree=pathToKeyFile&Version&“3”
如果存在fso.files(ChkThree),则
“占位符代码”
MsgBox“使用的最大延迟尝试次数。您现在必须选择接受或拒绝策略。”
ElseIf fso.FileExists(ChkTwo)然后
'删除文件ChkTwo-创建文件ChkThree'
删除文件(ChkTwo)
设置objFile=fso.CreateTextFile(ChkThree)
窗户关上
ElseIf fso.FileExists(ChkOne)然后
'删除文件ChkOne-创建文件ChkTwo'
删除文件(ChkOne)
设置objFile=fso.CreateTextFile(ChkTwo)
窗户关上
其他的
“创建文件ChkOne”
设置objFile=fso.CreateTextFile(ChkOne)
窗户关上
如果结束
端接头
副禁用任务管理器
objShell.RegWrite系统,“REG_SZ”
objShell.RegWrite System&“/DisableTaskMgr”,1,“REG\u DWORD”
端接头
子启用任务管理器
objShell.RegWrite系统,“REG_SZ”
objShell.RegWrite System&“/DisableTaskMgr”,0,“REG\u DWORD”
端接头
“子登录”方法仅在用户单击“我接受”时运行
出错时继续下一步
调用EnableTaskMgr
如果没有(fso.FileExists(currentDir&“/store/”&LCase(用户名)&.csv)),则
出错时继续下一步
设置objFile=fso.CreateTextFile(currentDir&“/store/”&LCase(用户名)&“.csv”,True)
objFile.close
其他的
如果结束
设置objCSVFile=fso.OpenTextFile(currentDir&“/store/”&UserName&“.csv”,2,True)
写入(用户名&“,”现在&“,”版本)
objCSVFile.Close
出错时继续下一步
fso.CreateTextFile(“C:/sys/”&Version)
窗户关上
端接头
“子注销”方法仅在用户单击“我拒绝”时运行
objShell.Run“shutdown/l”
端接头
函数检查文件()
{
var fileLocation=“C:/sys/”
var regex=new RegExp('\w+\-3');
如果(fso.FileExists(fileLocation+regex)){
警报(“成功找到文件”);
}否则{
警报(“未找到文件”);
}
}
window.onload=检查文件;
是,将搜索“C:/sys/\w+\-3”
。相反,您可以从一个路径读取所有文件,然后使用RegExp筛选集合。
<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>