使用批处理创建文件夹,但前提是该文件夹尚不存在

发布于 2024-10-02 06:57:59 字数 212 浏览 1 评论 0原文

谁能告诉我如何在 Windows 批处理脚本中执行以下操作? (*.bat):

  • 仅当文件夹尚不存在时才创建该文件夹

更详细地说,我想在 C: 上创建一个名为 VTS 的文件夹\ 驱动器,但前提是该文件夹尚不存在。如果文件夹已经存在并且批处理已执行,我不想覆盖该文件夹的内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

囍笑 2024-10-09 06:58:00

您只需使用以下命令:if not exit "C:\VTS\" mkdir C:\VTS 仅当该文件夹不存在时,它才会创建一个目录。

请注意,仅当 VTS 存在并且是目录时,此存在测试才会返回 true。如果它不存在,或者作为文件存在,则 mkdir 命令将运行,并且应该会导致错误。您可能还想检查 VTS 是否也作为文件存在。

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.

苏大泽ㄣ 2024-10-09 06:58:00
if exist C:\VTS\NUL echo "Folder already exists"

if not exist C:\VTS\NUL echo "Folder does not exist"

另请参阅https://support.microsoft.com/en-us/kb/65994

(2018 年 3 月 7 日更新;Microsoft 文章已下架,存档于 https://web.archive.org/web/20150609092521/https://support.microsoft.com/en-us/kb/65994 )

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 )

七月上 2024-10-09 06:58:00

无论如何,只需调用 mkdir C:\VTS 即可。它只会报告子目录已经存在。

编辑:正如其他人所指出的,如果文件夹已存在,这确实会设置%ERRORLEVEL%。如果您的批处理(或调用它的任何进程)不关心错误级别,则此方法效果很好。由于问题没有提到避免错误级别,所以这个答案是完全有效的。它满足创建文件夹(如果文件夹不存在)的需要,并且不会覆盖现有文件夹的内容。否则请遵循Martin Schapendonk 的回答

Just call mkdir C:\VTS no matter what. It will simply report that the subdirectory already exists.

Edit: As others have noted, this does set the %ERRORLEVEL% if the folder already exists. If your batch (or any processes calling it) doesn't care about the error level, this method works nicely. Since the question made no mention of avoiding the error level, this answer is perfectly valid. It fulfills the needs of creating the folder if it doesn't exist, and it doesn't overwrite the contents of an existing folder. Otherwise follow Martin Schapendonk's answer.

埋葬我深情 2024-10-09 06:58:00
mkdir C:\VTS 2> NUL

创建一个名为 VTS 的文件夹,并将 A subdirectory or file TEST hasexists 输出到 NUL

或者

(C:&(mkdir "C:\VTS" 2> NUL))&

将盘符更改为C:mkdir,将错误输出为NUL并运行下一个命令。

mkdir C:\VTS 2> NUL

create a folder called VTS and output A subdirectory or file TEST already exists to NUL.

or

(C:&(mkdir "C:\VTS" 2> NUL))&

change the drive letter to C:, mkdir, output error to NUL and run the next command.

翻了热茶 2024-10-09 06:58:00
set myDIR=LOG
IF not exist %myDIR% (mkdir %myDIR%)
set myDIR=LOG
IF not exist %myDIR% (mkdir %myDIR%)
你与昨日 2024-10-09 06:58:00

我使用这种方式,你应该在目录名末尾加一个反斜杠以避免该位置存在于没有扩展名且与你指定的目录同名的文件中,切勿使用” C:\VTS" 因为"C:"分区中可能存在名为"VTS"的文件,正确的方法是使用< strong>“C:\VTS\”,检查一下VTS后面的反斜杠,这样才是正确的方法。

@echo off
@break off
@title Create folder with batch but only if it doesn't already exist - D3F4ULT
@color 0a
@cls

setlocal EnableDelayedExpansion

if not exist "C:\VTS\" (
  mkdir "C:\VTS\"
  if "!errorlevel!" EQU "0" (
    echo Folder created successfully
  ) else (
    echo Error while creating folder
  )
) else (
  echo Folder already exists
)

pause
exit

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.

@echo off
@break off
@title Create folder with batch but only if it doesn't already exist - D3F4ULT
@color 0a
@cls

setlocal EnableDelayedExpansion

if not exist "C:\VTS\" (
  mkdir "C:\VTS\"
  if "!errorlevel!" EQU "0" (
    echo Folder created successfully
  ) else (
    echo Error while creating folder
  )
) else (
  echo Folder already exists
)

pause
exit
塔塔猫 2024-10-09 06:58:00

您可以使用:

if not exist "C:\VTS\" mkdir "C:\VTS"

您还可以扩展代码来替换任何丢失的预期文件。

if not exist "C:\VTS\important.file" echo. > "C:\VTS\important.file"

You can use:

if not exist "C:\VTS\" mkdir "C:\VTS"

You can also expand the code to replace any missing expected files.

if not exist "C:\VTS\important.file" echo. > "C:\VTS\important.file"
标点 2024-10-09 06:58:00

如果文件夹不存在,您需要创建一个文件夹吗?好吧,这里有一个如何做到这一点的示例。

首先,我通过输入以下代码来检查该文件夹是否尚不存在:

if not exist "FOLDERPATH" ( 
    mkdir "FOLDERPATH"
)

So if I run the code.如果该文件夹已经存在,它将什么也不做。如果文件夹已经存在,这就是我们要做的事情:

if exist "FOLDERPATH" ( 
    rmdir /s /q "FOLDERPATH"
    mkdir "FOLDERPATH"
)

现在,如果我运行代码,它将重新创建该文件夹(如果它已经存在)。这是示例代码:

@echo off
cls

if not exist "C:\ExamplePath\" ( 
    echo Creating Folder...
    mkdir "C:\ExamplePath\"
    pause
)

if exist "C:\ExamplePath\" ( 
    echo Re-Creating Folder...
    rmdir /s /q "C:\ExamplePath"
    pause
)

现在 if exit 部分是可选的。如果该文件夹已经存在,您可以跳转到一个标签,如下所示:

if exist "FOLDERPATH" ( 
    goto :ExampleLabel
    
    :ExampleLabel
    echo Hi.
    pause
)

希望这可以帮助解决您的问题。

You need to create a folder if it doesn't exist eh? Well, here is an example of how to do it.

First, I check to see if the folder doesn't already exist by entering this code:

if not exist "FOLDERPATH" ( 
    mkdir "FOLDERPATH"
)

So if I run the code. And if the folder already exists, It will do nothing. This is what we do if the folder already exists:

if exist "FOLDERPATH" ( 
    rmdir /s /q "FOLDERPATH"
    mkdir "FOLDERPATH"
)

Now if I run the code, It will re-create the folder if it already exists. This is the example code:

@echo off
cls

if not exist "C:\ExamplePath\" ( 
    echo Creating Folder...
    mkdir "C:\ExamplePath\"
    pause
)

if exist "C:\ExamplePath\" ( 
    echo Re-Creating Folder...
    rmdir /s /q "C:\ExamplePath"
    pause
)

Now the if exist part is Optional. If the folder already exists, you can jump to an label instead like this:

if exist "FOLDERPATH" ( 
    goto :ExampleLabel
    
    :ExampleLabel
    echo Hi.
    pause
)

Hopefully, this could help with your problem.

ぶ宁プ宁ぶ 2024-10-09 06:58:00

这应该对您有用:

IF NOT EXIST "\path\to\your\folder" md \path\to\your\folder

但是,还有另一种方法,但它可能不是 100% 有用:

md \path\to\your\folder >NUL 2>NUL

此方法创建文件夹,但如果文件夹存在,则不会显示错误输出。我强烈建议您使用第一个。第二个是如果你和另一个有问题。

This should work for you:

IF NOT EXIST "\path\to\your\folder" md \path\to\your\folder

However, there is another method, but it may not be 100% useful:

md \path\to\your\folder >NUL 2>NUL

This one creates the folder, but does not show the error output if folder exists. I highly recommend that you use the first one. The second one is if you have problems with the other.

埋葬我深情 2024-10-09 06:58:00

我正在从 Dockerfile 在 docker 上下文中创建一个文件夹,以避免如果 mkdir 命令上已存在文件夹,则构建错误停止,并且这里的任何答案都不适合我,但有些启发我试试这个,它有效(RUN 是 Dockerfile 命令,默认情况下在 Windows docker 上,在 cmd 上下文中运行):

RUN mkdir C:\VTS & echo.

I'm creating a folder in the context of docker from a Dockerfile to avoid build stopping in error if folder already exists on a mkdir command, and none of answer here worked for me, but some inspired me to try this, that works (the RUN is Dockerfile command that, by default on Windows docker, runs in context of cmd):

RUN mkdir C:\VTS & echo.
つ低調成傷 2024-10-09 06:58:00

就我个人而言,我会这样做:

if not exist "C:\YourFolderPathHere\" (
mkdir C:\YourFolderPathHere\
) else (
echo Folder already exists!
)

让我们分解一下:

  1. 如果不存在“C:\YourFolderPathHere\”:这会检查文件夹。路径后面的反斜杠(\)非常重要,否则它将查找文件而不是文件夹。
  2. mkdir C:\YourFolderPathHere\:如果上述语句成立,则创建目录。
  3. echo 文件夹已存在!:打印到控制台它已经存在。

这是相同的代码,但带有注释:

::Does the folder exist?
if not exist "C:\YourFolderPathHere\" (
::No, make it.
mkdir C:\YourFolderPathHere\
) else (
::Yes, don't make it, and print text.
echo Folder already exists!
)

一行版本:
如果不存在 "C:\YourFolderPathHere\" mkdir C:\YourFolderPathHere\

不过还没有测试过,所以不要引用我的话。

Personally, I would do this:

if not exist "C:\YourFolderPathHere\" (
mkdir C:\YourFolderPathHere\
) else (
echo Folder already exists!
)

Let's break that down:

  1. if not exist "C:\YourFolderPathHere\": this checks for the folder. The Backslash (\) after the path is very important, else it will look for a file rather than a folder.
  2. mkdir C:\YourFolderPathHere\: Creates the directory if the above statement is true.
  3. echo Folder already exists!: prints to console that it already exists.

Here's the same code, but with comments:

::Does the folder exist?
if not exist "C:\YourFolderPathHere\" (
::No, make it.
mkdir C:\YourFolderPathHere\
) else (
::Yes, don't make it, and print text.
echo Folder already exists!
)

One-line version:
if not exist "C:\YourFolderPathHere\" mkdir C:\YourFolderPathHere\

Haven't tested it though, so don't quote me on it.

枕头说它不想醒 2024-10-09 06:58:00

我为我在 Eyebeam 工作中使用的脚本创建了这个。

:CREATES A CHECK VARIABLE

set lookup=0

:CHECKS IF THE FOLDER ALREADY EXIST"

IF EXIST "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\" (set lookup=1)

:IF CHECK is still 0 which means does not exist. It creates the folder

IF %lookup%==0 START "" mkdir "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\"

i created this for my script I use in my work for eyebeam.

:CREATES A CHECK VARIABLE

set lookup=0

:CHECKS IF THE FOLDER ALREADY EXIST"

IF EXIST "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\" (set lookup=1)

:IF CHECK is still 0 which means does not exist. It creates the folder

IF %lookup%==0 START "" mkdir "%UserProfile%\AppData\Local\CounterPath\RegNow Enhanced\default_user\"
心意如水 2024-10-09 06:58:00

您可以使用以下批处理脚本来执行此操作
此外,您还可以根据创建或不创建文件夹来执行更多命令。

if not exist "Folder_Path" ( 
GOTO CREATE_FOLDER_LABEL
      ) else ( 
GOTO DONT_CREATE_FOLDER_LABEL 
)

:CREATE_FOLDER_LABEL
rem all the commands which you want to execute due to creating folder goes here
MKDIR "FOLDER_PATH"
exit

:DONT_CREATE_FOLDER_LABEL
rem all the commands which you want to execute due to not creating folder goes here
ECHO "the FOLDER_PATH is already exist"
exit

You can do this using the following batch script
Also, you can execute more commands due to creating or not creating the folder.

if not exist "Folder_Path" ( 
GOTO CREATE_FOLDER_LABEL
      ) else ( 
GOTO DONT_CREATE_FOLDER_LABEL 
)

:CREATE_FOLDER_LABEL
rem all the commands which you want to execute due to creating folder goes here
MKDIR "FOLDER_PATH"
exit

:DONT_CREATE_FOLDER_LABEL
rem all the commands which you want to execute due to not creating folder goes here
ECHO "the FOLDER_PATH is already exist"
exit
如何视而不见 2024-10-09 06:58:00

您可以将 /I 选项添加到 mkdir 命令中。 /I 选项告诉 mkdir 仅在目录尚不存在时创建该目录。如果该目录已存在,则 mkdir 命令将不执行任何操作并继续执行下一个命令。

mkdir /I dist

You can add the /I option to the mkdir command. The /I option tells mkdir to only create the directory if it doesn't already exist. If the directory already exists, the mkdir command will do nothing and continue with the next command.

mkdir /I dist
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文