Delete Files and Process Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jobs

    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




  • nos

    #2
    Re: Delete Files and Process Problem

    try
    f.deleteOnExit( ); // since f.delete() doesn't always work


    "Jobs" <jobs_sse@sympa tico.ca> wrote in message
    news:b8HCb.1806 6$aF2.2141389@n ews20.bellgloba l.com...[color=blue]
    > Hello All,
    >
    > I want to delete all files in a directory. I am making a backup copy of[/color]
    all[color=blue]
    > files in the directories say c:\abc by reading and writing to a file.[/color]
    After[color=blue]
    > 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[/color]
    new[color=blue]
    > child process(execute d bacth file). This solution works fine on[/color]
    windows2000.[color=blue]
    > But on WindowsNT, the JVM can not create child process. So the files can[/color]
    not[color=blue]
    > be deleted.
    >
    > Could you Java Gurus help me to solve this probem?
    >
    > Thankx in advance.
    >
    > Manmohan
    >
    >
    >
    >[/color]


    Comment

    • hiwa

      #3
      Re: Delete Files and Process Problem

      "Jobs" <jobs_sse@sympa tico.ca> wrote in message news:<b8HCb.180 66$aF2.2141389@ news20.bellglob al.com>...[color=blue]
      > 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[/color]
      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>

      Comment

      • Manmohan

        #4
        Re: Delete Files and Process Problem

        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" <HGA03630@nifty .ne.jp> wrote in message
        news:6869384d.0 312131609.7ed9f ea6@posting.goo gle.com...[color=blue]
        > "Jobs" <jobs_sse@sympa tico.ca> wrote in message[/color]
        news:<b8HCb.180 66$aF2.2141389@ news20.bellglob al.com>...[color=blue][color=green]
        > > Hello All,
        > >
        > > I want to delete all files in a directory. I am making a backup copy of[/color][/color]
        all[color=blue][color=green]
        > > files in the directories say c:\abc by reading and writing to a file.[/color][/color]
        After[color=blue][color=green]
        > > 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[/color][/color]
        me[color=blue][color=green]
        > > 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[/color][/color]
        new[color=blue][color=green]
        > > child process(execute d bacth file). This solution works fine on[/color][/color]
        windows2000.[color=blue][color=green]
        > > But on WindowsNT, the JVM can not create child process. So the files can[/color][/color]
        not[color=blue][color=green]
        > > be deleted.
        > >
        > > Could you Java Gurus help me to solve this probem?
        > >
        > > Thankx in advance.
        > >
        > > Manmohan[/color]
        > 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>[/color]


        Comment

        • Anthony Borla

          #5
          Re: Delete Files and Process Problem


          "Jobs" <jobs_sse@sympa tico.ca> wrote in message
          news:b8HCb.1806 6$aF2.2141389@n ews20.bellgloba l.com...[color=blue]
          > 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.
          >[/color]
          <SNIP CODE>[color=blue]
          >
          > 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.
          >[/color]

          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 !
          [color=blue]
          >
          > But on WindowsNT, the JVM can not create
          > child process. So the files can not be deleted.
          >[/color]

          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].


          Comment

          • hiwa

            #6
            Re: Delete Files and Process Problem

            One suspicion. The File object in question may be referenced from
            other File variable. In other words, It may be a 'shared' File
            object....

            Comment

            Working...