powershellloopsforeachbatch-processingfile-move

Loop command to move multiple files


I have multiple files in a folder (C:\webfix) the folder has 23 items including random files and folders.

I have 70+ folders I need to push these files out to. Each folder looks like this: C:\saas\CU01313\wwwroot\

C:\saas\CU01316\wwwroot\

C:\saas\CU08453\wwwroot\

etc. etc.

The destination is all the same minus the CU0* part.

I would like to be able to mass move the 23 files/folders to each of these destinations, but I have not been able to figure out how.

After some research, I found that I might be better off using a 'foreach' loop command?

I have been trying to accomplish this in Powershell.

I have tried a couple of things which I can show the code below for. The first "script" uses the Involk-Expression command which I can get to work if I do it one by one. I have not been able to figure out how to "Wild Card" that \CU0*\ part.

First thing:

Invoke-Expression -Command "robocopy C:\webfix\ 'C:\saas\TT08931\wwwroot\' /e /b /COPYALL /MT:8 /r:2 /log:C:\log\log.txt "

If anyone could give me a hand with this I would be very grateful. Thank you very much!


Solution

  • Figure out a way to put all the CU0xxxx foldernames in a text file. Then do something like this.

    $folderlist = get-content C:\temp\Folderlist.txt
    
    foreach ($folder in $folderlist)
    {
        Copy-Item -Path  "C:\Webfix\*" -Destination "C:\saas\$folder\wwwroot\" -Recurse
    }