472,353 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Delete Files and Process Problem

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
5 14032
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
"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Richard Arthur | last post by:
This is a weird problem. 1) I use MediaDet to save a bitmap in a temporary file. 2) I create a bitmap using that temporary file's name. 3) I use...
3
by: News | last post by:
Is it possible to delete a file by copying it to the "bit bucket" or "null device"? Back in my youth when I live in VMS-land you could delete a...
1
by: Matt Hamilton | last post by:
I have a simple image gallery where I want to allow users to delete files. The problem I have is that after an image is displayed in the browser, I...
12
by: Lucas Tam | last post by:
I have a very simple loop: If (Directory.Exists(tempDirectory)) Then Try Dim Files() As String = Directory.GetFiles(tempDirectory) 'Clear out...
5
by: wo20051223 | last post by:
Deleting some files with C# fails with "Access to the path 'X' is denied". I have files copied from a CD that I burned (and not locked by a...
1
by: Eric | last post by:
Is it possible after i run my process i delete text files from server. I run a process to read from text files after that i want to delete these...
9
by: Osamede Zhang | last post by:
I just find i can't understand the code like this int main() { int *p=new int; //do something delete p; return 1; } I use new operator...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
On a form - I have a datagridview which is docked to the entire form. The datagridview allows users to Delete and/or Add Rows. On the Form_Load...
2
by: Viewer T. | last post by:
I am trying to write a script that deletes certain files based on certain criteria. What I am trying to do is to automate the process of deleting...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.