Solved

Delete a directory created in the same instance

Posted on 2008-11-03
13
285 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

789 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