Solved

Delete a directory created in the same instance

Posted on 2008-11-03
13
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
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.

734 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