Search code examples
powershellwinscpwinscp-net

Creating a directory structure which may partially exist with WinSCP 409 Conflict


Im using WinSCP + Powershell to automatically transfer the build result of Jenkins to our shared server so it can easily be accessed by QA and Dev teams.

WinSCP is required because it can only be transferred via the Webdav protocol. Dont worry about it!!

Lets say the directory on the remote server that i need to create is

MyProduct/BuildOutput/bin

Now if i perform the below command a with a totally missing folder structure it works perfectly.

$session.CreateDirectory("MyProduct/BuildOutput/bin")

However if say the "MyProduct/BuildOutput" structure already exists but the "bin" is missing the WinSCP gives a 409 Conflict error.

Is there any way to create a full directory structure that already partially exists without having to write a complex program to do this?


Solution

  • WinSCP CreateDirectory method does not support the whole directory structure creating. Just single directory. You will have to build the structure on your own. Try this way:

        [Array]$dir = 'hello/this/is/test/directory/structure' -split '/'
        $path = [System.String]::Empty
        for ($i=0; $i-lt $dir.Count; $i++){
            if ($i -lt $dir.Count){$path+= $dir[$i] + '/'}
            $session.CreateDirectory($path)
        }