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
Hi,

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.

Code:

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


Below is my test.txt file

Code:

shipment_0010000036_140308231803.csv
ARV_O_140211144818.csv
ARV_V1404212100.csv
ARV_V_11111.csv
ARV_O_140211144825.csv


Error :
Code:

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.

Code:

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
                    }
                    else{
                        $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"
            $transferResult.Check()
            exit 0
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24995
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:
https://winscp.net/eng/docs/library_session_escapefilemask
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

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!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License