0

I am new to batch scripting and wonder if anyone would help me understand the differences below:

Differences between these 3 lines:

Set "File=Test.txt"
Set File="Test.txt"
Set File=Test.txt

Differences between these 3 lines:

Set /A "Start=3"
Set Start=3
Call Set /A "Start=3"

Difference between these 2 lines:

SETLOCAL EnableDelayedExpansion
@SETLOCAL EnableDelayedExpansion

What does this line do?

break > %outfile%

Thank you for your help.

I have tried writing basic scripts and trying the minor differences but didn't see much change. Just wonder why the different way of writing it.

  • 1
    Please read [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) It explains the differences in full details with an example for demonstrating the differences. – Mofi Mar 28 '23 at 06:04
  • 1
    `@` at beginning of a command line instructs `cmd` not printing the command line after parsing before execution. That is only necessary if the first line in the batch file is not `@echo off` which turns off the command echo mode with `@` used to prevent getting output this command line itself before execution. Please open a [command prompt](https://www.howtogeek.com/235101/), run `cmd /?` and read the output help. It is also possible to start `cmd.exe` with option `/Q` to turn echo off, but that is not the default on starting `cmd.exe` for batch file processing with a batch file double click. – Mofi Mar 28 '23 at 06:11
  • 1
    `break > %outfile%` creates an empty file with a file size of 0 bytes overwriting an existing file, except the existing file has the read-only attribute set. I have never seen a batch file where this command is really necessary. The first output of a string can be always redirected with redirection operator `>` into a file creating the file new even on already existing and all other strings are appended to the once created file with redirection operator `>>`. There is no need to first create the output file with a file size of 0 bytes on having a line which always writes the first line. – Mofi Mar 28 '23 at 06:17
  • I suggest further reading the __issue__ chapters in [this answer](https://stackoverflow.com/a/60686543/3074564) and [debugging a batch file](https://stackoverflow.com/a/42448601/3074564) and [this answer](https://stackoverflow.com/a/67039507/3074564) for details about the commands __SETLOCAL__ and __ENDLOCAL__ as well as [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](https://stackoverflow.com/questions/4094699/) – Mofi Mar 29 '23 at 16:41
  • Thank you everyone for your replies. I've really learnt a lot from just this one post. You guys are a great resource. Thanks again for sharing your knowledge and being a great teacher. – user19568486 Mar 29 '23 at 20:12

1 Answers1

1

Set "File=Test.txt" - safest way when the value contains special characters

Set File="Test.txt" - the value will include the quotes

Set File=Test.txt - in this case it will be the same as the first approach

2)

Set /A "Start=3" - the switch indicates that there will be arithmetic e expression. Though here it is just "3" so it will be the same with and without "/A".

Set Start=3 - just simple assigning start to 3

Call Set /A "Start=3" - used when it is set inside brackets section and the value contains another variable. Usually is better to use delayed expansion. Here you can see more detailed explanation

3) the @ at the beginning of the line will not print the command itself ,but only the output (if there's any). Same as echo off but applied only for one line. There should not be a space after the @

4) break > %outfile% - clears the content of the %outfile%

npocmaka
  • 55,367
  • 18
  • 148
  • 187