Can anybody tell me how to do the following in in a Windows batch script? (*.bat
):
- Create a folder only if it doesn't already exist
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.
i created this for my script I use in my work for eyebeam.
I use this way, you should put a backslash at the end of the directory name to avoid that place exists in a file without extension with the same name as the directory you specified, never use "C:\VTS" because it can a file exists with the name "VTS" saved in "C:" partition, the correct way is to use "C:\VTS\", check out the backslash after the VTS, so is the right way.
Try this
ROBOCOPY C:\Users\ADMIN\Pictures c:%date:~4,2%%date:~7,2%%date:~10,4% /MOV
This batch will make a new folder and set its name as the current date. (03222013) It then will move any files in the
C:\Users\ADMIN\Pictures
folder to that newly created folder.If today is March 22 2013.... All files in
C:\Users\ADMIN\Pictures
will be moved to a folder named03222013
.Only files in
C:\Users\ADMIN\Pictures
will be moved. Any folder inC:\Users\ADMIN\Pictures
will not be touched.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.
create a folder called
VTS
and outputA subdirectory or file TEST already exists
toNUL
.or
change the drive letter to
C:
,mkdir
, output error toNUL
and run the next command.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 )