Batch File to Loop Through Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys now we are going to learn our new topic we are in the ninth topic batch file to Loop through files okay how to write a script by Script for look through the files so let's read the requirement the user Story number nine rows needs to see all the files present inside any given folder so that means we have to design a prompt where the user will enter any folder let's say any folder path the moment he enters any folder path it should go inside that and start showing how many different files are there inside okay for a security check can you build a patch script simple requirement but again very tricky so let's see how this can be achieved so let's go uh to our notepad plus plus and quickly let's create a file so we are now in the ninth right so I will say 0 9 underscore Loop through files okay and Dot b80 this is a batch file hit on Save done now what we are going to do next are the rate we go off and then the title we will give what are the title to Loop through files okay and then we have to ask the user what is which is the file path right so what I will say I will use set and I will prompt the user by writing slash P prompt and I will take the user fold or the folder path okay folder underscore path equals to I will say enter the folder path so this this variables you can Define us as you want okay this is up to you this is your user defined variables so enter the folder path I will ask give a little space so that it appears appropriate done after that once the folder path is entered I would like to Loop through that specific path getting it I have to Loop through that specific path now what you do um you know first thing let's change the directory because let's say the I'm running the batch file from here what would happen when you run the loop it is going to Loop through this for example let me show you okay so let me make this to invalid you know right how to make it invalid just type to colons okay so this is invalid now let me first show you how a remark you know it become a remark it will not run so first of all let me show you something okay so if I say 4 the the code to look through the files is pretty simple four then I will say percentage percentage any variable you want I will say I I for iteration so I'm saying hi you can say a b c d whatever you like doesn't matter then I will say in okay then the current directory the presentation of wherever the current the script is running the current directory right so for that all you have to do star very simple one I'll teach you look at this tar dot star and close the bracket so that means current directory representation of the current directory so it will Loop through the current directory then what to do then you say do Echo and I would like to print that value that goes inside this variable so to Loop through each file name it will pick the I will pick and I would like to print that so print means Echo and I will say percentage percentage I okay this is done this is the simple code and I will pause it I will show you the output of this then we will actually design as per the roads requirement first let's understand this okay save and uh what I'm going to do is run this so what is happening open URL do you see all the back files open so it is what exactly did because our batch file is there in this current directory whatever files are there in this directory it showed as the output okay Loop profile so everything it is showing these are the files present in inside this directory so we understood what is the use of this so what we have to do we have to change the directory as per the user provided some path right so that's why we are asking provide the folder path where you like to search so once he provides that folder path we will change directory and once you change directory after this if we write this command whichever current directory it is it is going to fetch the files pretty simple okay so what I'm going to do I remove this and what I will do I will use the next command called change directory in the change directory what I will do the folder path will have that path for example C drive document so and so folder so that so right now because the basket is running from here this is pointing to the specific folder if I do change directly go to that folder right okay that specific partial point so let me do that so I'll say enter the folder path and here I will say folder underscore the value of the folder underscore path so I will write percentage do you see how we are creating different logics I will say path and percentage so what would happen by this it will go to that folder where exactly it has to Loop and this you know this will go to wherever it is it is going to see what are the files are pretty simple code now let's save it and quickly run it and see um so let's point out okay which folder we will give let's say this folder now this folder has got all these files right so I'll copy this path okay I'll copy this specific path I copied sample PDF files and these are the files it should show us the output so what I'm doing I'll run this okay it is asking enter the folder path what did I do what did I do okay let me close it let me run it again okay now let me enter the path okay so I've entered the path now let me hit on enter so did it show you all the files one two three four five the same files do we have here one two three four five getting it so this is the final code and this one meets the requirement of rows so as you provide different different folders for example let's test once one more um I don't know where the files are okay this will be inside C drive what is it documents rightly let me give this this one okay C drive uses Rakesh user documents so this has got directories directly so let's see how it is appearing so I'll go to the batch script run it and I'm going to provide this path hit on enter so what did it show you automate with Rakesh change format.txt decision.txt sample PDF zip test py dot jpptx so these are the different files okay we are not pointing looking at the folders but different files because you want to YT short PPT is there a yeah test dot zip okay so this folders are not appearing this is these folders I have got uh only the files are appearing getting it so this is your final code so thank you guys let's move on to our next topic before that type completed okay type completed once you complete any topic thank you guys
Info
Channel: Automate with Rakesh
Views: 10,456
Rating: undefined out of 5
Keywords: batch file, batch scripting, batch file command, batch file tutorial, batch file loop through files
Id: rbl9dL3LpEQ
Channel Id: undefined
Length: 8min 23sec (503 seconds)
Published: Wed Nov 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.