Generate robocopy jobs from foldername automatically

The estimated reading time 3 minutes

Generate robocopy jobs from foldername automatically

Hi there,

from time to time I’m migrating some small fileservers. Often I have to create a hole new folder structure for the new fileserver. When the structure is available of course I have to migrate the data, by using robocopy. But it is difficult to copy all data to the right place in the new environment. Therefor I created a small excel to generate the hole robocopy with parameters.

From this execl I’m able to generate a lot of robocopy batches within secondes.

Lets see my example (of course you have to merge the old and the new folder name, otherwise excel does not know how to generate the command):

=(""&B3&"|robocopy  ""\\oldfile\share\"&A3&""" ""\\newfile\share\"&B3&"""  /MIR /COPY:DAT /DCOPY:T /R:2 /W:3 /LOG+:""\\someserver\logs\%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%-"&B3&".log"" /TEE /NDL")

As you can see I can easily create the robocopy commands in excel

The column csv robocopy job generates the content for the future csv file that powershell can read and interpret.

You can change in a special tempfolder to generate the csv file. Just create an empty textfile with the following “header” inside the file. „name|robocopyjob“

NOTE: there should be no spaces or something else in the header of the csv file, otherwise delimiter will not work properly.

Let’s see my csv file

Please change the directory from the open powershell to the special temp directory to import the csv file. In my case I use the ISE.

First step is to import the csv content. Please have a look at the filename you gave.

$jobs = @()
$jobs = (Import-Csv -Path .\robocopy-csvimportfile.csv -Delimiter "|")
$jobs

The next step is to execute the rest of the script to generate the robocopy batch files.

foreach ($job in $jobs){

  $name = ($job.name)
  $robocopy = ($job.robocopyjob)
  New-Item -Path .\ -Name "$name.cmd" -ItemType File -Force
  Get-Item -Path .\$name.cmd | Add-Content -Value "chcp 1252"
  Get-Item -Path .\$name.cmd | Add-Content -Value "$robocopy"
  Get-Item -Path .\$name.cmd | Add-Content -Value "pause"

}

With this script you generate one batchfile per job in the directory you browsed. That’s it.

Here is the hole script with some more features.

#define codepage (utf8 code would be 65001)
$codepage  = "1252"
#create array for csv import 
$jobs = @()
$jobs = (Import-Csv -Path .\robocopy-csvimportfile.csv -Delimiter "|")
#write all robocopy jobs to variable 
$all = (($jobs.robocopyjob))
#generate new cmd file 
New-Item -Path .\all.cmd -ItemType File -Force
#set encoding to west european latin (utf8 code would be 65001)
Get-Item -Path .\all.cmd | Add-Content -Value "chcp $codepage"
#adding content from $all to cmd file
$all | Out-File -FilePath .\all.cmd -Append -Encoding utf8 -Force
Get-Item -Path .\all.cmd | Add-Content -Value "pause"
#foreach loop to generate the seperate robocopy jobs
foreach ($job in $jobs){

  $name = ($job.name)
  $robocopy = ($job.robocopyjob)
  New-Item -Path .\ -Name "$name.cmd" -ItemType File -Force 
  Get-Item -Path .\$name.cmd | Add-Content -Value "chcp $codepage"
  Get-Item -Path .\$name.cmd | Add-Content -Value "$robocopy"
  Get-Item -Path .\$name.cmd | Add-Content -Value "pause"
}

The combination of excel and powershell is really powerful. If you like it please push “helpful” or write a short comment. Thank you!

Print Friendly, PDF & Email
  • Was this Helpful ?
  • yes   no
0 0 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Batcher007
Batcher007
1 year ago

Hello,
I have a problem with this code.
The problem is when I start the script it will copy the folder and rename it and add the current date and time to the name. But while the script is running and changes in the folder were made it would overwrites the old folder with the old date without the /E /XC /XN /XO.

So how can I let the script always update the date and time in the name of the new folder so it won’t get overwritten with the old folder?

Code:
@echo off
color 2

Set Source=C:\source
Set Destination=C:\destination
Set Folder=FolderName
Set currentdate=%date:/=-%_%time::=-%

robocopy “%Source%” “%Destination%\%currentdate% %Folder%” /E /XC /XN /XO /mon:1 /mot:1

trackback
2 years ago

[…] See also my article about generating Robocopy jobs with PowerShell. […]