Powershell script to upload files and exclude some

Advertisement

gothbox
Joined:
Posts:
7

Powershell script to upload files and exclude some

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 ?

Reply with quote

Advertisement

gothbox
Joined:
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.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,453
Location:
Prague, Czechia

Re: Powershell script to upload files and exclude some

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:
$transferOptions.FileMask = "|e2b_0170*.xml,e2b_0280*.xml,e2b_0515*.xml"

Reply with quote

gothbox
Joined:
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 ?

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,453
Location:
Prague, Czechia

You are not passing the $transferOptions to $session.PutFiles.

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

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum