View Full Version : Help Please - Find & Rename - Noob
Project:
To install an application and find on hard drive files and rename extension for unnecessary files. I need to do full search for TestFile1.dll and TestFile2.dll. When found rename files to TestFile1.dl and TestFile2.dl. If files are not found to display error message. These file can reside on either C: or D: so I would need to search multiple drives.
I can have it rename files at the known drive letter and folder(C:\Blah) but I would like for the script to "FIND"(search C: D: E: ) incase it isn't the default folder.
I managed to search C:\ drive and when not found error message came up but when found files it would not rename(script error) with this example:
result = File.Find("WindowsRoot=String.Left(_WindowsFolder, 3)", "TestFile1.dll", true, true, nil);
if result == nil then
Message = file not found
else
Rename =
end
(adapted from TJTigger in forums)
Can anyone help, almost giving up.
Desmond
04-21-2004, 04:05 PM
Hello Grim,
The following example may be what you are looking for.
Please keep in mind, searching the user's hard drive like this takes a LONG time. You may want to be more specific of where you are searching for the files, and turn the recursion option off.
-- The drives to search through, in format c:\\
tDrivesToSearch = {"C:\\", "D:\\"};
-- The files to search for
tFilesToSearchFor = {"TestFile1.dll", "TestFile2.dll"};
--traverse through the drive table
for nIndex, sDrive in tDrivesToSearch do
-- traverse through the file table
for nIndex, sFile in tFilesToSearchFor do
--search for the specified file on the specified drive
tFoundFiles = File.Find(sDrive, sFile, true, false, nil);
-- if files were found:
if tFoundFiles then
-- traverse through the found files table
for nIndex, sFile in tFoundFiles do
-- Rename files from *.dll to *.dl
File.Rename(sFile, String.TrimLeft(sFile, "dll") .. "dl");
end
else
-- File was not found, display error message
Dialog.Message("Error", "File '" .. sFile .. "' was not found on your system.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
end
end
end
You're correct, failed to realize that one.
Do I change this line:
tDrivesToSearch = {"C:\\", "D:\\"};
"C:\\" to {"C:\program files", "D:\programs files"} ?
Changed Error = string.TrimLeft to String.TrimRight)
BTW - with script above, files are renamed from TestFile.dll to TestFile.dl sucessfully, however message "Error Files Not Found" still pops up even after files were found and renamed. It looks like "end" function after File.Rename is not working properly. What's your suggestion?
Thank you very much for your response, really. This is about the deepest in script I have ever been.
Desmond
04-22-2004, 09:23 AM
Good Morning!
You're right, TrimLeft should have been TrimRight. My bad!
With regards to the error message popping up . . . please see the below code. I failed to take into account that unless every file in the table was found on every drive/location listed, the error would pop up. What i've done is as soon as at least one file is found, i set a boolean variable to true. At the end, the error message only displays if this value is set to false (not true).
Hope that works for ya!
-- The drives to search through, in format c:\\
tDrivesToSearch = {"C:\\", "D:\\"};
-- The files to search for
tFilesToSearchFor = {"TestFile1.dll", "TestFile2.dll"};
-- Used to control error message at the end
bFound = false;
--traverse through the drive table
for nIndex, sDrive in tDrivesToSearch do
-- traverse through the file table
for nIndex, sFile in tFilesToSearchFor do
--search for the specified file on the specified drive
tFoundFiles = File.Find(sDrive, sFile, true, false, nil);
-- if files were found:
if tFoundFiles then
-- Keep track that at least this file was found
bFound = true;
-- traverse through the found files table
for nIndex, sFile in tFoundFiles do
-- Rename files from *.dll to *.dl
File.Rename(sFile, String.TrimRight(sFile, "dll") .. "dl");
end
end
end
end
if not bFound then
-- File was not found, display error message
Dialog.Message("Error", "File '" .. sFile .. "' was not found on your system.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
end
Stumbled in to this message:
Error
"On Click, Line 35: attemp to concatenate global 'sFile'(a nil value)
Referring to this line:
33 if not bFound then
34 -- File was not found, display error message
35 Dialog.Message("Error", "File '" .. sFile .. "' was not found on your system.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
end
What I did was changed messsage to say "Error - TestFile1.dll and TestFile2.dll were not found on your system, please check they exist or reinstall Application!" Which makes ALL work perfect. I'll try and learn more APMS scripting more and see if I can get the proper solution (practice project).
I really thank you for your assistance. As a plus I added your name "Desmond" to the Credits screen under "Created with APMS 5.0" Assistance by "Desmond" Indigorose Forums. ;)
Let me know if you want me to change anything from above.
Corey
04-22-2004, 05:47 PM
First the grammy for male bassoonist of the year, now this. Desmond's name is really getting out there...
Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)
Desmond
04-23-2004, 09:14 AM
Sorry Grim, I shoulda tested that before I posted. The variable sFile only exists within the for loop. You were 100% correct with your change. Good show!
Sorry dude! And thanks for the credits. :) That's really cool!
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.