If in windows batch




















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.

Add a comment. Active Oldest Votes. Addendum - This is a duplicate question with nearly identical answers to Using an OR in an IF statement WinXP Batch Script Final addendum - I almost forgot my favorite technique to test if a variable is any one of a list of case insensitive values.

If doing the test within a block where delayed expansion is needed to access current value of VAR then for Killroy - yes, I had already noted that limitation in the answer. But your comment forced me to look at the answer and realize there are additional limitations with the FOR solution. I've updated the end of the answer a bit. Wouldn't an echo to findstr be an option? The answer is wrong. I don't think so. Yeah as far as I can tell those two are the only option.

But just needed to do a small bug fix to a batch script and wanted to know if this was possible. I'm picky when it comes to clean code lol — Anthony Miller. Correct English would read "Didn't find it". I was going to edit the question, but did find that someone has already done it, then did found that it got rolled back.

Show 2 more comments. Magoo Apostolos Apostolos 2, 19 19 silver badges 23 23 bronze badges. I found this to be the most straight forward and simple to use case in my batch scripts. How come this never made it as the recommended answer? I don't know, upvoting in general is a very strange process and dubious? But in this case, I guess it's timing. The question is quite old and my answer vere recent.

Anyway, we must be happy when we can find any answer that works. One possible drawback with this technique although I like the out-of-the-boxness of the approach! Theoretically, yes. But I have created hundreds of batch files, I use dozens of them everyday and I never met such a case. So let's stay in practice! I have such a case today where I don't want to execute the condition twice.

I want to do something if either file a or file b exists. Show 1 more comment. Thanks for this post, it helped me a lot. Gwenc37 2, 7 7 gold badges 16 16 silver badges 22 22 bronze badges. Cactus Cactus 91 1 1 silver badge 1 1 bronze badge. Placing an IF command on the right hand side of a pipe is also possible but the CMD shell is buggy in this area and can swallow one of the delimiter characters causing unexpected results.

A simple example that does work:. This is so that the IF statement will treat the string as a single item and not as several separate strings. This is an important difference because if you compare numbers as strings it can lead to unexpected results: "2" will be greater than "19" and "" will be less than "10".

Many people start using batch jobs for simple tasks that need to be executed in sequence. With IF statements it's possible to add a lot more intelligence to your scripts. Of course if you really want to step it up a notch, you might consider taking a look at VBA with our guide on creating your first VBA application , or maybe even learn Python programming.

You can often use more advanced programming languages like these, or learn to use PowerShell , to accomplish many of the same tasks you currently use batch jobs for. The command prompt is still a powerful Windows tool. Here are the most useful CMD commands every Windows user needs to know. Ryan has a BSc degree in Electrical Engineering.

He's worked 13 years in automation engineering, 5 years in IT, and now is an Apps Engineer. If you're ready to start scripting, let's get started! Compare Values 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. Finally, set this script up as a windows scheduled batch job that runs daily.

Share Share Tweet Email. Related Topics Windows Programming Windows.



0コメント

  • 1000 / 1000