Batch File to Upload Multiple Directory to Ftp Server

jeroenbudding
Joined:
Posts:
2
Location:
Netherlands

So the problem is that I take 40 files in a directory. Add the moment if I employ my Script which I volition post below it will upload all the files but information technology takes ages.
And then what I want is that the script downloads 6 or 8 files add the aforementioned fourth dimension. Is this possible and if so how can I do it.

#Upload information to LivingStone SFTP attempt {     # Load WinSCP .NET assembly Rember The binder must contain winscpnet.dl and WinSCP.exe     Add-Type -Path "C:\Powershell\LivingStone_SFTP\WinSCPnet.dll"       # Setup session options     $sessionOptions = New-Object WinSCP.SessionOptions     $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp     $sessionOptions.HostName =      $sessionOptions.UserName =      $sessionOptions.Password =      $sessionOptions.SshHostKeyFingerprint =        $session = New-Object WinSCP.Session       try     {         # Connect         $session.Open($sessionOptions)           # Upload files         $transferOptions = New-Object WinSCP.TransferOptions         $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary         $TransferOptions.PreserveTimestamp = $simulated         $TransferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::on                            #here we define source and destination         $transferResult = $session.PutFiles("D:\Backups\SCCM\*", "/inbox/", $False, $transferOptions)            # Throw on any error                 $transferResult.Cheque()           # Print results         foreach ($transfer in $transferResult.Transfers)         {             Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)         }     }     finally     {         # Disconnect, clean up         $session.Dispose()     }       get out 0 } grab [Exception] {     Write-Host $_.Exception.Message     exit 1 }[/quote]

Respond with quote

martin◆
Site Admin
martin avatar
Joined:
Posts:
37,089
Location:
Prague, Czechia

Respond with quote

jeroenbudding
Joined:
Posts:
2
Location:
Netherlands

Prikryl thanks for your help in the good direction only that script is downloading not uploading.
I adjusted the script but still get the post-obit mistake when I run the script:
Downloading C:\Powershell\LivingStone_SFTP\Test\4.exe to /inbox/4.exe in two
Can't get attributes of file 'C:\Powershell\LivingStone_SFTP\Examination\four.exe'.
Script now is as followed:

#Powershell script to upload the SCCM SQL Database, Active Directory Computers and users param (  $sessionUrl = "sftp://user:password;fingerprint=ssh-rsa-xx-xx-xx@example.com/", $remotePath = "/inbox",  $localPath = "",  $batches = iii )  #Upload data to LivingStone SFTP  try  {      # Load WinSCP .NET assembly      $dllPath = (Join-Path $PSScriptRoot "WinSCPnet.dll")     # Load WinSCP .Internet assembly     Add-Type -Path $dllPath    # Setup session options     $sessionOptions = New-Object WinSCP.SessionOptions     $sessionOptions.ParseUrl($sessionUrl)       $started = Get-Date       try     {         # Connect         Write-Host "Connecting..."         $session = New-Object WinSCP.Session         $session.Open($sessionOptions)                  # Retrieve list of files and sort them from larges to smallest         $files = Get-ChildItem $localPath | Sort-Object Length -Descending           # Summate full size of all files         $total = ($files | Mensurate-Object -Property Length -Sum).Sum                  # And batch size         $batch = [int]($total / $batches)           Write-Host ("Will upload {0} files totaling {1} bytes in {2} parallel batches, {iii} bytes on boilerplate in each" -f $files.Count, $total, $batches, $batch)                  $showtime = 0         $sum = 0         $no = 0           for ($i = 0; $i -lt $files.Count; $i++)         {             $sum += $files[$i].Length               # Institute enough files for the next batch             if (($sum -ge $batch) -or ($i -eq $files.Count - 1))             {                 Write-Host ("Starting batch {0} to upload {1} files totaling {2}" -f $no, ($i - $showtime + 1), $sum)                                  $fileList = $files[$first..$i] -join ";"                                  # Start the background task for the batch                 Start-Chore -Name "Batch $no" -ArgumentList $dllPath, $sessionUrl, $localPath, $remotePath, $no, $fileList {                     param (                         [Parameter(Position = 0)]                         $dllPath,                         [Parameter(Position = 1)]                         $sessionUrl,                         [Parameter(Position = 2)]                         $localPath,                         [Parameter(Position = 3)]                         $remotePath,                         [Parameter(Position = four)]                         $no,                         [Parameter(Position = 5)]                         $fileList                     )                       try                     {                         Write-Host ("Starting batch {0}" -f $no)                           # Load WinSCP .Net assembly.                         # Need to use an accented path as the Chore is started from user's documents binder.                         Add-Type -Path $dllPath                           # Setup session options                         $sessionOptions = New-Object WinSCP.SessionOptions                         $sessionOptions.ParseUrl($sessionUrl)                                                  try                         {                             Write-Host ("Connecting batch {0}..." -f $no)                             $session = New-Object WinSCP.Session                               $session.Open up($sessionOptions)                                                          $files = $fileList -split ";"                               # Download the files selected for this batch                             foreach ($file in $files)                             {                                 $localFilePath = "$localPath\$file"                                 $remoteFilePath = "$remotePath/$file"                                 Write-Host "Downloading $localFilePath to $remoteFilePath in $no"                                   $session.GetFiles($session.EscapeFileMask($localFilePath), $remoteFilePath).Check()                             }                         }                         finally                         {                             # Disconnect, make clean up                             $session.Dispose()                         }                                                  Write-Host ("Batch {0} done" -f $no)                     }                     grab [Exception]                     {                         Write-Host $_.Exception.Message                         go out i                     }                 } | Out-Null                   # Reset for the side by side batch                 $no++                 $sum = 0                 $start = $i + i             }         }           Write-Host "Waiting for batches to consummate"         Become-Chore | Receive-Job -Await           Write-Host "Done"           $concluded = Go-Engagement         Write-Host ("Took {0}" -f (New-TimeSpan -Start $started -Finish $ended))     }     finally     {         # Disconnect, clean up         $session.Dispose()     }       get out 0 } catch [Exception] {     Write-Host $_.Exception.Message     get out 1 }

Answer with quote

martin◆
Site Admin
martin avatar

You have to at least supercede the GetFiles with PutFiles.

Respond with quote

carpenterthisidiever1981.blogspot.com

Source: https://winscp.net/forum/viewtopic.php?t=22800

0 Response to "Batch File to Upload Multiple Directory to Ftp Server"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel