Home > Software design >  wildcards in directory path for transferring the files from source to destination using powershell
wildcards in directory path for transferring the files from source to destination using powershell

Time:05-04

I have to fetch the files from SFTP account from directory "/sourcedir/Test/TRANS*/transferfiles.csv" using PUTTY and transfer them over to my local destination dir. Having trouble using the wildcard "TRANS*" in the directory path. How do i use multiple wildcards in the directory path?? I'm getting the error "/sourcedir/Test/TRANS*/*transferfiles*.csv": multiple-level wildcards unsupported.". TIA

[string]$TransferResults = & 'C:\Program Files\PuTTY\pscp.exe' -l 'username' -pw 'password' "username@$(IPaddress):/sourcedir/Test/TRANS*/*transferfiles*.csv" $Destination

I tried the solution that @Cpt.Whale suggested. Output:

Listing directory /sourcedir/Test/                                                                                 drwxr--r--   1        -        -        0 Apr 28 14:43 TRANS_whiteplain         drwxr--r--   1        -        -        0 Apr 28 14:43 TRANS_test_1

Code snippet to parse in foreach loop.

 foreach($file in $parsedfolders){
    [string]$fpath = $file.fullName
    [string]$TransferResults = & 'C:\Program Files\PuTTY\pscp.exe' -l 'username' -pw 'password' "username@$(IPaddress):$fpath/*transferfiles*.csv" 
$Destination

i get the error : unable to identify /transferfiles.csv: no such file or directory

CodePudding user response:

I'll assume your output from pscp -ls looks like this, based on your comment:

Listing directory /sourcedir/Test/                                                                                 
drwxr--r--   1        -        -        0 Apr 28 14:43 TRANS_whiteplain         
drwxr--r--   1        -        -        0 Apr 28 14:43 TRANS_test_1
drwxr--r--   1        -        -        0 Apr 28 14:43 TRANS test spaces

From that output, we can use regex to get the folder names:

# First, list the folder names in the upper level folder
$folders = & 'C:\Program Files\PuTTY\pscp.exe' -l 'username' -pw 'password' -ls "[email protected]:/sourcedir/Test/"

# only lines starting with d, select everything after time "0:00"
$pattern = '^d. \d:\d{2} (.*)' 

# parse output into folder names using regex
$parsedFolders = foreach ($folder in $folders) {
  # trim whitespace
  [regex]::Match($folder,$pattern).Groups[1].Value.trim() |
    # discard empty results
    Where { -not [string]::IsNullOrWhiteSpace($_) }
}

Now the parsed folders should be usable:

$parsedFolders

TRANS_whiteplain
TRANS_test_1
TRANS test spaces

So try and do your copy for each folder:

# Finally, copy files from each parsed folder to destination
$results = foreach ($parsedFolder in $parsedFolders) {
  & 'C:\Program Files\PuTTY\pscp.exe' -l 'username' -pw 'password' "[email protected]:/sourcedir/Test/$parsedfolder/*transferfiles*.csv" $Destination 
}
  • Related