Re: Unexpected output for File-Folder name comparing
Thanks! I'm still on a PowerShell learning curve.
Looking to donate at some point soon.
Looking to donate at some point soon.
if ($optoutFile.Name.Contains($remoteFolder.Name)){
$remoteFolder
is a variable that you've never used before.
$optoutFile
?
$remoteFolders
.
OptOut
in their name, and put them in a folder on the FTP side that matches the company name in the file name on the local side with the folder name.
AAIndustries-OptOuts.csv
needs to go to in the folder AAIndustries
on the FTP side.
"Exception calling "Check" with "0" argument(s): "Error transferring file 'C:\Users\UserName\Company New\Group Name 1\Group name\ReportNames\EE-Elections\AACompany-OptOutsReport-20240105.csv'.
Copying files to remote side failed.
Invalid filename '"'."
At C:\Users\Username\Downloads\FTPPowershellFiles\Opt_Outs.ps1:49 char:22
+ ... $session.PutFiles($optoutFile.FullName, $remoteFolder.Ful ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException"
try
{
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = ""
UserName = ""
Password = ""
FtpSecure = [WinSCP.FtpSecure]::Implicit
}
$session = New-Object WinSCP.Session
Remove-Item "C:\Users\Username\OneDrive - Company Name 1\Documents\FTP Project LOGS\testoptouts.log"
$session.SessionLogPath = "C:\Users\Username\OneDrive - Company Name 1\Documents\FTP Project LOGS\testoptouts.log"
$session.Open($sessionOptions)
$remoteFolders =
$session.ListDirectory( "/1 - CompanyName/Name/3 - Client Folders/HealthCoFolders/Test/")
$localchildItems =
Get-ChildItem -Path "C:\Users\Username\Company\Group\Group List\HealthcareServices-82\02-Reports\" -Recurse
$optoutFiles =
foreach ($localchildItem in $localchildItems){
Get-ChildItem -Path $localchildItem.FullName | Where-Object {$_.Name -like "*OptOut*"}
}
foreach($optoutFile in $optoutFiles){
if ($optoutFile.Name.Contains($remoteFolder.Name)){
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*.*"
$transferOptions.AddRawSettings("NewerOnly","1")
$session.PutFiles($optoutFile.FullName, $remoteFolder.FullName, $false, $transferOptions).Check()
}
else {
Write-Output "None found"
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
foreach($optoutFile in $optoutFiles){
if ($optoutFile.Name.Contains($remoteFolder.Name)){
Write-Host "$($optoutFile.Name) and $($remoteFolder.Name)" | Where-Object {$optoutFile.Name.Contains($remoteFolder.FullName)}
}
else {
Write-Output "None found"
}
}
'AACompanyName-OptOut.csv' and ' '
AACompanyName
folder for each file. Also, I don't get why I get the main error on the main block. Any clues?