Please help

Advertisement

OSabbagh
Joined:
Posts:
2
Location:
Can

Please help

Hey all,

I'm completely new to programming / scripting / WINSCP and the world of IT

I have 4 objectives
-automatically do the below requirements without any user input needed
-download all files from folder in REMOTE folder, and EXCLUDE subfolders to LOCAL and delete on remote after success transfer
-Upload new files from local to REMOTE folder, and archive after success transfer

I've been playing with scripts/batches/powershell for the past week, and am finally giving up and asking for help.

I'm not familiar with WINSCP or that great at coding, would really appreciate the help.

I'm willing to pay if someone can bring me a solution as well.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
29,790
Location:
Prague, Czechia

Re: Please help

OSabbagh wrote:

-download all files from folder in REMOTE folder, and EXCLUDE subfolders to LOCAL and delete on remote after success transfer
See
https://winscp.net/eng/docs/faq_delete_synchronized_files
https://winscp.net/eng/docs/faq_script_non_recursive

-Upload new files from local to REMOTE folder, and archive after success transfer
See https://winscp.net/eng/docs/script_local_move_after_successful_upload

If you have problems with reusing these examples, please ask more specific questions.

Reply with quote

OSabbagh
Joined:
Posts:
2
Location:
Can

-download all files from folder in REMOTE folder, and EXCLUDE subfolders to LOCAL and delete on remote after success transfer
See
https://winscp.net/eng/docs/faq_delete_synchronized_files
https://winscp.net/eng/docs/faq_script_non_recursive


So i took your Extension "Keep Local Drectory Up to Date and modified it by adding
-delete after the Get in the code, is the below correct ???

# @name &Keep Local Directory up to Date...
# @command powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" -sessionUrl "!S" -localPath "%LocalPath%" -remotePath "%RemotePath%" %Delete% %Beep% %ContinueOnError% -interval "%Interval%" -pause -sessionLogPath "%SessionLogPath%"
# @description Periodically scans for changes in a remote directory and reflects them on a local directory
# @version 4
# @homepage https://winscp.net/eng/docs/library_example_keep_local_directory_up_to_date
# @require WinSCP 5.9.2
# @option - -run group "Directories"
# @option RemotePath -run textbox "&Watch for changes in the remote directory:" "!/"
# @option LocalPath -run textbox "... &and automatically reflect them on the local directory:" "!\"
# @option - -config -run group "Options"
# @option Delete -config -run checkbox "&Delete files" "" -delete
# @option Beep -config -run checkbox "&Beep on change" "" -beep
# @option ContinueOnError -config -run checkbox "Continue on &error" "" -continueOnError
# @option Interval -config -run textbox "&Interval (in seconds):" "30"
# @option - -config group "Logging"
# @option SessionLogPath -config sessionlogfile
# @optionspage https://winscp.net/eng/docs/library_example_keep_local_directory_up_to_date#options

param (
# Use Generate Session URL function to obtain a value for -sessionUrl parameter.
$sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/",
[Parameter(Mandatory = $True)]
$localPath,
[Parameter(Mandatory = $True)]
$remotePath,
[Switch]
$delete,
[Switch]
$beep,
[Switch]
$continueOnError,
$sessionLogPath = $Null,
$interval = 30,
[Switch]
$pause
)

try
{
# Load WinSCP .NET assembly
$assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot }
Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.ParseUrl($sessionUrl)

$session = New-Object WinSCP.Session

# Optimization
# (do not waste time enumerating files, if you do not need to scan for deleted files)
if ($delete)
{
$localFiles = Get-delete-ChildItem -Recurse -Path $localPath
}

try
{
$session.SessionLogPath = $sessionLogPath

Write-Host "Connecting..."
$session.Open($sessionOptions)
TransferOptions transferOptions = new TransferOptions();
transferOptions.FileMask = "|*/";

while ($True)
{
Write-Host "Synchronizing changes..."
$result =
$session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $delete)

$changed = $False

if (!$result.IsSuccess)
{
if ($continueOnError)
{
Write-Host "Error: $($result.Failures[0].Message)"
$changed = $True
}
else
{
$result.Check()
}
}

# Print updated files
foreach ($download in $result.Downloads)
{
Write-Host "$($download.Destination) <= $($download.FileName)"
$changed = $True
}

if ($delete)
{
# scan for removed local files (the $result does not include them)
$localFiles2 = Get-delete-ChildItem -Recurse -Path $localPath

if ($localFiles)
{
$changes =
Compare-Object -DifferenceObject $localFiles2 `
-ReferenceObject $localFiles

$removedFiles =
$changes |
Where-Object -FilterScript { $_.SideIndicator -eq "<=" } |
Select-Object -ExpandProperty InputObject

# Print removed local files
foreach ($removedFile in $removedFiles)
{
Write-Host "$removedFile deleted"
$changed = $True
}
}

$localFiles = $localFiles2
}

if ($changed)
{
if ($beep)
{
[System.Console]::Beep()
}
}
else
{
Write-Host "No change."
}

Write-Host "Waiting for $interval seconds, press Ctrl+C to abort..."
$wait = [int]$interval
# Wait for 1 second in a loop, to make the waiting breakable
while ($wait -gt 0)
{
Start-Sleep -Seconds 1
$wait--
}

Write-Host
}
}
finally
{
Write-Host "Disconnecting..."
# Disconnect, clean up
$session.Dispose()
}
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
}

# Pause if -pause switch was used
if ($pause)
{
Write-Host "Press any key to exit..."
[System.Console]::ReadKey() | Out-Null
}

# Never exits cleanly
exit 1





my only issue is that im trying to read and comprehend the code, $delete appears twice

from my understanding

if the setting delete is enabled then the function will run is this correct ?

This is found on line 52, and is the first statement after openning session.

if ($delete)
{
$localFiles = Get-delete-ChildItem -Recurse -Path $localPath
}




Also there's another $delete found on line 95, so is this where the setting is enabled it will run this function ??
im new to coding and have experience in VBA so i am able to comprehend some things.

if ($delete)
{
# scan for removed local files (the $result does not include them)
$localFiles2 = Get-delete-ChildItem -Recurse -Path $localPath

if ($localFiles)
{
$changes =
Compare-Object -DifferenceObject $localFiles2 `
-ReferenceObject $localFiles

$removedFiles =
$changes |
Where-Object -FilterScript { $_.SideIndicator -eq "<=" } |
Select-Object -ExpandProperty InputObject

# Print removed local files
foreach ($removedFile in $removedFiles)
{
Write-Host "$removedFile deleted"
$changed = $True
}
}

$localFiles = $localFiles2
}

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
29,790
Location:
Prague, Czechia

So i took your Extension "Keep Local Drectory Up to Date and modified it by adding
-delete after the Get in the code, is the below correct ???
I didn't suggest you to use that example. And I do not even see how that relates to your problem.
Also, your modification is wrong. It won't work at all.

Reply with quote

Advertisement

You can post new topics in this forum