Batch File If Time Variable

  1. Batch File If Exist
  2. Using Variables In Batch Files
  3. Batch File If Condition

Batch File Variables Any programming or scripting languages require variable to store the data and access them whenever we need the value. Using of variables. Create a date and time stamp in your batch files. By Steve Wiseman on February 16. The problem with that is the utility needs to be around if you want to send the batch file to someone. This is the only place I’ve been able to find that so clearly explains the use of the date and time variable in renaming files in a script (I’ve.

Active3 months ago

Batch file if else statement. So, as the syntax signifies, first a condition is checked and if true, the corresponding statements are executed in the batch file if statement. As for batch file if else, first a condition of if statement is checked and if true, the statement1 is executed else statement2 is executed.

I'm trying to define and use a variable in a batch file. It looks like it should be simple:

The output I get is the following:

What's going on here? Why is my variable not being echo'd?

aschipfl
21.5k9 gold badges36 silver badges62 bronze badges
Jamie DixonJamie Dixon
39.2k14 gold badges111 silver badges149 bronze badges

3 Answers

The space before the = is interpreted as part of the name, and the space after it (as well as the quotation marks) are interpreted as part of the value. So the variable you’ve created can be referenced with %location %. If that’s not what you want, remove the extra space(s) in the definition.

Brian NixonBrian Nixon
7,3141 gold badge14 silver badges23 bronze badges

The spaces are significant. You created a variable named (enclosing single quotes added to show location of space) 'location 'with a value of ' 'bob'.

If you want quotes in your value, then your code should look like

If you don't want quotes, then your code should look like

Or better yet

The last syntax prevents inadvertent trailing spaces from getting in the value, and also protects against special characters like & | etc.

dbenhamdbenham
106k20 gold badges190 silver badges293 bronze badges

input location.bat

output

(mistakes u done : space and ' ')

PhilipPhilip

protected by CommunityJan 16 '14 at 13:50

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged batch-filecmdenvironment-variables or ask your own question.

Active2 years, 7 months ago

I have a question how to set multiple files as variables in batch file? I trying to do something with below script:

Above works fine only with one file. If there are more than two it only echos one of them. How can I echo in one line all the files that where copied?

geminatores
geminatoresgeminatores

2 Answers

There are multiple problems with your code

First, you're assigning new values to the variable file each loop and then overwriting it every time without doing anything. Therefore after the loop file will only contain the value in the last loop. Move the echo part to inside the loop instead

Another problem is that variables are expanded at parse time by default. You need to enable delayed expansion and use ! instead of % to make it expand at runtime

If you want echo all files at once then set the file variable to contain the list of files

The check for file exist is redundant. You can check for the exit code after moving it and exit if the files weren't moved.

phuclvphuclv
17.7k9 gold badges61 silver badges258 bronze badges

The variables are not constant so remember to activate the expansion delayed variable, here the expansion of the variable file must be delayed.

Delayed Expansion will cause variables to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL command. When delayed expansion is in effect variables can be referenced using !variable_name! (in addition to the normal %variable_name% )

Delayed variable expansion is often useful when working with FOR Loops, normally an entire FOR loop is evaluated as a single command even if it spans multiple lines of a batch script.This is the default behaviour of a FOR loop:

Batch File If Exist

Example :

Notice that when the FOR loop finishes we get the correct total, so the variable correctly increments, but during each iteration of the loopthe variable is stuck at it's initial value of 0

The same script with EnableDelayedExpansion, gives the same final result but also displays the intermediate values:

Notice that within the for loop we use !variable! instead of %variable%.

File

And your batch script can be written like that :

HackooHackoo

Using Variables In Batch Files

11.1k3 gold badges20 silver badges47 bronze badges

Batch File If Condition

Not the answer you're looking for? Browse other questions tagged batch-file or ask your own question.