Knil92 Posted May 31, 2017 Posted May 31, 2017 Hey, Just wondering if any one has a script that can delete files that are already in use? Thanks, Knil
mavhc Posted May 31, 2017 Posted May 31, 2017 If they're on the server and being access by remote clients, you can close connections in Computer Management, otherwise there's Unlock Locked Files & Folders with File Governor | NoVirusThanks or https://lockhunter.com/
mavhc Posted May 31, 2017 Posted May 31, 2017 Are they locally locked or being accessed by other computers?
Geoff Posted May 31, 2017 Posted May 31, 2017 <# .Synopsis Closes Files Opened in Network Share .EXAMPLE @("file\path\1", "file\path\2") | Close-OpenFile Attempts to close "file\path\1" and "file\path\2" if they are open. .EXAMPLE Close-OpenFile @("file\path\1", "file\path\2") Attempts to close "file\path\1" and "file\path\2" if they are open. .EXAMPLE "file\path\1" | Close-OpenFile Attempts to close "file\path\1" if it is open. .EXAMPLE Close-OpenFile "file\path\2" Attempts to close "file\path\2" if it is open. #> function Close-OpenFile { [CmdletBinding()] Param ( [Parameter(Mandatory=$true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName=$true, Position=0)] [string[]]$filesToClose ) Begin { $netFile = net file if($netFile.length -lt 7) { Throw "No Files are Open" } $netFile = $netFile[4..($netFile.length-3)] $netFile = $netFile | ForEach-Object { $column = $_ -split "\s+", 4 New-Object -Type PSObject -Property @{ ID = $column[0] FilePath = $column[1] UserName = $column[2] Locks = $column[3] } } $count = 0 } Process { ForEach ($file in $filesToClose) { ForEach ($openFile in $netFile) { if($openFile.FilePath -eq $file) { $count++ net file $openfile.ID /close > $null } } } } End { Write-Output "Closed $count Files" } }
Knil92 Posted June 2, 2017 Author Posted June 2, 2017 is that a powershell script? So if i wanted to do this manually I would type ("C:\testfolder\testfile.txt", "C:\testfolder\testfile2") | Close-OpenFile and that would make it so I can delete the two files?
Geoff Posted June 2, 2017 Posted June 2, 2017 It expects a string array. Close-OpenFile @("C:\testfolder\testfile.txt", "C:\testfolder\testfile2")
rsaddul1 Posted June 13, 2017 Posted June 13, 2017 Hello, I was hoping you can help? I am trying to achieve the below: 1) Copy 2 newest files from Scripts to Test and show this in a log. The log must show if has failed or been a success. This log to be sent in an email. 2) Delete any files in Scripts older than 365 days My Code is: # Moves latest two files Get-ChildItem 'C:\scripts' -File | Sort-Object -Property CreationTime -Descending | Select-Object -Last 2 | Copy-Item -Destination 'C:\TEST' -Force $Path = "C:\scripts" $Daysback = "-365" $CurrentDate = Get-Date $DatetoDelete = $CurrentDate.AddDays($Daysback) Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -WhatIF # Creates Log File $logProgress = 'C:\scripts\scriptlog.log' $source = 'C:\scripts\*.txt' $destination = 'C:\TEST' $txtfiles = get-childitem $source -recurse | Select-Object FullName foreach ($file in $txtfiles) {Move-Item -Path $file.FullName -Destination $destination -Force -ErrorAction SilentlyContinue if($? -eq $false){Write-Output "$($file.FullName) did not copy ok to $destination" | out-file -append $logProgress} else {write-output "$($file.FullName) copied OK to $destination" | out-file -append $logProgress} }
fewd01 Posted August 26, 2017 Posted August 26, 2017 If you're talking stuck files that you can't delete, try reducing the file path size by with renaming folder names or mapping a share to the file's folder and deleting the file through the share rather than on the server. Or... by browsing to the file using the file explorer built into 7zip and try to delete the file through 7zip. Sometimes works... oddly.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now