 Welcome back, everyone. Today, we're talking about batch scripting in Windows to monitor the disks that are inserted. So I had a request to monitor disks that are inserted into a computer to say which physical drive they've been assigned. And I thought the easiest way to do that is to make a quick batch file. And it turned out to be a little bit more difficult than I thought. So I would like to talk about it today. So this is my batch script. And if you've never made a batch script before, in Windows, you can just open up Notepad and then save, just go to file and save as, and make sure you save as all files and make the extension, whatever your script is called, dot B-A-T. So if you save that as a batch file, then you should get this kind of gear looking icon in Windows. Now, if you just click on this, it will run. And then you can also run it from the command line. We'll be running it from the command line today. Normally, I would be working in Linux and I would do bash scripts. And bash, I think is a lot more powerful. Maybe it's just because I don't understand the syntax for batch scripting very well, but I finally figured out well enough how to do it. So basically what we are interested in doing today is figuring out what physical disks are in the system. So I can figure out what physical disks are in the system by running the command, this command here, let me scroll this down, by running the command WMIC disk drive list brief. And then that will give me the caption, kind of this header line, this heading line. And then the caption, the name of the disk, the device ID, which is actually what we're really interested in, the physical drive zero. And then the model vbox hard disk partitions and size. I'm a little bit interested in size, but I'm mostly interested in which physical device has just been connected. So let me show you how I'm doing that here. So in our batch file, I started with echo off, otherwise all of these lines whenever they're run will be echoed. So we don't want to show everything, we only want to show the disk that has been inserted. Okay, so then I have this two dots and then update disks. And this is basically like a function name. It's a little bit different in that we can run it over and over again, but it's a label that we can go back to. So if you see down further in my code, we can use the go to command update disks. And then this will go back to update disks, and then it will start running each line from here. Now, the important thing to realize here is that update disks, if I call this, then it's also going to run WMIC disk drive brief to and save that to disks old. But then it's also going to run get disks, WMIC list brief and update disks new. Okay, so where have we start if I go back to update disks, it's going to run everything in sequence again. Okay, so then I have update disks, I have get disks, that's basically just listing the drives. Now I'm running the same command twice. But what I'm doing is saving those saving that output to different files. And the reason I'm saving these two different files is because I want to get disks to monitor any of the new disks that are inserted and then compare this files output to the old file. So basically I have in disks old.txt, I have the files or the disks that already exist in the system. And then I check to see if there's any new disks and put the results of checking into disks new.txt. Then I can compare these two text files and see if there's a difference. Okay. Now, so I have these two label names, update disks, get disks, that way we can go back to them whenever we need them. And then I have this loop label. And at the end I have go to loop. So this would keep looping over and over. But I already have a loop in here already. So that's a little bit redundant. But this is just, let's say I want to keep doing all of these commands over and over and over again, then you can make a loop label. And then before the end of your document, just go to loop. Okay. So next, this is probably one of the more important commands. I'm using the FC command in Windows, FC in Windows, and checking temp disks old and temp disks new. And then if error level is one, that means they are not the same file. They don't have the same content. So let me show you what this FC command does. Okay, I'll just have it. So C drive, C drive temp disks old.txt, C drive temp disks new.txt. Okay, if I run that. So it's comparing files, C drive disk old.txt and disks new.txt, there are no differences encountered. So let's go back to, let's say disks new. And actually this one has two. So I'm going to go back to disks old. Sorry. And I'm going to remove this USB stick. Okay. So now this file does not have the USB stick. Disk old does not have the USB stick, but disk new does. So this was kind of like representing what it would be like to insert a disk into the computer. Okay. So if I run FC again, it will say there is a difference and it lists the difference in a really weird way, but that's just what they do. So basically it's saying that all of these characters are what is different in the new file. Okay. So we can see that these characters are different. So these two files are different. Now, what we're actually doing here is checking the error level. So whenever the files are different, we get an error level, we can do echo and then use the variable error level. So percent error level percent is the variable for the error of the command that just ran. In Linux, we use this a lot. In Windows, it's echo percent error level percent. Okay. Enter. And then notice that it came back with a one. Okay. So if I can just tell you that if the files were the same, our error level would be zero. So FC would give an error level of zero. So in this case, we're saying if the error level of this FC command is one, then do everything inside these brackets. So basically do all of this. If it's not one, if it's zero, then just time out. And this just gives it, it just stops for 10 seconds, and then it goes back and gets the disks again. So this is part of the loop. Okay. Now, if the two files are different, then we, so if error level equals one, the two files are different, then we echo some just information for the user. Date and time, we detected a change. So whatever date and time it is now, I'm going to detect this change, or I did detect change. Then we're going to use the command find string or find str slash, and then these are the arguments given to the find string command. So slash be vg. And then the two files that were interested are temp disks old, and temp disks new. Okay, so find the strings. And basically what what these are saying is find everything that's different between these two. Find the strings that are different between the old file and the new file. Okay, so let's go ahead and run that. So remember that we have two different files right now. So find str slash be vg, C drive. Okay. So find string be vg, C drive, temp disks old, C drive temp disk new. Okay, enter. And notice that the string that we removed from disks old is the difference here. So the string that we removed, now these two files are not the same. So it lists the drive that has been removed in this case from disks old. So this is what it would look like if disks new, we update disks new, we get all of the drives and then let's say physical drive one was inserted into the computer. So now disks old has the old version without physical drive one, disks new has the new version with physical drive one. Okay, so then it would just output only the difference which would be all of the disk, all of the physical drive one information. Okay. So that's all that find string is doing. So there were two commands basically that I was trying to use Linux utilities in Windows to solve. And I figured out if you have two different commands, you can do it. Usually I would use in Linux just diff. And then diff would pretty much handle all of this use case. But in Windows I was using FC to compare whether they're the same, and then checking the error level of FC, and then using find string to actually find the differences, and output only the differences between the two files. Okay, and that will give us, if we have two files, one has the old disks that were in the system, one has the new disks or the changes, then we can get only the files or only the disks that have been inserted into the computer. And then, and then just display them. Okay. So once we print the new disk, if there is any change the new disk that has been changed, then we go to update disks and that goes back to this update disks, right. So I don't, yeah, okay, I could have put this somewhere else actually I probably should have put it after this time out but it's okay. So here we have WMIC disk drive list brief, and then I update the disks old to reflect the new changes that we've already detected. I update the disks new to reflect all of the new changes and then now I'm at a state that we know. So now if you insert another disk, then I can detect if that has changed or not, or I can detect if any new disks have changed. Okay. So if we skip this error level, then we are doing a time out slash T 10, no break. And what that does is basically just time out for 10 seconds or sleep for 10 seconds. And then I'm outputting that to null or nowhere, basically, after we sleep for 10 seconds, just to give the computer, we don't need to be checking every, you know, millisecond or whatever it is. Then I can sleep for a second and then go to get disks and start over again. So basically I'm looping between get disks here. Now, if for some reason that failed, then I would go to loop and then I would start from here again. But like I said, I can probably remove this and be fine. So this was a quick script that I wanted to write. It didn't really take me very long, but I had two problems. I normally use diff in Linux and the equivalent for that is basically two commands in Windows. First off, FC to check if there is a difference and then find string to actually show what those differences are. Everything else is a basic batch script commands. Now notice just like a bash script, most of these commands are just commands that you could normally type into the command line. So for example, WMIC disk drive list brief, in my batch script, I can also just type that out like I would in the command line. So it's fairly similar to batch scripting. I just thought I would give an example. So I hope that was helpful and I hope you can use that in some of your own scripts to monitor disk drive insertions or whatever it is. So let's go ahead and just run the script. So my script is called monitor, monitor disks dot bat. And I am running this command prompt as an administrator. So monitor disk dot bat and then it starts and it's not printing out anything. So in the background, if we check the disks old, we'll see one drive. If we check the disks new, we will see one drive. So disks old, disks new, we just have one drive. They're the same file. So it's not going to show us any new information. Now, if I go back and I plug in my USB stick, now it's saying, OK, change detected. Notice the date and time. We have the date, current date and current time. Change detected, USB 2.0 device, physical drive 1, 2.0, all the information. Now, if I remove that, then it will say the same thing. So it'll say the same thing. Why? Because we don't actually check whether it inserted the disk or took the disk out. Now, we could add a little bit more logic because we know, for example, if it's in one file, but not in another file, then, for example, if it is in old, oh, sorry, if it's in new, if it's in disks new, but not in disks old, then you've inserted the disk. If it is in disks old, but not in disks new, then you've removed the disk. So we could add some logic in there to try to figure out if you've inserted or removed, and I probably should actually do that because we don't really care about the removal. We care more about the inserting and what physical drive is assigned. Actually, now that I see it, it's showing physical drive 0, so I need to fix that as well. So this is basically just the leftover, I guess, from disks old. So I do need to fix that logic. So there's a couple things. I just thought I'd talk about batch scripts can be quite useful. I've never tried to do this much logic in a batch script before, so I just thought I'd show you guys how to do it. It's not very, very difficult, as long as you know the commands to get exactly what you're looking for in the command line, then you can basically make your batch script. Use labels to go around and jump around your code, and other than that, that's pretty much it. So thank you very much.