Setup Factory 9 - 检查文件中的字符串
Setup Factory 9 - Check File for String
我是 Lua 的 100% 新手,需要一种方法来检查我的 etc/hosts
文件中的字符串。我找到的其他帖子是处理字符串搜索和逐行读取文件的。
这是我的一些脚本,它是我在此处找到的一些示例的组合:
file = io.open("C:\Windows\System32\drivers\etc\hosts", "a")
function check(file)
if file.match(str, "nbs") then
file:close()
Screen.Next();
else
file:write("\n", %UserIP%, " nbs document")
file:close()
Screen.Next();
end
end;
如您所见,我正在文件 hosts
中搜索 sting nbs
。如果它存在,我想继续前进。如果没有,我计划在文件中附加一个新行。
当我将上面的内容输入 Lua Shell.
时,上面的内容似乎什么也没做
编辑 1: 添加完整脚本;这是完整的原始脚本 + 我的补充
-- These actions are performed when the Next button is clicked.
-- from _SUF70_Global_Functions.lua:
-- is the "Name:" field empty?
if(g_EditFieldIsEmpty(CTRL_EDIT_01)) then
-- the name field is empty...tell the user that it's a required field
-- and remain on this screen (don't advance to the next one)
-- "Invalid Entry"
local strTitle = SetupData.GetLocalizedString("MSG_INVALID_ENTRY");
-- get the label for the "Name:" edit field (since it may have been translated)
local strFieldName = DlgStaticText.GetProperties(CTRL_STATICTEXT_LABEL_01).Text;
-- strip off the trailing ":" (if present)
strFieldName = String.TrimRight(strFieldName, ":");
-- "The <fieldname> field cannot be empty."
local strPrompt = SetupData.GetLocalizedString("MSG_THE")
..strFieldName
..SetupData.GetLocalizedString("MSG_FIELD_CANNOT_BE_EMPTY");
Dialog.Message(strTitle, strPrompt, MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
else
--andrew you added the lines below
file = io.open("C:\Windows\System32\drivers\etc\hosts", "a")
function check(file)
if file.match(str, "test") then
file:close()
Screen.Next()
else
file:write("\n", "test")
file:close()
Screen.Next();
end
end;
-- the "Name:" field isn't empty...so
-- advance to the next screen
-- Screen.Next();
end;
您的代码创建了一个文件句柄并定义了一个函数 check()。
如果它应该做更多的事情,您就必须添加更多的内容。就像一个函数调用来检查。
调用检查后,您很可能会遇到脚本错误,因为您调用了本机 Lua.
中不存在的函数 file.match
file = io.open("C:\Windows\System32\drivers\etc\hosts", "a")
-- this loop will give you each line of the file as a string and you can use string.match
for line in file:lines() do
if line:match("nbs") then
print("I found one!!")
end
end
我相信你可以从这里拿走它。请参考https://www.lua.org/manual/5.3/manual.html#6.8
这最终成为我正在寻找的解决方案:
ip = SessionVar.Expand("%UserIP%")
result = TextFile.ReadToString(SessionVar.Expand("%SystemFolder%\drivers\etc\hosts"));
if string.match(result, "nbs") then
Screen.Next()
else
file = io.open("C:\Windows\System32\drivers\etc\hosts", "a")
file:write("\n", ip, " nbs document")
file:close()
Screen.Next()
end
在这种情况下,我使用的是内置函数的应用程序。这些似乎也使用 C,因此它们不能在 Lua shell.
中工作
我是 Lua 的 100% 新手,需要一种方法来检查我的 etc/hosts
文件中的字符串。我找到的其他帖子是处理字符串搜索和逐行读取文件的。
这是我的一些脚本,它是我在此处找到的一些示例的组合:
file = io.open("C:\Windows\System32\drivers\etc\hosts", "a")
function check(file)
if file.match(str, "nbs") then
file:close()
Screen.Next();
else
file:write("\n", %UserIP%, " nbs document")
file:close()
Screen.Next();
end
end;
如您所见,我正在文件 hosts
中搜索 sting nbs
。如果它存在,我想继续前进。如果没有,我计划在文件中附加一个新行。
当我将上面的内容输入 Lua Shell.
编辑 1: 添加完整脚本;这是完整的原始脚本 + 我的补充
-- These actions are performed when the Next button is clicked.
-- from _SUF70_Global_Functions.lua:
-- is the "Name:" field empty?
if(g_EditFieldIsEmpty(CTRL_EDIT_01)) then
-- the name field is empty...tell the user that it's a required field
-- and remain on this screen (don't advance to the next one)
-- "Invalid Entry"
local strTitle = SetupData.GetLocalizedString("MSG_INVALID_ENTRY");
-- get the label for the "Name:" edit field (since it may have been translated)
local strFieldName = DlgStaticText.GetProperties(CTRL_STATICTEXT_LABEL_01).Text;
-- strip off the trailing ":" (if present)
strFieldName = String.TrimRight(strFieldName, ":");
-- "The <fieldname> field cannot be empty."
local strPrompt = SetupData.GetLocalizedString("MSG_THE")
..strFieldName
..SetupData.GetLocalizedString("MSG_FIELD_CANNOT_BE_EMPTY");
Dialog.Message(strTitle, strPrompt, MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
else
--andrew you added the lines below
file = io.open("C:\Windows\System32\drivers\etc\hosts", "a")
function check(file)
if file.match(str, "test") then
file:close()
Screen.Next()
else
file:write("\n", "test")
file:close()
Screen.Next();
end
end;
-- the "Name:" field isn't empty...so
-- advance to the next screen
-- Screen.Next();
end;
您的代码创建了一个文件句柄并定义了一个函数 check()。
如果它应该做更多的事情,您就必须添加更多的内容。就像一个函数调用来检查。
调用检查后,您很可能会遇到脚本错误,因为您调用了本机 Lua.
中不存在的函数 file.matchfile = io.open("C:\Windows\System32\drivers\etc\hosts", "a")
-- this loop will give you each line of the file as a string and you can use string.match
for line in file:lines() do
if line:match("nbs") then
print("I found one!!")
end
end
我相信你可以从这里拿走它。请参考https://www.lua.org/manual/5.3/manual.html#6.8
这最终成为我正在寻找的解决方案:
ip = SessionVar.Expand("%UserIP%")
result = TextFile.ReadToString(SessionVar.Expand("%SystemFolder%\drivers\etc\hosts"));
if string.match(result, "nbs") then
Screen.Next()
else
file = io.open("C:\Windows\System32\drivers\etc\hosts", "a")
file:write("\n", ip, " nbs document")
file:close()
Screen.Next()
end
在这种情况下,我使用的是内置函数的应用程序。这些似乎也使用 C,因此它们不能在 Lua shell.
中工作