Topic "Powershell script to upload files and exclude some"

Author Message
gothbox
[View user's profile]

Joined: 2014-02-26
Posts: 7
Hi.

Im trying to upload all files exepc some files based on its name.

Ive made this script, and it does quite well, except the exclusion...


try
{
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("WinSCPnet.dll") | Out-Null

# Setup session options
$SessionOptions.DisableVersionCheck
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::ftp
$sessionOptions.HostName = "localhost"
$sessionOptions.UserName = "ftp1"
$sessionOptions.Password = "ftp1"
# $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"

$session = New-Object WinSCP.Session

try
{
# Connect
$session.Open($sessionOptions)

$localPath = "\\10.0.0.105\asdf\*"
$remotePath = "/asdf/efh/"
$backupPath = "\\10.0.0.105\asdf\ok\"

# Upload files, collect results
#$TransferOptions()
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "|e2b_0170*.xml","|e2b_0280*.xml","|e2b_0515*.xml"
$transferResult = $session.PutFiles($localPath, $remotePath)

# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host ("Upload of {0} succeeded, moving to backup" -f
$transfer.FileName)
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f
$transfer.FileName, $transfer.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}




Can anyone help me ?
Advertisements
gothbox
[View user's profile]

Joined: 2014-02-26
Posts: 7
It transfers all files and moves them after sucsess.

Why wont it exclude the files named e2b_0170* , e2b_0280* , e2b_0515* ?

I've used the same exclusion when calling a script file from winscp.com and it does the job correct, but it doesnt move the files after, so i had to make a powershell script.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
I'm no expert in PowerShell, but your string syntax for file mask seems wrong to me. And you definitely have too many "pipes" there. Try:
Code:
$transferOptions.FileMask = "|e2b_0170*.xml,e2b_0280*.xml,e2b_0515*.xml"
gothbox
[View user's profile]

Joined: 2014-02-26
Posts: 7
Hi.

changed the line, but still it copies all files in the folder.


It seems like it ignores the filemask option completely...

I've also tries to replace the line:
$transferOptions.FileMask = "|e2b_0170*.xml,e2b_0280*.xml,e2b_0515*.xml"
to :
$transferOptions.FileMask = "*<1d"

to have it only transfer files older then 24 hours, and still, all files are beeing transfered.

I'm new to powerhell myself. There are no errors, output says
"........\e2b_02282014020656623.xml succeeded, moving to backup"

I've prettymuch copypasted the script from https://winscp.net/eng/docs/script_local_move_after_successful_upload

Is there something preventing filemask optin to be read ?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
You are not passing the $transferOptions to $session.PutFiles.

You should do:
Code:
$transferOptions.FileMask = "|e2b_0170*.xml","|e2b_0280*.xml","|e2b_0515*.xml"
$transferResult = $session.PutFiles($localPath, $remotePath, $False, $transferOptions)

_________________
Martin Prikryl
gothbox
[View user's profile]

Joined: 2014-02-26
Posts: 7
HAH!

Thank you so much!

Works like a charm Smile
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License