Can anybody tell me how to do the following in in a Windows batch script? (*.bat
):
In more detail, I want to create a folder named VTS
on the C:\
drive, but only if that folder doesn't already exist. I don't want to overwrite the contents of the folder if it already exists and the batch is executed.
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
To create a Windows batch file, follow these steps: Open a text file, such as a Notepad or WordPad document. Add your commands, starting with @echo [off], followed by, each in a new line, title [title of your batch script], echo [first line], and pause. Save your file with the file extension BAT, for example, test.
The md and mkdir commands are functionally identical. You can also create new folders in Windows Explorer by going to File → New → Folder. You may indicate an absolute or relative path for the path parameter. When absolute, the new directory is created as specified from the root directory.
You just use this: if not exist "C:\VTS\" mkdir C:\VTS
it wll create a directory only if the folder does not exist.
Note that this existence test will return true only if VTS exists and is a directory. If it is not there, or is there as a file, the mkdir command will run, and should cause an error. You might want to check for whether VTS exists as a file as well.
if exist C:\VTS\NUL echo "Folder already exists" if not exist C:\VTS\NUL echo "Folder does not exist"
See also https://support.microsoft.com/en-us/kb/65994
(Update March 7, 2018; Microsoft article is down, archive on https://web.archive.org/web/20150609092521/https://support.microsoft.com/en-us/kb/65994 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With