Solved

Delete a directory created in the same instance

Posted on 2008-11-03
13
282 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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

16 Experts available now in Live!

Get 1:1 Help Now