Powershell script to loop through a txt file to copy files

Advertisement

Tanveer Munavar
Joined:
Posts:
2

Powershell script to loop through a txt file to copy files

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.

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

Below is my test.txt file

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

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.
Description: Please find the full script.

Reply with quote

Advertisement

Tanveer Munavar
Joined:
Posts:
2

Powershell script to loop through a txt file to copy files

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

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: Powershell script to loop through a txt file to copy files

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 RemotePath.EscapeFileMask:
https://winscp.net/eng/docs/library_remotepath_escapefilemask

Reply with quote

Advertisement

You can post new topics in this forum