Powershell script with FIleZilla Pro CLI script embedded – how to properly iterate over local and remote paths

I have a scenario where I need to ftp thousands of files from a remote server to an intermediate – the intermediate has the ftp server installed – then from there to the destination server, Due to security I cannot ftp direct from remote to destination.

I have a Powershell script for each of the 2 steps, each script creates a FileZilla Pro CLI script per subdirectory.

My 1st script works as expected, iterating over all folders and subfolders, doing a put where newer or a different size.

The 2nd script I cannot get to work, it does not seem to work out the remote(intermediate) and local paths properly, or create folders locally if not existing(where they exist on remote).

I have included only the relevant parts of each script, as an example for the 2nd script, local root would be ‘F:MyDirImages’ and on remote the root is ‘Images’

1st push script – remote to intermediate:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> Add-DirectoryCommands -localPath $dirPath -remotePath $remoteDirPath -filezillaCommands ([ref]$filezillaCommands)
# Iterate over each subfolder and file in the current directory, including nested subfolders
Get-ChildItem -Path $dirPath -Recurse | ForEach-Object {
if ($_.PSIsContainer) {
# Create the directory on the remote server
$relativePath = $_.FullName.Substring($localDirectory.Length) -replace "\", "/"
$remoteSubDirPath = "$remoteDirPath/$relativePath"
Add-DirectoryCommands -localPath $_.FullName -remotePath $remoteSubDirPath -filezillaCommands ([ref]$filezillaCommands)
$dirsCreated++
} else {
# Increment the files checked counter
$filesChecked++
$globalFilesChecked++
# Prepare local and remote file paths
$localFile = $_.FullName -replace "\", "/"
$relativePath = $_.DirectoryName.Substring($dirPath.Length) -replace "\", "/"
$remoteFilePath = if ($relativePath) { "$remoteDirPath/$relativePath/" + $_.Name } else { "$remoteDirPath/" + $_.Name }
# Add file upload command
#log-message "$localFile to $remoteFilePath"
$filezillaCommands += "put --exists size_newer `"$localFile`" `"$remoteFilePath`""
$filesCopied++
$globalFilesCopied++
}
}
</code>
<code> Add-DirectoryCommands -localPath $dirPath -remotePath $remoteDirPath -filezillaCommands ([ref]$filezillaCommands) # Iterate over each subfolder and file in the current directory, including nested subfolders Get-ChildItem -Path $dirPath -Recurse | ForEach-Object { if ($_.PSIsContainer) { # Create the directory on the remote server $relativePath = $_.FullName.Substring($localDirectory.Length) -replace "\", "/" $remoteSubDirPath = "$remoteDirPath/$relativePath" Add-DirectoryCommands -localPath $_.FullName -remotePath $remoteSubDirPath -filezillaCommands ([ref]$filezillaCommands) $dirsCreated++ } else { # Increment the files checked counter $filesChecked++ $globalFilesChecked++ # Prepare local and remote file paths $localFile = $_.FullName -replace "\", "/" $relativePath = $_.DirectoryName.Substring($dirPath.Length) -replace "\", "/" $remoteFilePath = if ($relativePath) { "$remoteDirPath/$relativePath/" + $_.Name } else { "$remoteDirPath/" + $_.Name } # Add file upload command #log-message "$localFile to $remoteFilePath" $filezillaCommands += "put --exists size_newer `"$localFile`" `"$remoteFilePath`"" $filesCopied++ $globalFilesCopied++ } } </code>
 Add-DirectoryCommands -localPath $dirPath -remotePath $remoteDirPath -filezillaCommands ([ref]$filezillaCommands)

    # Iterate over each subfolder and file in the current directory, including nested subfolders
    Get-ChildItem -Path $dirPath -Recurse | ForEach-Object {
        if ($_.PSIsContainer) {
            # Create the directory on the remote server
            $relativePath = $_.FullName.Substring($localDirectory.Length) -replace "\", "/"
            $remoteSubDirPath = "$remoteDirPath/$relativePath"
            Add-DirectoryCommands -localPath $_.FullName -remotePath $remoteSubDirPath -filezillaCommands ([ref]$filezillaCommands)
            $dirsCreated++
        } else {
            # Increment the files checked counter
            $filesChecked++
            $globalFilesChecked++

            # Prepare local and remote file paths
            $localFile = $_.FullName -replace "\", "/"
            $relativePath = $_.DirectoryName.Substring($dirPath.Length) -replace "\", "/"
            $remoteFilePath = if ($relativePath) { "$remoteDirPath/$relativePath/" + $_.Name } else { "$remoteDirPath/" + $_.Name }

            # Add file upload command
            #log-message "$localFile to $remoteFilePath"
            $filezillaCommands += "put --exists size_newer `"$localFile`" `"$remoteFilePath`""
            $filesCopied++
            $globalFilesCopied++
        }
    }

2nd get script – intermediate to destination:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Get-ChildItem -Path $rootFolder.FullName -Recurse | ForEach-Object {
if ($_.PSIsContainer) {
# Create the directory on the remote server
$relativePath = $_.FullName.Substring($rootFolder.FullName.Length) -replace "\", "/"
$remoteDirPath = "./$relativePath"
$scriptContent += "mkdir $remoteDirPath" + "`n"
} else {
# Increment the files checked counter
$global:filesChecked++
# Prepare the download command
$localFile = $_.FullName -replace "\", "/"
$relativePath = $_.DirectoryName.Substring($rootFolder.FullName.Length) -replace "\", "/"
$remoteFilePath = if ($relativePath) { "/$relativePath/" + $_.Name } else { "/" + $_.Name }
log-message "$relativePath -- $remoteFilePath to $localFile"
$scriptContent += "get --exists size_newer `"$remoteFilePath`" `"$localFile`"" + "`n"
}
}
</code>
<code>Get-ChildItem -Path $rootFolder.FullName -Recurse | ForEach-Object { if ($_.PSIsContainer) { # Create the directory on the remote server $relativePath = $_.FullName.Substring($rootFolder.FullName.Length) -replace "\", "/" $remoteDirPath = "./$relativePath" $scriptContent += "mkdir $remoteDirPath" + "`n" } else { # Increment the files checked counter $global:filesChecked++ # Prepare the download command $localFile = $_.FullName -replace "\", "/" $relativePath = $_.DirectoryName.Substring($rootFolder.FullName.Length) -replace "\", "/" $remoteFilePath = if ($relativePath) { "/$relativePath/" + $_.Name } else { "/" + $_.Name } log-message "$relativePath -- $remoteFilePath to $localFile" $scriptContent += "get --exists size_newer `"$remoteFilePath`" `"$localFile`"" + "`n" } } </code>
Get-ChildItem -Path $rootFolder.FullName -Recurse | ForEach-Object {
        if ($_.PSIsContainer) {
            # Create the directory on the remote server
            $relativePath = $_.FullName.Substring($rootFolder.FullName.Length) -replace "\", "/"
            $remoteDirPath = "./$relativePath"
            $scriptContent += "mkdir $remoteDirPath" + "`n"
        } else {
            # Increment the files checked counter
            $global:filesChecked++

            # Prepare the download command
            $localFile = $_.FullName -replace "\", "/"
            $relativePath = $_.DirectoryName.Substring($rootFolder.FullName.Length) -replace "\", "/"
            $remoteFilePath = if ($relativePath) { "/$relativePath/" + $_.Name } else { "/" + $_.Name }
           log-message "$relativePath -- $remoteFilePath to $localFile"
           $scriptContent += "get --exists size_newer `"$remoteFilePath`" `"$localFile`"" + "`n"
        }
    }

1

Without the generated script, it’s hard to know what is cause.
Also, you can enable logs to debug the problem:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># enable info level logging
set engine.log.debug_level info
# note that you have to provide the full path to the file
set engine.log.file C:\Users\me\fzprocli.logs
set engine.log.detailed 1
</code>
<code># enable info level logging set engine.log.debug_level info # note that you have to provide the full path to the file set engine.log.file C:\Users\me\fzprocli.logs set engine.log.detailed 1 </code>
# enable info level logging
set engine.log.debug_level info

# note that you have to provide the full path to the file
set engine.log.file C:\Users\me\fzprocli.logs
set engine.log.detailed 1

Have you considered using file synchronization? See the documentation for the sync command at https://filezillapro.com/docs/v3/filezilla-pro-cli/file-synchronization-cli/

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật