Robocopy + Powershell Variable

Tyrone Hirt 41 Reputation points
2022-10-18T17:58:57.957+00:00

I have a robocopy script that works very well so far, the point is that I would like to make it a little more advanced, and I believe the way is to integrate powershell into it.

My current script:

chcp 1252  
set /p dummy=Tecle ENTER para continuar  
  
robocopy  /e /zb /v /sec /copyall "E:\My Company - Aaron Trevor\02. Clients" "\\192.168.0.130\Backup\Aaron Trevor" /xd "03. Proxy" /xf *.avi  
  
set /p dummy=Backup efetuado com Sucesso! Tecle ENTER para finalizar.  

As you can see, the Backup folder path is hard-coded in the code, what I wanted is for it to be flexible according to the SSD name.

I would like the script to detect the name after the dash and copy the files to the folder with the contributor's name inside the backup folder.

For example, if the SSD name is "E:\My company - Aaron Trevor" the script will copy the files to the folder "\192.168.0.130\Backup\Aaron Trevor"

Why?

Because if at some point the backup path changes, I will have to create a specific backup file for each contributor instead of creating just one and asking everyone to replace the file at the root of the SSD.

If you can help me with this, I would be very grateful!

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-10-18T18:58:41.077+00:00

    How about something like this?

    $var1 = "E:\My Company - Aaron Trevor\02. Clients"  
    $var2 = ""  
    if ($var1 -match "\s-\s(.+)\\"){  
        $var2 = $Matches[1]  
        robocopy  /e /zb /v /sec /copyall "$var1" "\\192.168.0.130\Backup\$var2" /xd "03. Proxy" /xf *.avi  
    }  
    else{  
        Write-Host "Didn't find expected pattern in '$var1'" -Foreground Yellow  
    }  
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2022-10-20T23:22:02.223+00:00

    "%~dp0" is the current directory, so by doing a CD to it, you are changing to the directory you are already in. That does nothing.

    powershell.exe -ExecutionPolicy Bypass -File "PowerShell Test.ps1"

    There is no need to have powershell launch powershell to run the script. Use the dot slash notation run the script from the current directory.

    powershell.exe -ExecutionPolicy Bypass -File .\Test.ps1

    Did you apply any auditing ACL's to the file system? If not, there is nothing to copy. Replace /copyall with /sec. Or if you require the owner use /COPY:DATSO

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.