Reputation: 23
I have been beating myself up over trying to come up with a process to accomplish a specific set of tasks, what I need to do is, test file for a lock from a specific program, if a lock is detected I need to increment a counter by 1 and add that specific file name to a log file for reference, and the file needs to have a tally of the applications (counter) for that specific run.
I really haven't even broke ground on an actual script yet as everything I have researched thus far hasn't looked promising, So I figured I'd see if the guru's here have any idea's or input.
Thanks in advance for anything you can add to helping me make sense of what I'm trying to do here.
Upvotes: 0
Views: 170
Reputation: 74
You can start with something like the code below:
#Path to the files list. I'm assuming you will have one complete path per line.
$Paths = gc "c:\Temp\Paths.txt"
#Path to the log file
$logFile = "c:\Temp\Log.txt"
$counter = 0
foreach ($Path in $Paths)
{
$oFile = New-Object System.IO.FileInfo $Path
if ((Test-Path -Path $Path) -eq $false)
{
continue
}
try
{
$oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
if ($oStream)
{
$oStream.Close()
}
continue
}
catch
{
# file is locked by a process.
$fileLocked = (gci $Path).Name
$filesLocked = $filesLocked + " " + $fileLocked
$counter++
}
}
$out = [string]$counter + $filesLocked
Add-Content -Path $logFile -Value $out
See if this works.
Upvotes: 1