• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Trying to delete a file in a directory with other files  RSS feed

 
Greenhorn
Posts: 19
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to duplicate a text document, re-write a portion of this text document, then re-write over the orginal using the duplicate. After the process is done, I'd like to delete the duplicate file, but I cannot do so. I keep seeing online that you cannot delete a file in a directory that contains other files, but I can't believe that to be true. Maybe I'm misunderstanding, I'm not sure.

If code is needed, I'll provide it, though it's a bit lengthy. But my question is, how do I delete a file within a directory with other files?
 
Saloon Keeper
Posts: 4988
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You certainly can delete a file in a directory (folder) that contains other files.

In terms of steps:
  • read in text file
  • change text in memory
  • write changed text to new file
  • delete old file
  • rename new file to old file name
  •  
    Carey Brown
    Saloon Keeper
    Posts: 4988
    54
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
     
    Saloon Keeper
    Posts: 4857
    117
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Drew Hedgecoke wrote:I'd like to delete the duplicate file, but I cannot do so.


    What does this mean? What happens if you try to do it? Post any exceptions and/or error messages .

    I keep seeing online that you cannot delete a file in a directory that contains other files, but I can't believe that to be true.


    It is not.
     
    Drew Hedgecoke
    Greenhorn
    Posts: 19
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Whenever the code reaches:



    I get this exception:

    java.nio.file.FileSystemException: D:\Program Files\Utility\NDH Bot\resource\profiles\test(copy).txt: The process cannot access the file because it is being used by another process.

    Extra information, content is the raw message received by a user through discord. I have the command arguments like so:

    This is to change things in a text file, such as a user's wallet amount.
     
    Carey Brown
    Saloon Keeper
    Posts: 4988
    54
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What other process are you running that has that file open?

    In general you should not be mucking with any file stored under "Program Files". I notice that it's on drive D: and not C:. Did you install some programs on D:? Is "NDH Bot" something that you wrote or something you installed? Program data is usually kept in C:\Users\<your-name>\AppData\...
     
    Drew Hedgecoke
    Greenhorn
    Posts: 19
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nothing, other than Windows Explorer in that directory. The file is not open in anything else, unless their is something hidden going on, thought I cannot imagine that.

    NDH Bot is the program folder that I have for the code I'm creating. It's stored in a hard drive that does not contain the OS, and I manually named it Program Files. I like to keep my OS very light.
     
    Carey Brown
    Saloon Keeper
    Posts: 4988
    54
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you perhaps opened the file for reading or writing and didn't close it when you were done with it?
     
    Marshal
    Posts: 60766
    189
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    . . . or maybe all files in Program Files are regarded as “in use by a process” regardless. Don't know enough about that sort of permission, but it is possible that such an exception will be thrown for any attempt to delete such files.
     
    Sheriff
    Posts: 23714
    50
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:Have you perhaps opened the file for reading or writing and didn't close it when you were done with it?



    Your original post strongly suggests that you're first creating a "duplicate" file and then copying it to the original location (which is presumably on a different drive or you wouldn't have to do that). If you don't close the duplicate file after you do the copying then you won't be able to delete it (not on Windows anyway).

    If you still have to post code, I don't see why it would have to be a lot of code. Try removing all irrelevant code so you have only code which does the duplicating and copying and deleting.
     
    Drew Hedgecoke
    Greenhorn
    Posts: 19
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As requested, I'm posting the code. I'm not experienced, so some things may be a bit unnecessary. And, to clear things up, I'm creating a bot for discord. I'm using the JDA (Java Discord API) and a couple of their objects and methods. "channel" discovers in what channel this user sent the message, "sendMessage()" is, to the point, sends a message to that discord server using the bot, "message" contains the raw content of the message sent by any user, and "queue()" sets the messages sent to the bot in an order by which came first. Hope that helps.

    To summarize, first, I create a new text document, add the tag "(copy)" to it, and reference the original. During this process, I check if the current line starts with "Wallet:", and if it does, I add the user's amount to the amount listed in the original text document, and then I print the result in that current line of the duplicate.


    Then, I re-write over the original by referencing the duplicate. After this, I attempt to delete the duplicate.


    Hopefully that clears it up.
     
    Carey Brown
    Saloon Keeper
    Posts: 4988
    54
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're not closing scanFile or scanTempfile,
     
    Paul Clapham
    Sheriff
    Posts: 23714
    50
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Paul Clapham wrote:If you don't close the duplicate file after you do the copying then you won't be able to delete it (not on Windows anyway).



    And yeah, you don't close it. You start by using a Scanner to open it (a variable named scanTempFile) but you never close it.

    Could I suggest this simpler method to do what you're doing?

    1. Copy the original file to the new file, applying the modifications.

    2. Close and delete the original file.

    3. Close and rename the new file.
     
    Carey Brown
    Saloon Keeper
    Posts: 4988
    54
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I had to hack your code to make it readable and compilable in order to find the errors. This is something that you should do to help us help you.

     
    Drew Hedgecoke
    Greenhorn
    Posts: 19
    Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I feel so stupid for that. You were right, I did not close the scans, therefore whenever the next time an event happened, its still scanning my file, therefore it cannot delete it. I apologize for the messy code, and thank you for the suggestion, I'll make sure to do that. Appreciate the help everyone.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!