Jump to content

Recommended Posts

Posted
<#
.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" }
}

Posted
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?
  • 2 weeks later...
Posted

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}

}

  • 2 months later...
Posted
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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...