Advanced File Copying With Batch Files!

by 20charlie02 in Circuits > Tools

5221 Views, 15 Favorites, 0 Comments

Advanced File Copying With Batch Files!

startup.png

WARNING: I CREATED THIS FOR EDUCATIONAL PURPOSES ONLY. I AM NOT RESPONSIBLE FOR ANYTHING THAT HAPPENS WHILE YOU ARE USING THIS.

Hey everyone! today I will be showing you how to make a file copying batch file (mine is on a 32 GB flash drive). so basically, what I intended was to make a file that would back up my computer quickly and easily just by clicking on the file. The following commands will be used:

- Color
-Title
-Xcopy
-Shutdown
-Choice
-etc.

Choose the Folders and or Files You Want to Back Up.

step 1.png

In the red rectangles are the folders I chose to back up. Here is the first bit of code you will want to type into your notepad.

@echo off
:: variables
SET odrive=%odrive:~0,2%
set backupcmd=xcopy /s /c /d /e /h /i /r /y
pause
%backupcmd% "%USERPROFILE%\pictures" "%drive%\User Files\Photos"

What this is doing is telling the computer to copy the folders you selected and everything inside them, and save it to your flash drive. I recommend a flash drive with at least 8 GB for this project, depending on how many files you have. This is the most complicated step. the line of code below is telling the computer what you want to copy. you can change it to your preference.

%backupcmd% "%USERPROFILE%\pictures" "%drive%\User Files\Photos"

this code is going to save my pictures onto my flash drive in a folder called /User_files/Photos which can be changed very easily like this

%backupcmd% "%USERPROFILE%\Videos" "%drive%\User Files\Videos"

See how I changed pictures to videos? It's that simple. Shown in the picture above, highlighted in red, are all the folders I chose to back up. here is the full strip of code.

%backupcmd% "%USERPROFILE%\pictures" "%drive%\User Files\Photos
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\User Files\Favorites"
%backupcmd% "%USERPROFILE%\documents" "%drive%\User Files\Documents"
%backupcmd% "%USERPROFILE%\videos" "%drive%\User Files\Videos"
%backupcmd% "%USERPROFILE%\downloads" "%drive%\User Files\Downloads"
%backupcmd% "%USERPROFILE%\desktop" "%drive%\User Files\Desktop"
%backupcmd% "%USERPROFILE%\onedrive" "%drive%\User Files\OneDrive"

This is copying my Photos, Favorites, Documents, Videos, Downloads, Desktop, and OneDrive files. DO NOT FORGET TO SAVE AS A .BAT FILE. THIS IS ESSENTIAL.

My Finishing Tweaks.

titles.png

Well congratulations! If you have made it this far you have done great! All of the stuff you have just done was just the mandatory things. Now I will be showing you how I made into an actual program that you interact with!

Lets start with the Title command.
At the start of every folder being copied, I title the command prompt to tell me what is being copied. it will look something like this:

title Copying photos...
%backupcmd% "%USERPROFILE%\pictures" "%drive%\User Files\Photos"
title Copying Favorites...
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\User Files\Favorites"
title Copying Documents...
%backupcmd% "%USERPROFILE%\documents" "%drive%\User Files\Documents"
title Copying Videos...
%backupcmd% "%USERPROFILE%\videos" "%drive%\User Files\Videos"
title Copying Downloads...
%backupcmd% "%USERPROFILE%\downloads" "%drive%\User Files\Downloads"
Title Copying Desktop...
%backupcmd% "%USERPROFILE%\desktop" "%drive%\User Files\Desktop"
title Copying OneDrive...
%backupcmd% "%USERPROFILE%\onedrive" "%drive%\User Files\OneDrive"

Do you see all the Title copying photos, copying videos, copying downloads, etc. These are optional, but it makes you look more professional.

More Finishing Tweaks.

completed.png

Remember how I said you can interact with it? Well here it is. What I made is a question in the batch file to ask me at the end "would you like to shutdown[Y/N]? If you type Y, hit enter it will shut down in ten seconds. If you type N, and hit enter, the file will close and all your files will be backed up! pretty cool, huh. You can also type COLOR A to give the font a cool looking green. So here is the "shutdown choice" command.

:choice
set /P c=Files have been copied. Would you like to shutdown[Y/N]?
if /I "%c%" EQU "Y" goto :Yes
if /I "%c%" EQU "N" goto :No
goto :choice
:yes
echo Shuting down...
pause
Shutdown -s -t 10 -c "Data transfer complete. Windows will shutdown in 10 seconds.
:no
exit

What this is saying is that if you choose yes it will shut down you computer, but if you type no, it will exit.

Your Finished Product.

Great job! If you have made it this far you are an expert batch programmer! I have included a video I shot of my script working and everything. This is my first instructable, so please comment and tell me what I can improve on and if you liked it! Thanks for reading.