Access is denied error on every script second run on his own folder

Advertisement

Ajax
Guest

Access is denied error on every script second run on his own folder

Hello, I have strange problem with WinSCP asssembly and powershell script. In script, I want to create tmp folder, upload files, rename old target directory, rename tmp to target and remove old. In first run, it works, in second run, library reports error
Access is denied
on folder, which created few moments ago. And on third run, it removes this folder, 4th run access denied, and so on.. Look into attachment, there is console and logs for good and bad run I can't find error, can someone help me?

Here is code:
$FTPHost = 'someserver'
$FTPUser = 'someuser'
$FTPPass = '*******'
$srcFolder = "D:\testUploadData"
$destPath = 'testFolder'
 
 
$destTmpPath = "${destPath}.TMP"
$destOldPath = "${destPath}.OLD"
 
try {
    # Load WinSCP .NET assembly
    Add-Type -Path (Join-Path $PSScriptRoot "..\winscp_assembly\WinSCPnet.dll")
 
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Ftp
        HostName = $FTPHost
        UserName = $FTPUser
        Password = $FTPPass
    }
 
    $session = New-Object WinSCP.Session
    $session.SessionLogPath = 'log.txt'
 
    try {
        Write-Host("Connecting to FTP")
        $session.Open($sessionOptions)
        
        Write-Host("Creating .TMP dir")
        if($session.FileExists($destTmpPath)) {
            $session.RemoveFiles($destTmpPath).Check()
        }
 
        $session.CreateDirectory($destTmpPath)
        
        Write-Host("Uploading to .TMP dir")
        $session.PutFiles($srcFolder, $destTmpPath).Check()
        
        Write-Host("Renaming live dir to .OLD")
        if(!$session.FileExists($destPath)) {
            $session.CreateDirectory($destPath)
        }
 
        if($session.FileExists($destOldPath)) {
            $session.RemoveFiles($destOldPath).Check()
        }
        
        $session.MoveFile($destPath, $destOldPath)
 
        Write-Host("Renaming .TMP dir to live")
        $session.MoveFile($destTmpPath, $destPath)
        
        Write-Host("Removing .OLD dir")
        $session.RemoveFiles($destOldPath).Check()
    }
    finally {
        $session.Dispose()
    }
 
    exit 0
}
catch {
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

Description: Console with multiple runs

console.png

  • log_goodRun.txt (18.64 KB, Private file)
Description: log for good run
  • log_badRun.txt (11.77 KB, Private file)
Description: log for bad run

Reply with quote

Advertisement

Ajax
Guest

Partial solve. Problem was when directory was not empty. I thought that MoveFile was recursive. So, there is no option to do atomic rename of big folder (rename only parent) and I have to rename all files or do SynchronizeDirectories? It leads to inconsistent directory for big folder.. It looks like I'll have to use something else. :(

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
33,710
Location:
Prague, Czechia

Re: Access is denied error on every script second run on his own folder

This has nothing to do with WinSCP.
Also, IIS can rename folders that contain files.
If it does not work, it's probably because something has the folder locked. Isn't there some server-side process that processes the uploaded files, which did not finish yet at the time of the next run? It can even simply be an antivirus application.

Reply with quote

Guest

Isn't there some server-side process that processes the uploaded files, which did not finish yet at the time of the next run?
No, this is test folder created for me without access.

It can even simply be an antivirus application.
Hmmm... interesting idea, How can I check?

At least, is my approach/idea right? This should work like I expect?

Reply with quote

martin
Site Admin
martin avatar

Yes it should work. Regarding the antivirus application: Try to turn off antivirus temporarily and see if it makes a difference.

Reply with quote

Advertisement

You can post new topics in this forum