Topic "Powershell script to loop through a txt file to copy files"

Author Message
Tanveer Munavar
[View user's profile]

Joined: 2014-07-15
Posts: 2

Im trying to copy the files listed in the text file using the below code.
But im ending up with error message.Please suggest how to use the GetFiles() method to loop through a txt file to copy the files.Im a new bie here.Im using powershell to acccomplish this.

Get-Content C:\test.txt | ForEach-Object{$session.GetFiles("$remotePath/$_", $localPath_out , $False, $transferOptions)}

Below is my test.txt file


Error :
Transfers                                    Failures                                                                       IsSuccess
---------                                    --------                                                                       ---------
{/development/cop/data/OUTBOUND/COP_INTER... {WinSCP.SessionRemoteException: 'E:\32616...                                       False

Winscp 5.5.4 ; windows OS 7 SP1

Please find the attached full script(sample.txt).Im able to come till here in a week research.

You help will be really appreciated.
sample.txt (3.3 KB) [Download]

Description: Please find the full script.

Tanveer Munavar
[View user's profile]

Joined: 2014-07-15
Posts: 2
Im able to iterate through the list file.And able to copy by passing the file name in the $transferOptions.FileMask.
The below code is working perfectly fine.May be it needs some perfection and good approach.Please suggest if any .
Hope this helps someone like me.

if ($session.FileExists($remotePath)){
            $now = Get-Date
            Write-Host "file exists in this directory  --- $now `n  "

            # store list of files in the directory to session
            $directory = $session.ListDirectory($remotePath)
            $now = Get-Date
            write-host "creating source file list...  --- $now `n"

            #create a list file of the source directory
            $file_list = $directory.Files | Where {$_.Name -like $wildcard}
            $file_list.Name > C:\test.lst

            $now = Get-Date
            write-host "source file list has been created  --- $now `n"

            $now = Get-Date
            write-host "copying files in the source file list...  --- $now `n "

            #variable to hold the objects of the list file for copying
            $fileNameList = Get-Content C:\test.lst

            #copy file from source to destination
            foreach($fileName in $fileNameList){
                 $transferOptions.FileMask = $fileName
                 $transferResult = $session.GetFiles($remotePath, $localPath_out , $False, $transferOptions)
                 $transferResult = $session.GetFiles($remotePath, $localPath_archive , $False, $transferOptions)

                 #Iterate over every transfer
                 foreach ($transfer in $transferResult.Transfers){
                    # Success or error?
                    if ($transfer.Error -eq $Null){
                        $now = Get-Date
                        Write-Host ("copying of {0} succeeded --- $now `n" -f $transfer.FileName)
                        # copied files need to be moved to the archive folder
                        #Move-Item $transfer.FileName $archivePath
                        $now = Get-Date
                        Write-Host ("Copying of {0} failed: {1} --- $now `n" -f  $transfer.FileName, $transfer.Error.Message)

            #Throw on any error to emulate "option batch abort"
            exit 0
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 27106
Location: Prague, Czechia
I'm not a PowerShell expert, so I cannot tell why your original approach with "$remotePath/$_" did not work. What was a full error message you were getting?

Anyway your new approach with FileMask is pretty ineffective.
Consider trying
$transferResult = $session.GetFiles("$remotePath/$fileName", $localPath_out , $False, $transferOptions)

Also note that you should (with either approach) pass the $fileName through Session.EscapeFileMask:
Martin Prikryl

You can post new topics in this forum


What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!


About donations

$9   $19   $49   $99

About donations


WinSCP Privacy Policy

WinSCP License