Solved

Delete a directory created in the same instance

Posted on 2008-11-03
13
284 Views
Last Modified: 2012-05-05
I would require suggestions on how to delete a directory that was created in the same program.

For example:

void main()
{
      system("mkdir c:\abc");
-
-
-
-
      system("rd /S /q c:\abc");
}

I am not able to delete it now because i get a message saying it is still attached to the process.
0
Comment
Question by:Cool-Guy
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22865867
Did you change the working directory to that directory ? Do you have a file open in that directory ?
0
 

Author Comment

by:Cool-Guy
ID: 22865931
Hi Infinity,
there are files in that directory but as you can see, i am using /s option along with RD. No, the working directory is outside of that directory.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22865952
>> there are files in that directory

And did you close all those files ?

You might also have to give a bit of time for the system to become aware that all files are closed.

I assume this is Windows ?



Oh, and I hope the code you posted isn't the literal code you use - ie. that there are no \'s in the string ? Because \a will generate an alarm ... You need to escape the backslash : \\a.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 86

Expert Comment

by:jkr
ID: 22868501
I'd rather suggest to use APIs rather than 'system()'. see http://msdn.microsoft.com/en-us/library/aa914759.aspx ("CreateDirectory") and http://msdn.microsoft.com/en-us/library/ms891470.aspx ("RemoveDirectory"), e.g.


CreateDirectory("c:\\abc",NULL);
 
//...
 
RemoveDirectory("c:\\abc");

Open in new window

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22870185
I would suggest you follow the advice jkr gave in his last comment though you have to delete all files in the directory before calling RemoveDirectory  cause this function expects an empty folder.

But you might get the same problem that RemoveDirectory would return with error when your process still blocks the newly created folder. To get around that you must avoid to ever change the current working directory to the new folder but always use a full-qualified path from outside of the folder.

0
 

Author Comment

by:Cool-Guy
ID: 22894605
Hi Infinity,
you are right, what i have posted is not the literal code, it was just to give an idea to the readers.
yes, all the files are closed. all the files within the directory get deleted but not the directory itself. I get the message "The process cannot access the file because it is being used by another process".

Hi jkr, itsmeandnobodyelse,
I can't use any window calls because of some limitations.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22896125
That indicates that your process is still using that directory.
0
 

Author Comment

by:Cool-Guy
ID: 22906759
That is my  question here, how do I delete a directlory before the program exits. What option do I have? Please note that I am trying to delete the directory before exiting from main.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22906924
>>>> how do I delete a directlory before the program exits. What option do I have?
You have the option to not blocking the directory yourself.

That means that none of your system calls may choose the new directory as working directory.

You better put all system commands to one command-file and do the delete in the command-file:

   system(myjob.cmd);

REM *****************************
REM myjob.cmd

mkdir c:\abc
...
c:
cd c:\
rd /s /q abc
if errorlevel 1 goto ERR
goto END
:ERR
echo could not remove folder abc
echo.
:END






0
 

Author Comment

by:Cool-Guy
ID: 22992762
itsmeandnobodyelse,
If I create and delete the folder within the same command line, then how do I use that folder for storing files when my job is getting processed.
During the processing, I will access files within it and at the end of the process, I am trying to delete the folder which is not happening.
0
 

Author Comment

by:Cool-Guy
ID: 23060393
Since none of these solutions really solved my problem, I would be deleting this question.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 23062736
>>>> If I create and delete the folder within the same command line,
>>>> then how do I use that folder for storing files when my job is getting processed.
You either should do all storing in the command job (A) or do all programmatically (B).  In the moment you were mixing things so that either your program or some other process like the explorer blocks the temporary folder for being deleted. If doing (B) your final statement before deleting the folder e.g.  by _rmdir() should be something like _chdir("..\") or _chdir("\\.") in order to definitively freeing the folder to remove from being blocked.
0
 

Author Closing Comment

by:Cool-Guy
ID: 31512634
This worked! Thanks!
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question