How to Backup Files Using For Loop in Batch File

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys how you guys doing in this video we are going to learn how to take a simple backup backing up files from one folder to another folder using for Loop so what is the syntax we'll be using for the for loop everything in detail bit by bit I am going to explain in this video so let's move on to our screen and let's get started all right so the very first thing is to see the Syntax for the for Loop look at the syntax and take a note looking at the syntax does it appear slightly complex not to worry I'm going to explain everything in a simple way now to because our example is around backup what we will do we will create some two folders first of all so let's create the first folder where I will name it as uh test okay in this one we will keep the source files okay we'll create some two files and keep it inside that and let's create another folder and name it as backup okay so in this one we will going to copy the source files to the destination file all right now let's go go inside the test folder inside the test folder what we are going to do we are going to create two text documents I am going to name the first one as test one hit on enter now let's create another text document and I'm going to name it as test2 okay and these are the two files we have to move it to the backup folder which is currently empty let's go on to the notepad plus plus and start writing the script you can also use a simple notepad so I'm saying Echo off and then here I'll be using the backup so first let's say something Echo and I am going to say backing up the files dot dot so that it will show in the command prompt and then we need to go inside the directory where the folder the source folder exists so where is this Source folder go to the source folder this is the source folder right test where did that go yeah test so this is the folder I am going to copy the entire path of this folder go back and write here so CD that means change the directory because your basket might be running from your desktop or anywhere right so and the location of the folder could be different so you have to Pro get inside the directory first okay using CD so once you go inside the directory then I will use a simple for Loop okay now in the for Loop let's look at the syntax the syntax said for percentage percentage variable so what if this person this percentage variable let's first write that I'm going to Simply say percentage percentage any variable you can write a b c d but ensure that variable only has a single letter it should not be multiple data so I can write a or I can write B whatever you like so let's go with a okay SK so the first part is done right and then I have to write in and then what did this set and do I am going to show you so first then I have to write in okay so I'll go and write in and then it is saying write the set what does it mean by set set means what are the things you would like to copy so what you can do you can put open parenthesis and you can mention the file name let's say I'm mentioning test1.txt and then I'm mentioning test2.txt this is one way I'm going to show you so many other ways okay that means you are mentioning after getting inside the directory you choose these two files and then do a loop loop through Loop means it needs more files right more than one so that it can Loop through one by one okay and then what are the syntax then it is saying do set means you understood then we are writing do I have to write do and then the next thing I will do is write the command whatever command you want to print the names you want to copy to some other things so here our objective is to take a backup so what which command we will use we'll use the copy command copy copy it not move it not cut it okay copy so the files will all will be there in the a source folder it will just copy that to Backup backup means that copying right okay so copy and here I have to write you know what to copy copy command needs what to copy where to copy so what to copy this one will have the first file when it Loops right and second time when it Loops it will have the second file so it so I'll mention copy percentage percentage a looping means first it will have this one second it will have this file so I am saying whatever value is there in the percentage percentage a you copy that to where where would you like to copy so to where I will go to the backup folder okay copy the entire path and I'm going to mention it here after the space give a space invention so this is your entire command getting it after do whatever you will write that is the entire command so whatever you like to do you have written it so this is done now I'm going to hit on enter and I'm going to say Echo backup completed okay and then I am going to say pause getting it so a pretty simple code let's save it and let's run this script okay so if I run the script you can see it is saying backing up backing up the files one file copied one file copied backup completed pressing key to continue okay so that means the backup is completed let's see that if I go to the backup folder you can see test1 and test two got copied now Additionally you you will have more questions you will have questions like Rakesh if there are 10 files do I need to specify this a little bit too much of trouble right if I go and continue to write all the names and high chances of making mistake while writing the uh names right so what I will do I'll simply say star dot star means what so every file will have a name test one dot txt so dot so that the name could be anything it's a wild character it could be anything and the extension could be anything or if you have to only copy text file you mentioned dot txt if you if there are multiple different types of files you write a star pretty simple okay so here let's say txt so if I do this even this is going to work for you so let me delete these two files okay and let's run the script now if I run the script it says one five copied so it is done let's move on to our backup folder okay now you can see the files have come so there are multiple ways to do it you understood the usage of the wild character pretty useful character when there are multiple different types of files and you don't know what could be the name this could be an easier way so thank you guys for watching this is a basic usage of for loop I am going to come up with more videos around the for Loop thank you guys let's move on to our next next video thanks for watching and please do subscribe bye
Info
Channel: Automate with Rakesh
Views: 3,657
Rating: undefined out of 5
Keywords: batch file, batch scripting, batch file command, batch file tutorial, backup batch, cmd backup, bath file backup, back up batch, batch file backup, batch file backup script, batch file backup folder daily, batch for loop, cmd for loop, batch for loop syntax, batch for loop syntax explanation, create batch file, batch
Id: hXpv8ghORdc
Channel Id: undefined
Length: 7min 29sec (449 seconds)
Published: Tue Dec 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.