如果目录不存在则创建 [英] Create directory if it does not exist

查看:66
本文介绍了如果目录不存在则创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 PowerShell 脚本来创建多个不存在的目录.

I am writing a PowerShell script to create several directories if they do not exist.

文件系统看起来像这样

D:\
D:\TopDirec\SubDirec\Project1\Revision1\Reports\
D:\TopDirec\SubDirec\Project2\Revision1\
D:\TopDirec\SubDirec\Project3\Revision1\

  • 每个项目文件夹都有多个修订版本.
  • 每个修订文件夹都需要一个 Reports 文件夹.
  • 一些修订"文件夹已经包含一个报告文件夹;然而,大多数没有.
  • 我需要编写一个每天运行的脚本来为每个目录创建这些文件夹.

    I need to write a script that runs daily to create these folders for each directory.

    我可以编写脚本来创建一个文件夹,但是创建多个文件夹是有问题的.

    I am able to write the script to create a folder, but creating several folders is problematic.

    推荐答案

    试试 -Force 参数:

    New-Item -ItemType Directory -Force -Path C:\Path\That\May\Or\May\Not\Exist
    

    您可以使用 Test-Path -PathType Container 先检查一下.

    You can use Test-Path -PathType Container to check first.

    请参阅新项目 MSDN 帮助文章了解更多详情.

    See the New-Item MSDN help article for more details.

    这篇关于如果目录不存在则创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆