How do you read how many folders are in a path and other tricks?
by Leroy Frederick · in Torque Game Builder · 07/05/2006 (10:43 am) · 18 replies
Good day all,
Just wondering where all the folder functions are for torque, i've looked through TGE_Console_Cmds.rtf, searched the site, checked documentation and unless i'm missing something, i can't find anything anywhere? It's way too Hot in the uk right now for me to search any longer and still have a computer by the end of it, so any help well be very, very appreciated!
I specificially want to...
>Read how many folders are contained in a directory path
>Create a new directory(s)
>Delete/Rename a directory(s)
>Copy/Paste/Move a directory(s) etc
The aim is to make a user profile (so many games completed with TGB use this, wheres the snippet/tutorial already?) system which i had done successfully in another game engine that made it clear what the file & folder commands are (except it lacked a copy directory command so i had to make one :( which took me ages!!!)
Bear in mind a 'Just program it in c++ and recompile' response is useless to me as i'm struggling with TGB scripting at times and the most i've done with c++ is make a menu dialog that opens web pages and executes a program (one simple command), far from a c++ pro.
Just wondering where all the folder functions are for torque, i've looked through TGE_Console_Cmds.rtf, searched the site, checked documentation and unless i'm missing something, i can't find anything anywhere? It's way too Hot in the uk right now for me to search any longer and still have a computer by the end of it, so any help well be very, very appreciated!
I specificially want to...
>Read how many folders are contained in a directory path
>Create a new directory(s)
>Delete/Rename a directory(s)
>Copy/Paste/Move a directory(s) etc
The aim is to make a user profile (so many games completed with TGB use this, wheres the snippet/tutorial already?) system which i had done successfully in another game engine that made it clear what the file & folder commands are (except it lacked a copy directory command so i had to make one :( which took me ages!!!)
Bear in mind a 'Just program it in c++ and recompile' response is useless to me as i'm struggling with TGB scripting at times and the most i've done with c++ is make a menu dialog that opens web pages and executes a program (one simple command), far from a c++ pro.
#2
07/07/2006 (8:20 am)
Not sure about managing directories (as opposed to the files in those directories), but maybe if you look at the FileObject it will get you started. Try a search here and on TDN and see what you come up with.
#3
07/07/2006 (8:34 am)
You could always tokenize the filepath on the separator to count the directory depth.
#4
Thus far i've managed to save and read data from a file via the pdf on FileObject.
Ben, what do you mean tokenize the filepath, do you mean create a file in each new created path, do a search for files in a path and work out the folders via checking for the last slash of a files path?
Any examples are more then welcome, sorry if my questions sound a bit newbish, i'm just use to have directory commands (from previous programming exploits)
Thanks in advance and thanks for the thoughts! :-)
EDIT: Oh, also is there a delete file command (i think there was something about a security issue from an older article regarding file/path deletion)
07/07/2006 (9:19 am)
Thanks guys, I've checked out the FileObject() sections but i was expecting perhaps they would be directory commands.Thus far i've managed to save and read data from a file via the pdf on FileObject.
Ben, what do you mean tokenize the filepath, do you mean create a file in each new created path, do a search for files in a path and work out the folders via checking for the last slash of a files path?
Any examples are more then welcome, sorry if my questions sound a bit newbish, i'm just use to have directory commands (from previous programming exploits)
Thanks in advance and thanks for the thoughts! :-)
EDIT: Oh, also is there a delete file command (i think there was something about a security issue from an older article regarding file/path deletion)
#5
Tokenizing strings is something i encourage every programmer to familiarize themselves with (unless they never plan to work with strings in any capacity).
07/09/2006 (11:05 am)
No, I'm talking about the general programming concept of tokenizing strings. TorqueScript has decent support for tokenizing strings. In pseudo-code:// counts the directory depth of a file path including the filename.
// returns the depth of directories to this file or -1 if the empty string is passed
function countPathDepth(path)
{
depth = -1;
tokenizer = new tokenizer(path, "/");
while (token = tokenizer.nextToken())
{
depth++;
}
return depth;
}Tokenizing strings is something i encourage every programmer to familiarize themselves with (unless they never plan to work with strings in any capacity).
#6
07/10/2006 (5:26 am)
Quote:Oh, also is there a delete file command (i think there was something about a security issue from an older article regarding file/path deletion)Try
fileDelete( "path/to/file" );
#7
there is:
findFirstFile();
findNextFile();
getFileCount();
07/10/2006 (5:08 pm)
Check out the TGBReference.pdf under ConsoleFunctions->File IOthere is:
findFirstFile();
findNextFile();
getFileCount();
#8
07/11/2006 (2:44 am)
Thanks for all the help/suggestions guys, i'll update when i've tried them out/succeded. @Matthew: yeah, i saw those commands but assumed since their called file, there were for files only, i guess i need to dig/read a little deeper! Thanks again, update soon!
#9
EDIT: Also, can any1 get any of the trim() commands to work?
'$gameFolder=trim("My Game Name ");' should result in '$gameFolder="MyGameName"' right?
07/12/2006 (6:02 am)
Thus far i've successfully created and deleted a file (via fileDelete();, thanks Jason),not tried the other stuff yet, is there a deleteDirectory()/deleteFolder() function at all as fileDelete() only seems to delete files as expected? Thanks in advance! :)EDIT: Also, can any1 get any of the trim() commands to work?
'$gameFolder=trim("My Game Name ");' should result in '$gameFolder="MyGameName"' right?
#10
This is pretty frustrating, doesn't TGB have any better file support file functions then this (without hacking the C++ engine)? Can someone please write the 'How to do a User profile' tutorial tdn.garagegames.com/wiki/index.php?title=Torque_2D/StandardTutorials/Advanced/Pr... because i've tried and I seem to be getting a fight back at the moment, it's bad enough that they're doesn't seem to be any folder/directory support! :(
07/15/2006 (7:33 am)
Man, is this fileDelete() command fully supported? I'm tring to update my drop menu (PopUpmenu) list when a profile is deleted when isFile($OldFile) is ==0 but it just remains at 1 after file deletion (yet if i put it in the console, it shows up as =0)?This is pretty frustrating, doesn't TGB have any better file support file functions then this (without hacking the C++ engine)? Can someone please write the 'How to do a User profile' tutorial tdn.garagegames.com/wiki/index.php?title=Torque_2D/StandardTutorials/Advanced/Pr... because i've tried and I seem to be getting a fight back at the moment, it's bad enough that they're doesn't seem to be any folder/directory support! :(
#11
07/15/2006 (11:47 am)
There are numerous ways to write user profiles, you could attach data to a script object and then run .save(%filePath)... that would probably be the easiest, then just exec the file if it exists for that user (name it appropriately.
#12
For example if i create a profile called 'Garage Games' then i decide to edit that profile and call it 'TGB', what you'd expect to happen is the profile to be file/folder to be copied/renamed to the new name, now of course, there seems to be no folder or rename commands so i have to instead create a new file, copy all the lines of the current one, write that data to the new file, delete the old file, wait for isFile() to return 0 (which isn't working) clear the PopUpMenu, repopulate the PopUpMenu with the fileBase() list of the dat files in the profile directory and a' presto! Easy huh
Perhaps i could use a schedule or timer delay although i expected isFile() to update in real time if the file no longer exists? Either that or i put a temporary refresh button...
07/15/2006 (3:51 pm)
I've already got a successful profile create, save and edit working (via fileObject), but the isFile() isn't returning as 0 after deleting a removed/edited profile file which leaves the PopUpmenu with the old file name in the list (even after the use of .clear()), it seems to repopulate the PopUpmenu list too quickly. For example if i create a profile called 'Garage Games' then i decide to edit that profile and call it 'TGB', what you'd expect to happen is the profile to be file/folder to be copied/renamed to the new name, now of course, there seems to be no folder or rename commands so i have to instead create a new file, copy all the lines of the current one, write that data to the new file, delete the old file, wait for isFile() to return 0 (which isn't working) clear the PopUpMenu, repopulate the PopUpMenu with the fileBase() list of the dat files in the profile directory and a' presto! Easy huh
Perhaps i could use a schedule or timer delay although i expected isFile() to update in real time if the file no longer exists? Either that or i put a temporary refresh button...
#13
07/16/2006 (10:31 am)
I believe you have to refresh your file list before isFile will return false after a delete. I'd imagine after you create a new file, that isFile might also return false until the file list is refreshed. I'm pretty sure Torque parses your file tree at startup only so isFile returns the state of that file on the file system when Torque was started, not the state right "now"
#14
07/16/2006 (12:56 pm)
Ben is correct.
#15
Oh, Thanks every1 for the help thus far, i wouldn't of been able to achieve what i have already in the such short amount of time without it :)!
EDIT: Also, in addition to thee above, i may be mistaken, but isn't there a command that checks/stops input if \/:*?"<>| has been inputted in an TextEditCrtl? Also whats/how do you use the validation option for this control, thanks :)
07/17/2006 (3:51 am)
I haven't looked yet but is there a specific command/process to update the file list, and would it be ok to use this command/process after file deletion/creation?Oh, Thanks every1 for the help thus far, i wouldn't of been able to achieve what i have already in the such short amount of time without it :)!
EDIT: Also, in addition to thee above, i may be mistaken, but isn't there a command that checks/stops input if \/:*?"<>| has been inputted in an TextEditCrtl? Also whats/how do you use the validation option for this control, thanks :)
#16
07/17/2006 (7:43 am)
Yes, there is a command. I don't remember it. You should be able to find it with the other file commands. And yes, it would be safe to use it after file delete/create.
#17
EDIT: is it something to do with the setModPaths($modPath); command?
If it is, what path should the setModPaths() be set to?
EDIT 2: I think i've got it, setModPaths("ProjectFolderName");. What a strange way to refresh a file list huh?
07/17/2006 (8:18 am)
Funny, and i just create a solution via arrays (creating an ignore list and emptying the entry when the profile is recreated). Thanks Ben, while i have another look, in case i miss it, if any1 knows/remembers the command, please share! :)EDIT: is it something to do with the setModPaths($modPath); command?
$modPath = pushback($userMods, $baseMods, ";"); setModPaths($modPath);
If it is, what path should the setModPaths() be set to?
EDIT 2: I think i've got it, setModPaths("ProjectFolderName");. What a strange way to refresh a file list huh?
#18
Some other questions
>Is there a string operator/escape sequence for quote " strings (e.g. \\ = slash or perhaps \") (although i've already created a function that does this)?
>is there a command that checks/stops input if \/:*?"<>| has been inputted in an TextEditCrtl (although i've already created a function that does this also)? Also whats/how do you use the validation option for this control?
07/17/2006 (9:24 am)
Thanks again for all the help and leading me in the right direction, i don't think i would of know that setModPath refreshes the file list!Some other questions
>Is there a string operator/escape sequence for quote " strings (e.g. \\ = slash or perhaps \") (although i've already created a function that does this)?
>is there a command that checks/stops input if \/:*?"<>| has been inputted in an TextEditCrtl (although i've already created a function that does this also)? Also whats/how do you use the validation option for this control?
Torque Owner Leroy Frederick