[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Opening files, deleting folders, etc.

I have several misc. questions here.

1. How do I open up a users file?  Ex: like a notepad file once the program is ran
2. Is it possible to open up a user's browser to my site?  If so, how?  This is for updates.
3. I know how to delete a file and folder, but how can I delete a folder with lots of files in there?  Would it also delete the subfolders in it?  If not, how can it be done?
4. How do I create a folder for the user?

Each question 25 points each.
0
greyknight17
Asked:
greyknight17
3 Solutions
 
mrwad99Commented:
(Apologies in advance if you are not using Windows)

For 1 & 2:

In windows:

char szCommandLine[256];

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);

sprintf(szCommandLine, "%s %s", "C:\\Your browser path ", "http://www.yahoo.com");
CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE,  0, NULL,  NULL, &si, &pi);

This shows how to create a process with a specified command line.  For simply starting an app, just adjust szCommandLine to hold the app path, eg C:\\WINNT\Notepad.exe, or the document path too...

Alternatively you could look up one of the execl derived functions.

3 will require some thought...

For 4, again on Windows:
See CreateDirectory(), http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/createdirectory.asp
0
 
aib_42Commented:
Also note that ShellExecute() with "open" as the second parameter will open up any text files, URLs, etc. with the user's default (and probably favorite) program, saving you the trouble of searching for the executable of Notepad/Internet Explorer and will also eliminate the possibility of annoying the user by opening the URL with IE while his favorite browser is Netscape, etc.
0
 
mrwad99Commented:
Yeah I suppose that system() would be the easiest option if a specific EXE was not required.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
greyknight17Author Commented:
Yes, I'm using Windows for this (should have made it clearer in the beginning - so my fault).

I tried the code and got like 10 errors for it:

char szCommandLine[256];

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);

sprintf(szCommandLine, "%s %s", "C:\\Your browser path ", "http://www.yahoo.com");
CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE,  0, NULL,  NULL, &si, &pi);

How do I declare those two constants?  Is it just:

CONST STARTUPINFO si;

in the top part (before main function)?  Any way to do this without specifiying the path for the browser program (like the default browser)?

Thanks.
0
 
mrwad99Commented:
Credit to aib_42 for the ShellExecute() note :)

This demonstrates the two methods.  ShellExecute() does not require the path to the browser, and opens the web page in the default browser.  You can specify the exe to open the file with using CreateProcess().

#include  <stdio.h>
#include  <windows.h>
#include <process.h>

int main()
{
      char szCommandLine[256];
      
      STARTUPINFO si;
      PROCESS_INFORMATION pi;
      HWND handle = NULL;

      ZeroMemory(&si, sizeof(si));
      si.cb = sizeof(si);
      
      sprintf(szCommandLine, "%s %s", "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE", "http://www.yahoo.com");
      CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE,  0, NULL,  NULL, &si, &pi);
      
      ShellExecute(handle, "open", "http://www.yahoo.com", NULL, NULL, SW_SHOWNORMAL);

      return  0;
}
0
 
stuartdehaanCommented:
To delete a directory with or without files/folders you can use a system() call: system("deltree <your folder>");
0
 
greyknight17Author Commented:
Ready to split points up now, just have one more question on that system call.

Do I need a library called to use it?  I did something like:

system("delete c:\documents and settings\username\desktop\oldfolder");

it says it doesn't recognize the call or something.  I could use spaces in the path right?
0
 
mrwad99Commented:
>> system("delete c:\documents and settings\username\desktop\oldfolder");

1) #include <windows.h> if not already
2) The command is del, not delete, in Windows
3) Use double backslashes, hence

system("del c:\\documents and settings\\username\\desktop\\oldfolder");


0
 
gbonneCommented:
Warning! Del does not remove folders! Only files!

The example above calls "deltree" which is not the same thing.

It is not clean to use system calls where you could loop subdirs and remove each file and its containing directory cleanly and properly and much faster than a "system" call.

I won't copy paste an example code for that here but you will need the following functions (standard C versions mentionned, you can find compiler specific enhanced versions too):
findfirst(), findnext(), remove(), unlink() (old function to delete files but still current for directories).
0
 
greyknight17Author Commented:
Ok, I will ask how to search in dirs in another post, but for the time being, I want to do the deletions now.

Sorry about that, I made a mistake for deleting directories.  But is this correct:

system("deltree c:\\documents and settings\\username\\desktop\\oldfolder");

I used the regular code (stdio.h etc....) and put windows.h library also on top.  Then the top line system... is all I put to delete that oldfolder.  I tried it and it doesn't work.  With or without any files in there.  I even tried replacing 'documents and settings' with docume~1.  Do I need to enclose quotes around the directory itself also?  Tried that and no go there.

This is really frustrating.  How do I delete a single file?  I tried replacing that deltree with delete or del and both won't work.  I'm sure the files I'm trying to delete is not read-only or protected.  

Another 50 points added to solve this problem.  Thanks.
0
 
mrwad99Commented:
The reason that you cannot use deltree is probably because you are running Windows 2000.  This version does not come with the deltree command, but it does come with

rd

or

rmdir

both doing the same, and short for remove directory.

I tested this command out, by first creating a directory "New Folder" in My Documents, then creating several text files, then creating a sub folder "New Folder", and adding several text files into that.  The command

system("rd \"C:\\Documents and Settings\\Administrator\\My Documents\\New Folder\" /Q /S");

deletes this, with the /Q and /S commands not prompting for confirmation of deletion of each file, and recursively deleting the folder respectively.

And yes, you will need to put the directory to delete in quotes, as designated by \"
0
 
greyknight17Author Commented:
Oh, thank you so much for the help mrwad99.  I'm actually running XP, but I guess NT and up will be the same.  Forgot about that rd command in dos too.  LOL.

Thanks again for all you guys who helped out, I really appreciate it.

I will close this question now and points will be split.
0
 
mrwad99Commented:
Glad to help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now