Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Yes it should work. Regarding the antivirus application: Try to turn off antivirus temporarily and see if it makes a difference.
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?
martin

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.
Ajax

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. :(
Ajax

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
}