By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

Delete Files and Process Problem

P: n/a
Hello All,

I want to delete all files in a directory. I am making a backup copy of all
files in the directories say c:\abc by reading and writing to a file. After
making a backup copy I want to delete these files. I am using following
code:
// get list of all files in the directories

File f = new File(sdir);

if(f.isDirectory()) {
sfiles = f.list();
}

for(int u=0;u<sfiles.length;u++) {
new File(sfiles[u]).delete();
}
The programm does not delete files from the directory. Can anyone tell me
how I can delete these files from the java programm. I used a trick on
windows2000, I created a bactch file to delete these files and created a new
child process(executed bacth file). This solution works fine on windows2000.
But on WindowsNT, the JVM can not create child process. So the files can not
be deleted.

Could you Java Gurus help me to solve this probem?

Thankx in advance.

Manmohan


Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
nos
try
f.deleteOnExit(); // since f.delete() doesn't always work
"Jobs" <jo******@sympatico.ca> wrote in message
news:b8*********************@news20.bellglobal.com ...
Hello All,

I want to delete all files in a directory. I am making a backup copy of all files in the directories say c:\abc by reading and writing to a file. After making a backup copy I want to delete these files. I am using following
code:
// get list of all files in the directories

File f = new File(sdir);

if(f.isDirectory()) {
sfiles = f.list();
}

for(int u=0;u<sfiles.length;u++) {
new File(sfiles[u]).delete();
}
The programm does not delete files from the directory. Can anyone tell me
how I can delete these files from the java programm. I used a trick on
windows2000, I created a bactch file to delete these files and created a new child process(executed bacth file). This solution works fine on windows2000. But on WindowsNT, the JVM can not create child process. So the files can not be deleted.

Could you Java Gurus help me to solve this probem?

Thankx in advance.

Manmohan

Jul 17 '05 #2

P: n/a
"Jobs" <jo******@sympatico.ca> wrote in message news:<b8*********************@news20.bellglobal.co m>...
Hello All,

I want to delete all files in a directory. I am making a backup copy of all
files in the directories say c:\abc by reading and writing to a file. After
making a backup copy I want to delete these files. I am using following
code:
// get list of all files in the directories

File f = new File(sdir);

if(f.isDirectory()) {
sfiles = f.list();
}

for(int u=0;u<sfiles.length;u++) {
new File(sfiles[u]).delete();
}
The programm does not delete files from the directory. Can anyone tell me
how I can delete these files from the java programm. I used a trick on
windows2000, I created a bactch file to delete these files and created a new
child process(executed bacth file). This solution works fine on windows2000.
But on WindowsNT, the JVM can not create child process. So the files can not
be deleted.

Could you Java Gurus help me to solve this probem?

Thankx in advance.

Manmohan

If the permission is ok, you need to specify parent path when you
create a new File instance.
<code>
void processFile(File f){
try{
if (f.isDirectory()){ //if dir then recurse
String[] flist = f.list();
for (int i = 0; i < flist.length; ++i){
// File fc = new File(flist[i]); // No!
// File fc = new File(f.getName(), flist[i]); // No!
File fc = new File(f.getPath(), flist[i]); // This is OK
processFile(fc);
}
}
else{ //ordinary file
f.delete(); //or, whatever job on f
}
}
catch(Exception e){
}
}
</code>
Jul 17 '05 #3

P: n/a
Hello All,

Thankx for your reply. But I tried both solutions, but it doesn't work. Is
there something that I can try(piece of code)?

Manmohan

"hiwa" <HG******@nifty.ne.jp> wrote in message
news:68**************************@posting.google.c om...
"Jobs" <jo******@sympatico.ca> wrote in message

news:<b8*********************@news20.bellglobal.co m>...
Hello All,

I want to delete all files in a directory. I am making a backup copy of all files in the directories say c:\abc by reading and writing to a file. After making a backup copy I want to delete these files. I am using following
code:
// get list of all files in the directories

File f = new File(sdir);

if(f.isDirectory()) {
sfiles = f.list();
}

for(int u=0;u<sfiles.length;u++) {
new File(sfiles[u]).delete();
}
The programm does not delete files from the directory. Can anyone tell me how I can delete these files from the java programm. I used a trick on
windows2000, I created a bactch file to delete these files and created a new child process(executed bacth file). This solution works fine on windows2000. But on WindowsNT, the JVM can not create child process. So the files can not be deleted.

Could you Java Gurus help me to solve this probem?

Thankx in advance.

Manmohan

If the permission is ok, you need to specify parent path when you
create a new File instance.
<code>
void processFile(File f){
try{
if (f.isDirectory()){ //if dir then recurse
String[] flist = f.list();
for (int i = 0; i < flist.length; ++i){
// File fc = new File(flist[i]); // No!
// File fc = new File(f.getName(), flist[i]); // No!
File fc = new File(f.getPath(), flist[i]); // This is OK
processFile(fc);
}
}
else{ //ordinary file
f.delete(); //or, whatever job on f
}
}
catch(Exception e){
}
}
</code>

Jul 17 '05 #4

P: n/a

"Jobs" <jo******@sympatico.ca> wrote in message
news:b8*********************@news20.bellglobal.com ...
Hello All,

I want to delete all files in a directory. I am making a backup
copy of all files in the directories say c:\abc by reading and
writing to a file. After making a backup copy I want to delete
these files.
<SNIP CODE>
The programm does not delete files from the directory.
Can anyone tell me how I can delete these files from the
java programm. I used a trick on windows2000, I created
a bactch file to delete these files and created a new child
process(executed bacth file). This solution works fine on
windows2000.

Indeed it does, though deleting files via a batch file could be considered
dangerous since it is inherently insecure [others can edit the batch file to
act in a malicious way], and outside the control of the JVM [which any
'Runtime.exec' spawned process is]. Despite the dangers, it does work, and
remarkably efficiently to boot !

But on WindowsNT, the JVM can not create
child process. So the files can not be deleted.


This seems quite unusual to me. What error message are you receiving ? Have
you tried different ways of invoking the command interpreter e.g.

cmd.exe /c del myfiles.*

cmd.exe del myfiles.*

cmd.exe mybatch.bat

Are you certain the problem is JVM-related rather than batch file or
command-string related ?

I hope this helps.

Anthony Borla

P.S.

An alternative is a JNI routine which calls the Win32 API 'DeleteFile'
function. If you are familar with JNI it is actually quite a simple task to
code up the relevant functions [C and Java wrapper functions].
Jul 17 '05 #5

P: n/a
One suspicion. The File object in question may be referenced from
other File variable. In other words, It may be a 'shared' File
object....
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.