Windows batch if string compare




















If you do a lot of work in Windows batch files, the IF statement offers a very powerful way to add flexibility to your scripts. In this article you're going to learn about the five main types of IF statements you can use in a Windows batch file, how the correct syntax looks, and a realistic example for each.

One of the basic things you'll usually need to do in a batch script is compare two values and follow a different course of action depending on the comparison. For example, let's say you wanted to write a batch script that checks your computer's hard drive size daily. To create a script that compares the current free hard drive space to your limit, you'd create the following batch script and save it as a.

This is how the "wmic" command in this script calls the "logicaldisk" space and places it into the FreeSpace variable. Now you can just replace the line "Echo Not enough free space" with a blat email command to send you an alert.

If you've never used blat before, we have an article that shows you how to set up blat. Unfamiliar with setting up scheduled jobs? We've got you covered with an article on how to set up Windows scheduled tasks. In the following example you'll see how to check your Windows version using a batch job. Then you can compare this to your expected Windows version. Some uses of this script would be for IT audits when you need to quickly run a script and make sure the current operating system is the latest, or whether it needs an upgrade.

The ability to compare strings in batch opens up a whole list of possibilities. If you explore all of the information you can obtain out of a WMIC command you'll see just how many statistics about your computer you can monitor. You can use scheduled batch jobs to alert on these.

Another useful situation where an IF statement in a batch file is to check for the existence of a data file. A lot of times the batch job is just a monitoring tool that might be scheduled to check for new incoming data files in a specific directory. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions.

Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. Solution: Enable delayed expansion and use! Community Bot 1 1 1 silver badge. Kiquenet Bernardo Ravazzoni Bernardo Ravazzoni 4 4 bronze badges. This is wrong. You can use spaces, as long as you quote them. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back



0コメント

  • 1000 / 1000