Solved

Delete a directory created in the same instance

Posted on 2008-11-03
13
283 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to show menu 5 87
FMX enumerated colours 2 84
How do I set up two external hard drives with Acronis True Image? 12 80
Create a path if not exists 7 69
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

896 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now