question

AkshaySolanki-2006 avatar image
0 Votes"
AkshaySolanki-2006 asked MotoX80 edited

Copy folders using csv in powershell

Hello,
I am fairly new to powershell with little to no experience in scripting. I have a task to copy folders from source to destination but it has some tricky part in it.
I have one source folder C:\source\ where all the folders and subfolders are resided.
133732-issue1.png


And I have a csv file where I have two separate columns to copy from source to destination.

C:\dest\company1\

C:\dest\company2\

IE two different companies have folders in same source folder so need to copy them and paste it in different destination folders.
133712-issue2.png



Here is the code I tried with some understanding from the internet.

 $source       = Get-Childitem "C:\source"
 $destination1 = "C:\dest\company1\"
 $destination2 = "C:\dest\company2\"
 $csv          = import-csv "C:\Scripts\copytask.csv"
    
 foreach ($row in $csv) {
     if($source -contains $row.company1){
         robocopy "C:\source\$($row.company1)*" $destination1 /E 
     }
     elseif($source -contains $row.company2){
         robocopy "C:\source\$($row.company2)*" $destination2 /E 
     }
 }


but I am not getting any output when I run the script and not copying as well.

windows-server-powershell
issue1.png (18.9 KiB)
issue2.png (5.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered MotoX80 edited

If your images are correct, then $($row.company1) will contain '11', but the actual folder name is 'folder(11)'.

You need to look at the folder names.

  if(($source).Name -contains $row.company1){

With robocopy, you need to put the folder names in both the source and destination parameters. I don't think that you want to just use $destination1 as the second parameters

Here is my reply to your first post.

I didn't fully test this, but give it a try. I added "/l" to robocopy so that it didn't spam your file system if the names were wrong. This assumes that you want to copy c:\Source\Folder11 to c:\Dest\Company1\Folder11.

 $source =  "C:\source"
 $destination1 = "C:\dest\company1\"
 $destination2 = "C:\dest\company2\"
 $csv = import-csv "C:\Scripts\copytask.csv"
 $srcFolders = Get-ChildItem -Path $source
        
  foreach ($folder in $SrcFolders) {      
     foreach($row in $csv) {
         if($Folder.name -match $row.company1 ) {                # does 'folder(11)' contain '11'?    
             robocopy "C:\source\$($Folder.Name)" "$destination1$($folder.Name)" /E /L    
         }
      }    
  }

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
That worked beautifully as needed. Thank you so much for helping.
I really appreciate it.

Kind regards,
Akshay

0 Votes 0 ·

It is not copying from the folders that are listed in csv.
For example in my csv, I dont have number 048360 mentioned to copy files from but when I run the script, it gets stuck at that folder location.
A folder with that number in folder name does exist in source but I dont want it still it gets stuck there and doesnt copy anything from the source. Not even the other files from other folders too.

I was thinking to add a variable and use regex to extract the number from the folder name and compare that with the csv list numbers.
Do you know how can I achieve this in my code?

0 Votes 0 ·
MotoX80 avatar image MotoX80 AkshaySolanki-2006 ·

For example in my csv, I dont have number 048360 mentioned to copy files from but when I run the script, it gets stuck at that folder location.

What do you mean by "stuck"?

I don't know what entries you have in your csv.
I don't know folders you have on your file system.

I would suggest that instead of actually calling robocopy, just write out the command that you would execute so that you can see what commands the script generates. Maybe display the folder name that the script is processing too.

 "robocopy C:\source\$($Folder.Name) $destination1$($folder.Name) /E /L " 


0 Votes 0 ·