How to close file for deletion

Welcome Forums General PowerShell Q&A How to close file for deletion

  • This topic has 7 replies, 3 voices, and was last updated 2 months ago by
    Participant
    .
Viewing 7 reply threads
  • Author
    Posts
    • #200069
      Participant
      Topics: 5
      Replies: 70
      Points: 326
      Helping Hand
      Rank: Contributor

      I am trying to figure out a way to close an open file via PS. I have a script that uses Start-Process to open a Java JAR file that encrypts data. The JAR completes the file encryption, then the process ends. However, the file it encrypted is open and I cant remove it using Remove-Item which is also part of the script.

      Once the Script finishes, the file is unlocked and I can delete it. However, I need the PS script to delete the file. Using OpenFiles.EXE, I can see the file is open by Powershell.EXE and thusly I feel as though I should be able to unlock and delete the file, yet I am failing to find a way.

      The likely first suggestion will be to use PS to encrypt the file, sadly, I MUST use the JAR file. I have a small function that detects if the file is locked using System.IO and it indeed reports the file is locked, I just have not found a way to unlock it using PS so I can delete it.

      Thanks in advance for any help.

    • #200099
      Participant
      Topics: 4
      Replies: 1908
      Points: 4,259
      Helping Hand
      Rank: Community Hero

      Without seeing your code it would be wild guessing. So please share your code (formatted as code using the code posting tag (“PRE”)). Thanks

      My first wild guess would be to use a job to encrypt the file using java.

    • #200231
      Participant
      Topics: 5
      Replies: 70
      Points: 326
      Helping Hand
      Rank: Contributor

      Sorry, script is too big with too many moving parts to post.

      I will try the job approach and see where that gets me. Thanks.

    • #200237
      Participant
      Topics: 4
      Replies: 1908
      Points: 4,259
      Helping Hand
      Rank: Community Hero

      Sorry, script is too big with too many moving parts to post.

      It sounds like the script deserves a makeover anyway. You might break your large script into smaller parts. Those usually are easier to maintain and easier to debug if needed. 😉

      • This reply was modified 2 months ago by Olaf.
    • #200243
      Participant
      Topics: 5
      Replies: 70
      Points: 326
      Helping Hand
      Rank: Contributor

      I totally agree. I am actually in the process of researching just that :). Any pointers/links would be greatly appreciated.

    • #200273
      Participant
      Topics: 5
      Replies: 316
      Points: 383
      Helping Hand
      Rank: Contributor

      Use Get-Process cmdlet to find what process encrypted and opened the file then use Stop-Process cmdlet to stop the process.

      # Get and stop java process
      Get-Process -Name javaw | Stop-Process
      # Get and stop notepad process
      Get-Process -Name notepad | Stop-Process
      
    • #200288
      Participant
      Topics: 5
      Replies: 70
      Points: 326
      Helping Hand
      Rank: Contributor

      Actually, there is no java process still running, it closes as expected. Running “openfiles” shows the encrypted file open via PowerShell.exe, not javaw, hence the reason I feel I should be able to delete the file from the script.

      Deleting the file is for clean up purposes and not the end of the world if I cant get rid of it.

      Thanks for the help.

    • #200297
      Participant
      Topics: 4
      Replies: 1908
      Points: 4,259
      Helping Hand
      Rank: Community Hero

      … Running “openfiles” shows the encrypted file open via PowerShell.exe, not javaw,

      You could try to start a second Powershell instance. Either with a job or simply using Start-Process.

Viewing 7 reply threads
  • You must be logged in to reply to this topic.