Tech

Getting to know Robocopy a bit, a command line tool for Windows

Yes ok windows is seen by the vast majority of the public as an operating system that is used through the graphical interface, that does not mean that it does not have pre-installed or at its disposal interesting tools that work through the command line, that interface whose use is usually associated with Linux.

What’s more, even your own Microsoft, without making much noise, has been taking care of the use of the command line in Windows and it even seems to have given it more prominence in recent times, something that from certain points of view can be understood as a kind of surrender to the obvious advantages it provides compared to graphical interfaces, although the latter are also better in certain aspects.

Microsoft has been developing and promoting for many years PowerShell. This command line interpreter was associated with Windows for a long time, but the company decided in 2016 to make it open source (although reserving some letters) and take it to Linux and macOS, so since then it has competed with other solutions in the same field such as bash and zsh. Compared to the command prompt, one of its advantages is that it allows you to create your own commands and scripts using the C# programming language.

Robocopy Help

Robocopy Help.

The care that the Redmond giant dedicates to the command line was more recently palpable with Windows Terminalan open source terminal application that has been set by default in Windows 11. It is capable of handling both Command Prompt (what some colloquially call an MS-DOS emulator) and PowerShell and can also run separate tabs for WSL, the Windows subsystem for Linux that allows you to run a full Linux operating system on top of Microsoft’s.

As we can see, the command line is far from being a corpse in Windows and what’s more, given that Microsoft has been giving it a greater role in recent times, although all these movements are mainly aimed at programmers, system administrators and profiles of advanced users. Although it is not so friendly for the common user, it never hurts to have certain notions, and it is that although the graphical interfaces are initially more comfortable, for many tasks they are slower than the command line once this is mastered. last.

On previous occasions we have mentioned the most useful commands for Windows and we extended ourselves with SDelete, a tool that allows you to delete files in a way that makes it difficult to recover them. This time we will expand a bit with robocopyan interesting tool to mainly copy directories and their contents.

What is Robocopy?

Robocopy is accessible from the command line, both from PowerShell and the command prompt, and is a robust file copy reference, robust file copy in English. Basically it’s about a tool to copy and move files from one place to another. In Windows 7 it was improved with the possibility of being able to work in multithreading (multithreading) to improve performance, which is a bit reminiscent of what Google Chrome/Chromium meant at the time against its rivals.

Before showing some of the features with examples, surely the name of the tool reminds many of RoboCop, the well-known cyborg hero and protagonist of the universe of the same name that takes place in a futuristic and dystopian Detroit. Is Robocopy a sarcastic reference to RoboCop? Whether or not it is true, the reality is that there is, or at least once existed, a graphical interface for Robocopy called RoboCop.

Using Robocopy

Using Robocopy to copy and move files

After exposing what Robocopy is, we are going to show how it works with some examples. Exposing everything that this tool can do in detail can go a long way, so we will focus on some of the most well-known features on a folder to help the user to get into its use.

We start with the most basic use of the tool, without introducing any parameters or arguments. To complete the source without having to type everything, the user can autocomplete by pressing the tab key. However, after entering the source, there is a backslash in front of the closing quote, so it looks like this if there is a space in the directory or file name: '.\Xonotic OST (OGG)\'.

If the user doesn’t remove the last backslash, they are escaping the closing quotes, so instead of the end of the string (the name of the file or folder with one or more spaces) they are telling the shell to understand them literally, that is, as if it were one more letter in the name of the file or folder. Because of that, you have to move the cursor to place it just in front of the second backslash and press the backspace key so that the origin is like this: '.\Xonotic OST (OGG)'. Another possibility is to leave only the quotes with the name of the file or the folder between them: 'Xonotic OST (OGG)'.

In addition to establishing an origin, you must also set a destination, which is the name of the folder that will be created in the copy process (Sintonic in this example). If you want to put spaces, you have to put the name of the destination inside some quotation marks, which we have not used in our case, so the command is as follows:

robocopy '.\Xonotic OST (OGG)' Sintonic

If the Robocopy process did not throw any errors, the user should be able to see where it is (C:\Users\Guillermo Puertas\Desktop'Guillermo Puertas is the name of the user) a folder called Sintonic with the contents of the source (Xonotic OST (OGG)), which is a directory that contains the soundtrack of the video game Xonotic.

Robocopy supports multithreaded copying, which is very useful to speed up the copy or move process in cases where there are one or several folders with a large amount of content. The feature does not work by default, so you have to put the parameter /mt at the end to activate it:

robocopy '.\Xonotic OST (OGG)' Sintonic2 /mt

Another interesting possibility is the resettable modewhich can be activated with the parameter /z. With restartable mode, if a file copy is interrupted, Robocopy can pick up where it left off instead of copying the entire file again.

robocopy '.\Xonotic OST (OGG)' Sintonic3 /z

As is normal in command interpreters, it is possible to use several parameters at the same time. Here’s an example with multithreaded and restartable mode at the same time:

robocopy '.\Xonotic OST (OGG)' Sintonic4 /mt /z

parameter /mov allows you to move the files contents by the origin, but not the subdirectories and the contents of these, towards the destination and without deleting the source directory:

robocopy .\Sintonic7\ Atomic1 /mov

parameter /move allows you to move the files contents by the origin, but not the subdirectories and the contents of these, towards the destination and deleting the source if it does not contain subdirectories:

robocopy Atomic1 OMEGA /move

And now we expose one of Robocopy’s most useful parameters, if not the most: /mir. look refers to mirror in English, which comes to mean mirrorand what it does is mirror the entire tree or subtree of directories and files on the destination.

robocopy '.\Xonotic OST (OGG)' Espejo /mir

If one looks for the recursive copy with all contents, it is supplied by the parameter /s:

robocopy '.\Xonotic OST (OGG)' Sintonic5 /s

The following arguments that we are going to mention require additional privileges to work, so it is recommended to start PowerShell or the command prompt with administrator privileges, which you do by right-clicking on the entry in the Start menu and then clicking on “Run as administrator”. It is important to note that this will cause the terminal session to C:\Windows\system32 instead of the user’s folder, which is C:\Users\nombreusuario.

Start PowerShell as administrator on Windows

Here we are going to mention three examples, starting with a backup mode (/b) that allows Robocopy to override file and folder permission settings (ACLs). This allows the copying of files that you would not otherwise have access to.

robocopy '.\Xonotic OST (OGG)' Sintonic5 /b

parameter /zb starts the copy process in restartable mode. In case access to the file is denied, the backup mode is started:

robocopy '.\Xonotic OST (OGG)' Sintonic6 /zb

As we have already said, these are just a few examples. All the parameters and possibilities of Robocopy are exposed in the official documentation of Microsoft.

Looking for alternatives to Robocopy to copy and move files within the same subdirectory

Robocopy is a tool focused above all on mirroring or migrating files from one path to another, so its use for files that are located within the same directory becomes cumbersome, and that’s if it’s achieved. To make it easier to copy and move files within the same directory, we’ll use a couple of alternatives to Robocopy.

For copy a file can be used copy-item:

copy-item heavymetal.ogg copia-heavymetal.ogg

While for move is available move-item:

copy-item copia-heavymetal.ogg heavymetal-movido.ogg

Conclusion

Robocopy is an interesting tool for making backup copies of the most important folders for the user. We’ve stuck to basic usage here, but the destination doesn’t have to be on the same unit or on the same team, so it can hold more than meets the eye.

Cover image: Pixabay

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *