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
For example:
File
When I run what I have, I get
Then, I simply checked the logic since it threw that error. I did:
and it returned a list that like this:
I have no idea why it won't show the FTP folder name in the blank spot. I expect it to output
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
This is my main project code:"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" } }
over and over with different file names.'AACompanyName-OptOut.csv' and ' '
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?