none
Files cannot access because they are used by another process RRS feed

  • Question

  • I am trying to delete some files in:

    C:\inetpub\wwwroot\my_site\

    Under my_site, there are many files and folders, including bin folders which has some .dll files. As soon as I run the script, I get an error message that some of the .dll files cannot be accessed because they are being used by another process, and on some dlls, I get a permission denied error.

    I don't know what the problem is. I am using the following script, which works perfect for any other folders, but not for the one I am trying to delete.

    $content = get-childitem 'C:\Backups\my_site' $sortedContent = $content | Sort-Object LastWriteTime -Descending write-host "This is the list of all the backups for my_site :" $count = 1 foreach ($item in $sortedContent) { Write-Host ("{0}: {1}" -f $count, $item.Name) $count++ } # 2.Take input from user $itemNumber = Read-Host "Please select which backup you want to restore" $confirmation = Read-Host "Are you Sure You Want To Proceed:" # 2.Take input from user if ($confirmation -eq 'y') { # 3. BACKUP script ./bacup_mysite.ps1 # 3. BACKUP # 4. DELETE CONTENTS OF my_site get-childitem "C:\inetpub\wwwroot\my_site\" -recurse | % { remove-item $_.FullName -recurse -force } } # 4. DELETE CONTENTS OF APP # 5. COPY CONTENTS OF ZIP INTO APP DIRECTORY $itemNumber = $itemNumber -1 if($sortedContent[$itemNumber].PSIsContainer -eq $true) { $src = $sortedContent[$itemNumber].FullName + "\" $WebzipPath = $src + "my_site.zip" $Date = Get-Date $folder_date = $Date.ToString("yyyy-MM-dd_HHmm") $tempPath = 'C:\_Temp\Restore\my_site_restore_' + $folder_date if (!(Test-Path -path $tempPath)) { New-Item $tempPath -type directory } ./Library/unzip.ps1 $WebzipPath $tempPath $tempPathWeb = $tempPath + "/my_site/*" Copy-Item -Path $tempPat -Destination 'C:\inetpub\wwwroot\my_site\' -Recurse - force

    Friday, July 11, 2014 9:30 AM

Answers

All replies

  • What are you asking?  You cannot access a file that is open or locked.  IIS and other web servers will not allow you to open files used by the server.

    To delete the files you will need to shut down the web service.


    ¯\_(ツ)_/¯

    Friday, July 11, 2014 9:35 AM
  •  am using a script which backups the folders and than in the next block tries to delete those folder from there original location.This is the script

    if ($confirmation -eq 'y') {
    
    # 3. BACKUP script
    

    ./bakup_mysite.ps1

    #If i put a Read-Host here it does stop and than ask user to press any key and than it
    deletes the next block, but i want to put a wait so user does not have to press any key
    and everything happens automatically, # 3. BACKUP

    # 4. DELETE CONTENTS OF my_site get-childitem "C:\inetpub\wwwroot\my_site\" -recurse | % { remove-item $_.FullName -recurse -force } }

    I would like the backup to finish first and than delete the original files, because if i use the above script while backup up some files gets locked and at the same time the next block gets executed and it tries to delete the files thats why i would like to put a wait for backup to finish


    • Edited by k786 Friday, July 11, 2014 10:58 AM
    Friday, July 11, 2014 10:58 AM
  •  am using a script which backups the folders and than in the next block tries to delete those folder from there original location.This is the script

    if ($confirmation -eq 'y') {
    
    # 3. BACKUP script
    

    ./bakup_mysite.ps1

    #If i put a Read-Host here it does stop and than ask user to press any key and than it
    deletes the next block, but i want to put a wait so user does not have to press any key
    and everything happens automatically, # 3. BACKUP

    # 4. DELETE CONTENTS OF my_site get-childitem "C:\inetpub\wwwroot\my_site\" -recurse | % { remove-item $_.FullName -recurse -force } }

    I would like the backup to finish first and than delete the original files, because if i use the above script while backup up some files gets locked and at the same time the next block gets executed and it tries to delete the files thats why i would like to put a wait for backup to finish


    Check your assumptions.  How do you know that backup is the only process that might have a lock on those files?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, July 11, 2014 11:14 AM
    Moderator
  • This is because when i put a read-host after ./backup , the backup gets completed and than it asks for user input , as soon as i press any key it then deletes the original file without any problem
    Friday, July 11, 2014 11:17 AM
  • In line with mjolinor's reply: see if you can find the culprit using PsFile

    http://technet.microsoft.com/en-us/sysinternals/bb897552

    Friday, July 11, 2014 11:19 AM
  • This is because when i put a read-host after ./backup , the backup gets completed and than it asks for user input , as soon as i press any key it then deletes the original file without any problem

    OK.  

    That probably means whatever is in that backup script is starting some process that's running outside of your script process, and your script is continuing on while that process is still running.

    And all we know about it is that the script is named  bacup_mysite.ps1.

    Can you think of any information you might be able to provide that would help answer this question?


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, July 11, 2014 11:52 AM
    Moderator
  • I modified my script so that powershell waits for the backup to finish and than moves on to the next command but this time when the backup script gets run it only create the base backup folder but doesnot actually copies the backup folder, meaning once the whole script finishes executing i have a backup folder but empty

     $BackupJob = Start-Job -FilePath "C:\test\my_site.ps1"
     Wait-Job $BackupJob 
    Friday, July 11, 2014 3:22 PM
  • I think we're still at the same place we were before (and where the same question posted at SO is at).  

    Without knowing what's in the backup script, all anybody can do is speculate about any number of things that might be causing the symptoms.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Proposed as answer by jrv Friday, July 11, 2014 3:41 PM
    Friday, July 11, 2014 3:31 PM
    Moderator
  • This is my backup script

    $Service_folder_name = 'C:\Services\'
    $Pics_folder_name = 'C:\Pics\'
    
    
    $Date = Get-Date
    
    $folder_date = $Date.ToString("yyyy-MM-dd_HHmm")
    
    $backup_folder_name = 'c:\_Backups\my_site\' + $folder_date
    
    if (!(Test-Path -path $backup_folder_name)) {
    
    New-Item $backup_folder_name -type directory
    }
    
    if ((Test-Path -path $Pics_folder_name)) {
    gi $pics_folder_name | .\Library\out-zip.ps1 $backup_folder_name\pics.zip $_
    }
    
    if ((Test-Path -path $Service_folder_name)) {
    gi $Service_folder_name | .\Library\out-zip.ps1 $backup_folder_name\Services.zip $_
    }

    Monday, July 14, 2014 8:27 AM
  • Same problem because we still cannot see the whole script.  Your script is s wrapper around yet another script which we cannot see.

    You need to stop and think clearly about what you are doing and what you are asking. 

    Without all of the correct information it is not possible for anyone to help you.

    If Out-Zip is using a shell compression technique then you will not be able to fix this.  I suspect that is what is in the last script.


    ¯\_(ツ)_/¯

    Monday, July 14, 2014 12:31 PM
  • If this is what you are using: http://stam.blogs.com/8bits/2010/06/create-zip-archives-with-powershell.html

    Then you will not be able to control how this works.


    ¯\_(ツ)_/¯

    Monday, July 14, 2014 12:33 PM
  • this is my out-zip script

    $path = $args[0]
    $files = $input
      
    if (-not $path.EndsWith('.zip')) {$path += '.zip'} 

    if (-not (test-path $path)) { 
      set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 


    $ZipFile = (new-object -com shell.application).NameSpace($path) 
    $files | foreach {$zipfile.CopyHere($_.fullname)} 

    Monday, July 14, 2014 1:09 PM
  • i have 1 big script which does all the job of restoring back up and deleteing the existing one. However before doing that i call my backup script from the main script which backs up the existing site before deleting it and restoring it to the previous state.  but when my backup script runs (which i am  calling from main script) at the same time the files gets deleted, i want to put a wait so that the backup finishes first than the current files gets deleted and than previous backup is restored and files are pasted on their respective folders. thats what i am trying to do
    Monday, July 14, 2014 1:12 PM
  • As I suspected. You cannot wait on shell zip/compression. It is "async" to the process.  Use 7-Zip or another full commandline zip tool or look for one of the Net Zip script in the repository.  Your problem will be solved

    ¯\_(ツ)_/¯

    Monday, July 14, 2014 1:12 PM
  • Is there any example you can give please, i am really stuck on this script or how i can modify my current script, will really appreciate that
    Monday, July 14, 2014 1:16 PM
  • Is there any example you can give please, i am really stuck on this script or how i can modify my current script, will really appreciate that

    If you're running .Net 4.5, use this:

    http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile%28v=vs.110%29.aspx

    If not, use the commandline version here:

    http://www.7-zip.org/download.html


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Monday, July 14, 2014 1:19 PM
  • will it be possible if you can add some 7zip code which i can modiy and get it working
    Monday, July 14, 2014 1:36 PM
  • will it be possible if you can add some 7zip code which i can modiy and get it working

    I have no idea what you're asking. Just use the command to zip your file.

    Here's some additional explanation of the switches, if that's what you're meaning:

    http://www.dotnetperls.com/7-zip-examples


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Monday, July 14, 2014 1:42 PM
  • as you can see from my code, the main script calls the backup script and the backup script uses another script to zip the backup directories, shall i give you the whole code, step by step 

    Monday, July 14, 2014 1:44 PM
  • as you can see from my code, the main script calls the backup script and the backup script uses another script to zip the backup directories, shall i give you the whole code, step by step 

    What Mike is carefully try8ing to point out is that we will not write your script for you.  If you read the posted information and spend some time learning how PowerShell works you will be able to create a simple script that zips files.  It should be just one simple script.  There is no need for 3 or 4 scripts to do the job.

    The learning materials are linked at the top of the page.

    If you have a specific question then post back with your specific question in a new topic.


    ¯\_(ツ)_/¯

    Monday, July 14, 2014 1:47 PM