Share via

Robocopy automatically taking source directory as the script file directory

Anonymous
2022-07-25T09:48:58+00:00

Synopsis:

- Script file directory name -> PowerShellScripts (in Desktop in local C:...)

- Script file name -> Dbackup.ps1

- Intended source for mirroring -> D:\ (a partition on local hard disk)

- Intended destination for mirroring -> F:\D_LAPTOP_BACKUP_ ... (F: is external SSD connected via USB)

Script:

[string]$currentDirectory = Get-Location

[DateTime]$currentDateTime = Get-Date

[string]$currentTimeStamp = $currentDateTime.ToString("yyyy-MM-dd_HH-mm-ss")

[string]$currentDayStamp = $currentDateTime.ToString("yyyy-MM-dd")

[string]$sourcePath = "D:"

write-host "Source Path: " $sourcePath

[string]$destinationDrive = "F:"

[string]$destinationFolderName = "\D_LAPTOP_BACKUP_"

[string]$destinationPath = $destinationDrive + $destinationFolderName + $currentDayStamp

write-host "Destination Path: " $destinationPath

[string]$logFile = ""

[string]$logFileExtenstion = ".txt"

$logFile = $currentDirectory + $destinationFolderName + $currentTimeStamp + $logFileExtenstion

write-host "Log File: " $logFile

[string]$defaultDirExclusionSet = '$RECYCLE.BIN "D:\System Volume Information"'

write-host "Dir Excluded: " $defaultDirExclusionSet

[string]$commandString = "Robocopy $sourcePath $destinationPath /MIR /R:3 /W:3 /DCOPY:DAT /COPY:DATSOU /TEE /LOG:$logFile /MT:32 /XD $defaultDirExclusionSet"

write-host $commandString

Invoke-Expression $commandString

Issue:

When this script is run by opening PowerShell in Admin mode from PowerShellScripts directory (which is in Desktop) it runs fine.

However, when I move this PowerShellScripts directory to D:\ and this script is run by opening PowerShell in Admin mode from now moved PowerShellScripts directory, the source for mirror is no more D:\ (when the robocopy expression is run by this script). The source automatically changes to "D:\PowerShellScripts" by robocopy. Note source is hardcoded in script and $commandString prints properly on console and shows the intended correct expression and source.

I am confused. It was scary when it happened yesterday (/MIR used) but luckily destination was kept F:\D_LAPTOP_BACKUP_2022-07-24\ by robocopy. Kindly help.

Result Header:


   ROBOCOPY     ::     Robust File Copy for Windows                              


  Started : 24 July 2022 11:04:51 PM

   Source : D:\PowerShellScripts\

     Dest : F:\D_LAPTOP_BACKUP_2022-07-24\

    Files : *.*

    

 Exc Dirs : D:\System Volume Information

    

  Options : *.* /TEE /S /E /COPYALL /PURGE /MIR /MT:32 /R:3 /W:3 


Windows for home | Windows 10 | Files, folders, and storage

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-07-26T01:29:13+00:00

    Hi

    Welcome to Microsoft community.

    Your problem is beyond the scope of the daily support of the forum. In order to better solve your problem. Here I recommend docs team. Where are more experts solve problems for you.

    Best regards

    Derrick Qian | Microsoft Community Support Specialist

    Was this answer helpful?

    0 comments No comments