473,572 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.isDirector y()) {
sfiles = f.list();
}

for(int u=0;u<sfiles.le ngth;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(execute d 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 14144
nos
try
f.deleteOnExit( ); // since f.delete() doesn't always work
"Jobs" <jo******@sympa tico.ca> wrote in message
news:b8******** *************@n ews20.bellgloba l.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.isDirector y()) {
sfiles = f.list();
}

for(int u=0;u<sfiles.le ngth;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(execute d 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******@sympa tico.ca> wrote in message news:<b8******* **************@ news20.bellglob al.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.isDirector y()) {
sfiles = f.list();
}

for(int u=0;u<sfiles.le ngth;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(execute d 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(Fil e 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.goo gle.com...
"Jobs" <jo******@sympa tico.ca> wrote in message

news:<b8******* **************@ news20.bellglob al.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.isDirector y()) {
sfiles = f.list();
}

for(int u=0;u<sfiles.le ngth;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(execute d 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(Fil e 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******@sympa tico.ca> wrote in message
news:b8******** *************@n ews20.bellgloba l.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(execute d 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
2522
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 the bitmap. 4) I want to destroy the file when the process exits, but I am having trouble I have written a Finalizer on the object that actually generates the bitmaps, as well as implementing...
3
2918
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 file by copying it to NL: ========== I have written a windows service as part of an interface between two different systems. The first system will write output into a file in a
1
1877
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 am not able to delete the file because "The process cannot access the file ... It is being used by another process". I also get this error when trying to delete through explorer on the server. I...
12
7981
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 directory For Each Filename As String In Files File.Delete(Filename) Next Catch ex As Exception
5
38976
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 process) and a text file that I created in Windows Explorer. I can delete all of them through Windows Explorer. I can programmatically delete the text file but not the others. Permissions: - All...
1
1498
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 text files so that no one again run the process. I am using ASP.
9
3912
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 allocate some memory in the heap,now i don't need it,but our process also need to be killed.The heap is in our process'
4
4846
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 event I Fill the datagridview source table with a sql DataAdapter (da) da.SelectCommand.CommandText = "Select * from Servertbl1" da.Fill(ds, "tbl1") so far, so good. If I add a row to the...
2
9259
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 certain malware files that disguise themselves as system files and hidden files. When I use os.remove() after importing the os module is raises a Windows Error: Access denied probably because the...
0
7733
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7640
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7705
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1240
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.