DarkW1nter
DarkW1nter

Reputation: 2861

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:\MyDir\Images' and on remote the root is '\Images'

1st push script - remote to intermediate:

 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:

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"
        }
    }

Upvotes: 0

Views: 128

Answers (1)

josuegomes
josuegomes

Reputation: 501

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

# 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/

Upvotes: 0

Related Questions