View Full Version : function for file.exist
threaded
08-17-2005, 12:37 PM
Ok, basically I have AMS doing
if File.DoesExist(CDrive.."blah") then
File.Run(CDrive.."blah", "/S", "", SW_MINIMIZE, true);
end
if File.DoesExist(sDrive.."blah") then
File.Run(sDrive.."blah", "/S", "", SW_MINIMIZE, true);
end
CDrive is cdrom and sDrive is systemdrive. I do this file.exist for around 50 files. I was hoping maybe setting up a function to do file.exist and switch between cdrive and sdrive and then set it to a variable so I could reduce how much is written, but Im stumped on how to set this up...basically I want a set variable to be the output. It need to search a set place on CDrive and sDrive and then output to the variable the exact location...so for instance, lets say I was doing this with 7zip.exe
function Blah (filename.exe)
if File.DoesExist(CDrive.."\\7zip.exe") then
variable = CDrive.."\\7zip.exe"
if File.DoesExist(sDrive.."\\7zip.exe") then
variable = sDrive.."\\7zip.exe"
and on page on show it would do somethig like this
Blah(7zip.exe)
File.Run(variable, "/S", "", SW_MINIMIZE, true)
any help is greatly appreciated.
TJ_Tigger
08-17-2005, 01:20 PM
You could try this
function Blah (filename.exe)
local variable = nil;
if File.DoesExist(CDrive.."\\7zip.exe") then
variable = CDrive.."\\7zip.exe";
elseif File.DoesExist(sDrive.."\\7zip.exe") then
variable = sDrive.."\\7zip.exe";
else
variable = nil;
end
return variable; --returns the variable
end
variable = Blah(7zip.exe)
File.Run(variable, "/S", "", SW_MINIMIZE, true)
threaded
08-18-2005, 01:21 PM
What about this?
function Blah (filename.exe)
local variable = nil;
if File.DoesExist(CDrive.."\\" .. filename.exe) then
variable = CDrive.."\\" .. filename.exe;
else
if File.DoesExist(sDrive.."\\" .. filename.exe) then
variable = sDrive.."\\" .. filename.exe";
else
variable = nil;
end
return variable; --returns the variable
end
then when I want to call the function for each executable, I would do this
Blah(7zip.exe)
File.Run(variable, "/S", "", SW_MINIMIZE, true)
right?
variable = Blah("7zip.exe")
File.Run(variable, "/S", "", SW_MINIMIZE, true)
TJ_Tigger
08-18-2005, 01:34 PM
Since the function you created returns the variable you want to make sure and assign a variable name for your function
variable = Blah("7zip.exe")
File.Run(variable, "/S", "", SW_MINIMIZE, true)
--or
File.Run(Blah("7zip.exe"), "/S", "", SW_MINIMIZE, true);
If the files are all listed in a table you could traverse the table using a for loop that would run that command for each file in the table
tFiles = File.Find("AutoPlay\\Docs", "*.exe", false, false, nil, nil);
if tFiles then
for index, strFilename in tFiles do
File.Run(Blah(strFilename), "/S", "", SW_MINIMIZE, true);
end
end
Tigg
TJ_Tigger
08-18-2005, 01:37 PM
No surprise there. Worm beat me to the punch. :wow
threaded
08-18-2005, 02:11 PM
I would love to create a table, but the switches change from program to program.
And I was using KillProcess and CopyFolder functions as a basis...and I notice in those they dont use the variable = KillProcess(sProcess), so i didnt think id need it, I guess I should have explained what I wrote a little more...for my purpose, Blah is KillProcess (its blah right now cuz I havent decided on a name) and filename.exe is sProcess (again, havent decided on the name for it yet...might just be sFilename)
PS ya know what, skip that paragraph...TJ_Tigger thank you very much, after reading the entire post, I notice its possible to write it in File.Run and save lines...so that what ill do...I appreciate everyones help in this as I am trying to cut down on how many lines this project consists of...right now (without this function), Im at around 675 lines of code (this is using wordpad to count the lines with no wordwrap)...so as you can see, its a pretty large project. This will save me 343 lines. so thank you TJ_Tigger and Worm
TJ_Tigger
08-18-2005, 02:40 PM
NP, hope it helps
If the processes are always the same you could build the table in your code, or in a file for that matter and have both the filename and switches for each program.
i.e.
tblPrograms = {}
tblPrograms.1 = {filename = "c:\\path\\to\\file\\filename.exe", switch = "/S"}
tblPrograms.2 = {filename = "c:\\path\\to\\file\\filename2.exe", switch = "/S"}
tblPrograms.3 = {filename = "c:\\path\\to\\file\\filename3.exe", switch = "/S"}
tblPrograms.4 = {filename = "c:\\path\\to\\file\\filename4.exe", switch = "/S"}
tblPrograms.5 = {filename = "c:\\path\\to\\file\\filename5.exe", switch = "/S"}
Then when you run that file you would do this.
for index, value in tblPrograms do
if Blah(tblPrograms[i]["filename"]) then
File.Run(tblPrograms[i]["filename"], tblPrograms[i]["switch'], "", SW_MINIMIZE, true);
else
Dialog.Message("Error", "File did not exist");
end
end
Tigg
for index, strFilename in tFiles do
File.Run(Blah(strFilename), "/S", "", SW_MINIMIZE, true);
end
threaded
08-18-2005, 03:10 PM
I have actually thought about the table...so my question is this, if I write all the apps to a textfile with switches, how do I get it to read the 1st portion to a table and then the second to another table so that it can use table1 for file and table2 for switches...the text file would look something like this
7zip.exe /S
bittornado.exe /S
tgf.exe /VERYSILENT /SP- /NORESTART
something like that is what i was thinking about, but I have no idea how to get it to use the textfile...I know there is TextFile.ReadToTable and that kin of thing, but it doesnt really explain about setting tabs in the textfile and if the first part can be put to one table and then a tab would signfiy parsing it to the 2nd table.
the reason for the textfile is that it would be changeable...some things woul get run, while others do not...I would have a full list backup, but the textfile it reads from could contain all or one, so I really wouldnt want the programs "coded" into the app.
Again, I cant tell you how much I appreciate the help. This is the first thing Ive done in AMS and I really dont know all that much about the program.
TJ_Tigger
08-18-2005, 03:29 PM
There are lots of ways you could do it. If you do use a text file I would suggest putting some unique delimiter between the filename and the executable, then you could us the DelimitedStringToTable function that is included with AMS.
You could then read the file to a table using the above specified action then break out the string into its different parts and check for the file then run if necessary.
If you don't want to use delimiters, you could search for a "/" for the start of the switches or the "exe" as the end of the file and grab (String.Right and String.Left) the rest from there.
HTH
Tigg
threaded
08-19-2005, 02:20 PM
I hate to do this, so im sorry, but I dont see anywhere on how to use delimitedstrings in the help file...I found that it seems to be a lua script, but i dont know how to enable the script or if it is always enabled and then i dont know exactly how to work through it. Is there any way you can give me some info on it, or help me on how to use it? I appreciate your help.
TJ_Tigger
08-20-2005, 09:02 AM
It is in the Gallery pane. Look for the scriplets section in the Gallery and you should find the Delimited string lua file. Just copy that code into your global functions and you then have use of the function in your project.
Tigg
threaded
08-26-2005, 05:38 PM
Ok, well i found it, but im not sure how to go about using it for this method...if i understand correctly, i first have to TextFile.ReadToTable, where the textfile has the name of the program, a delimiter, and the silent switches. Then I use DelimitedTableToString...this is where I seem to have problems...i just dont see where I get the variables from...I would need the program to be one variable and the switch to be a second variable...also, String.Right and String.Left require me to use a certain number of characters to read from, but with this type of situtation, the number of characters will always change...Im sorry, I know youre prolly tired of helping me, but maybe if you could give me a example, I could figure out the rest myself.
My text file so far reads something like this (as an example)
7zip.exe,/S
Progs.exe,/SP- /VERYSILENT /NORESTART
Hexeditor.msi,/qb-!
whatd be really neat is if i could include the directory in there, where the prog exists. like this
7zip\7zip.exe,/S
Progs\Progs.exe,/SP- /VERYSILENT /NORESTART
HexEditor\Hexeditor.msi,/qb-!
Again, Im sorry, and i appreciate your help.
TJ_Tigger
08-26-2005, 09:26 PM
Not a problem on the help. If you knew the number of questions that I asked when I first started and continue to ask ;)
Since you have just two items in the list you can use some simple String functions to extract your data. Here is an example that hopefully will work for you.
--Here is the contents of your text file. programs.txt
7zip\7zip.exe,/S
Progs\Progs.exe,/SP- /VERYSILENT /NORESTART
HexEditor\Hexeditor.msi,/qb-!
--this should extract the information you want from the file
tblReturn = TextFile.ReadToTable("AutoPlay\\Docs\\programs.txt");
if tblReturn then --if the table is valid then proceed
tblProgs = {}; --build a place to store the contents of the text file.
for i,v in tblReturn do --traverse each item in the table
--search for the position of the delimeter in the string.
nPos = String.Find(v, ",", 1, false);
if nPos ~= -1 then --the delimeter was found
tblProgs[i] = {};
tblProgs[i].Program = String.Left(v, nPos -1);--Get the program information
tblProgs[i].Switches = String.Mid(v, nPos +1, -1);--Get the switches
end
end
end
--Then if you wanted to run it
if tblProgs then
for i,v in tblProgs do
File.Run(tblProgs[i].Program, tblProgs[i].Switches, "", SW_MINIMIZE, true);
--You can use the following line if you need the switches to be in quotes
--File.Run(tblProgs[i].Program, string.format("%q", tblProgs[i].Switches), "", SW_MINIMIZE, true);
end
end
Hope that helps get you started.
Tigg
threaded
08-27-2005, 03:40 PM
thank you soo much...ok a couple of questions
I included a msi file in there just to see what your response on that was...but its ok, ill ask about it now and in accordance with this new script.
First things first...I would like to combing this with the FindDrive script, so Im assuming something like this would be correct..
File.Run(FindDrive(tblProgs[i].Program), tblProgs[i].Switches, "", SW_MINIMIZE, true);
also, what is the deal with the switches in quotes? do i need them to be in quotes? Im not sure...or if they arent does that mean they should be in quotes in the textfile?
Now for the msi portion (I may break the programs up and have one for executables and one for msi files)
Shell.Execute(FindDrive(tblProgs[i].Program), "open", tblProgs[i].Switches, "", SW_MINIMIZE);
or maybe there is a way to seperate the msi and exe in the textfile without me having to use 2 txt files? Looks like it is almost finished...WHEW! LOL. Thank you TJ_Tigger.
PS. Also, do I just run it once, or do I need to run it each time for each program...Im pretty sure its just once since it uses the for command. just checking :D
TJ_Tigger
08-28-2005, 08:42 AM
First things first...I would like to combing this with the FindDrive script,
Depending on what the FindDrive script returns. If it returns a full path when found and false/nil when not then I would preempt the File.Run command with the FindDrive and if it is true then use the File.Run command.
also, what is the deal with the switches in quotes? do i need them to be in quotes? Im not sure...or if they arent does that mean they should be in quotes in the textfile?
With Windows some commands require quotes in the switches. It probably is not the whole switch, but maybe rather than quoting the entire switch use doublequotes in the switch and wrap it with singlequotes.
Now for the msi portion (I may break the programs up and have one for executables and one for msi files)
Shouldn't be necessary, just get the extension of the file and then determine which command you should run.
--Here is the contents of your text file. programs.txt
7zip\7zip.exe,/S
Progs\Progs.exe,/SP- /VERYSILENT /NORESTART
HexEditor\Hexeditor.msi,/qb-!
--this should extract the information you want from the file
tblReturn = TextFile.ReadToTable("AutoPlay\\Docs\\programs.txt");
if tblReturn then --if the table is valid then proceed
tblProgs = {}; --build a place to store the contents of the text file.
for i,v in tblReturn do --traverse each item in the table
--search for the position of the delimeter in the string.
nPos = String.Find(v, ",", 1, false);
if nPos ~= -1 then --the delimeter was found
tblProgs[i] = {};
tblProgs[i].Program = String.Left(v, nPos -1);--Get the program information
tblProgs[i].Switches = String.Mid(v, nPos +1, -1);--Get the switches
end
end
end
--Then if you wanted to run it
if tblProgs then
for i,v in tblProgs do
if FindDrive(tblProgs[i].Program) then
tblSplit = String.SplitPath(tblProgs[i]Program)
if tblSplit.Extension == ".exe" then
File.Run(tblProgs[i].Program, tblProgs[i].Switches, "", SW_MINIMIZE, true);
else
Shell.Execute(FindDrive(tblProgs[i].Program), "open", tblProgs[i].Switches, "", SW_MINIMIZE);
end
end
end
end
Or something along those lines anyway
threaded
08-28-2005, 09:14 AM
well finddrive basically looks on 2 drives ONLY...and in 2 locations. Now it will return the full path to the file if the file exists on one of those 2 drives. If it does not find it on either drive, then I wouldnt want anything to run, mainly just for the script to bypass that exe file.
TJ_Tigger
08-28-2005, 11:43 AM
well finddrive basically looks on 2 drives ONLY...and in 2 locations. Now it will return the full path to the file if the file exists on one of those 2 drives. If it does not find it on either drive, then I wouldnt want anything to run, mainly just for the script to bypass that exe file.
The above script does that. You may want to make sure that if the file is not found then the function returns nil. That last section does all the running and checking of the files. I didn't comment it but basically if there is a table full of programs and switches it will run through each of them one at a time. If the file is found it will then split the file to get the extension and then File.Run if it is an .exe or Shell.Execute if msi.
Hope that helps
Tigg
threaded
08-28-2005, 10:40 PM
Well FindDrive does return a nil if the file is not found on either drive. Everything is pretty much finished as far as the scripting goes...now Im going through everything and making sure Im ending in ";" where needed (I know its not needed, but seeing as how over 3/5 of the project ends with ";" I figured it would be easier just adding the rest). I set up all the global functions with a semi-help portion which includes name, purpose, values, returns, and author - that way I can give proper credit to people who i steal from (not really stealing as everything was offered to me through asking questions, but still Im sure the people who offered them didnt want me to pass it off as my own ;) ).
Im also going through and including some checks in the scipt...if a file doesnt exist, then certain things dont need to be run. Been converting a couple of "if then else end" scripts to "if then elseif then elseif then else end". For instance, the script you gave me...this is how I have it now (includes FindDrive, just in case you see something and could let me know if its gonna cause a problem with the program installer script)
-- ************************************************** ********************************************
-- Name: Programs
-- Purpose: Load text file that contains program directory, program, and switches
-- (ex. 7zip\\7zip.exe,/S)
-- Values: none
-- Returns: none
-- Author: TJ_Tigger
-- ************************************************** ********************************************
--****(LOAD PROGRAM LIST INTO TABLE)****
tblReturn = TextFile.ReadToTable(CDrive.."\\OEM\\Autoplay\\programs.txt");
--****( IF TABLE CONTAINS ENTRIES )****
if tblReturn then
--****(BUILD TABLE TO STORE CONTENTS)****
tblProgs = {};
-- ****( TRAVERSE THE PROGRAM TABLE )****
for i,v in tblReturn do
--****(SEARCH FOR POSITION OF THE DELIMETER)****
nPos = String.Find(v, ",", 1, false);
--****(IF DELIMETER FOUND)****
if nPos ~= -1 then
tblProgs[i] = {};
--****(GET PROGRAM INFORMATION)****
tblProgs[i].Program = String.Left(v, nPos -1);
--****(GET SWITCHES)****
tblProgs[i].Switches = String.Mid(v, nPos +1, -1);
end
end
end
-- ************************************************** ********************************************
-- Name: FindDrive
-- Purpose: Find file on cdrom or system drive
-- Values: sFilename: the filename, with extension, to find
-- Returns: none
-- Author: TJ_Tigger
-- ************************************************** ********************************************
function FindDrive (sFilename)
-- ****( DECLARE LOCAL VARIABLES )****
local found = nil;
-- ****( SEARCH ON CDROM FIRST )****
if File.DoesExist(CDrive.."\\OEM\\Applications\\" .. sFilename) then
found = CDrive.."\\OEM\\Applications\\" .. sFilename;
-- ****( SEARCH ON SYSTEM DRIVE )****
elseif File.DoesExist(sDrive.."\\Applications\\" .. sFilename) then
found = sDrive.."\\Applications\\" .. sFilename;
else
-- ****( IF NOT FOUND )****
found = nil;
end
-- ****( RETURN VARIABLE )****
return found;
end
that gets put into global functions
if tblProgs then
for i,v in tblProgs do
if FindDrive(tblProgs[i].Program) then
tblSplit = String.SplitPath(tblProgs[i].Program);
Paragraph.SetText("ApplicationText", "Installing "..tblSplit.Filename);
if tblSplit.Extension == ".exe" then
File.Run(tblProgs[i].Program, tblProgs[i].Switches, "", SW_MINIMIZE, true);
elseif tblSplit.Extension == ".msi" then
Shell.Execute(tblProgs[i].Program, "open", tblProgs[i].Switches, "", SW_MINIMIZE);
end
end
end
end
that gets put into Page OnLoad
Im thinking of adding to the script you gave me so that the txt file does not have to have "\\" in between the directory and the filename...or does it require that in the txt file? I just assumed it did as AMS says that when running files, all \ need to be converted to \\
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.