Unexpected output for File-Folder name comparing

Advertisement

abd1111
Joined:
Posts:
14

Unexpected output for File-Folder name comparing

Hi! I hope someone can help me with this output I keep getting on my PowerShell 'put' files script.
With this script, I aim to take a group of folders on the local side, find the files that have 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.
For example:
File AAIndustries-OptOuts.csv needs to go to in the folder AAIndustries on the FTP side.

When I run what I have, I get
"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"
This is my main project code:
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()
}
Then, I simply checked the logic since it threw that error. I did:
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"
    }  
}
and it returned a list that like this:
'AACompanyName-OptOut.csv' and ' '
over and over with different file names.

I have no idea why it won't show the FTP folder name in the blank spot. I expect it to output AACompanyName folder for each file. Also, I don't get why I get the main error on the main block. Any clues?
  • testoptouts.txt (11.89 KB, Private file)
Description: The log file for the main script

Reply with quote

Advertisement

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

Re: Unexpected output for File-Folder name comparing

Here:
if ($optoutFile.Name.Contains($remoteFolder.Name)){
the $remoteFolder is a variable that you've never used before.
I do not know what exactly are you trying to do here.
Why don't you just extract the company name from the $optoutFile?

Or are you trying to validate if the company name folder exists? Then you need to search in the $remoteFolders.

Not a WinSCP question.

Reply with quote

Advertisement

You can post new topics in this forum