我正在写一个PowerShell脚本来创建几个目录,如果它们不存在的话。

文件系统与此类似

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

每个项目文件夹都有多个版本。 每个修订文件夹都需要一个Reports文件夹。 一些“修订”文件夹已经包含了一个报告文件夹;然而,大多数人并没有。

我需要编写一个脚本,每天运行为每个目录创建这些文件夹。

我能够编写脚本创建一个文件夹,但创建几个文件夹是有问题的。


当前回答

下面的代码片段帮助您创建一个完整的路径。

Function GenerateFolder($path) {
    $global:foldPath = $null
    foreach($foldername in $path.split("\")) {
        $global:foldPath += ($foldername+"\")
        if (!(Test-Path $global:foldPath)){
            New-Item -ItemType Directory -Path $global:foldPath
            # Write-Host "$global:foldPath Folder Created Successfully"
        }
    }
}

上面的函数分割了传递给函数的路径,并将检查每个文件夹是否存在。如果它不存在,它将创建相应的文件夹,直到目标/最终文件夹创建。

要调用函数,使用下面的语句:

GenerateFolder "H:\Desktop\Nithesh\SrcFolder"

其他回答

我也有同样的问题。你可以这样使用:

$local = Get-Location;
$final_local = "C:\Processing";

if(!$local.Equals("C:\"))
{
    cd "C:\";
    if((Test-Path $final_local) -eq 0)
    {
        mkdir $final_local;
        cd $final_local;
        liga;
    }

    ## If path already exists
    ## DB Connect
    elseif ((Test-Path $final_local) -eq 1)
    {
        cd $final_local;
        echo $final_local;
        liga;  (function created by you TODO something)
    }
}

Use:

$path = "C:\temp\"
    
If (!(test-path $path))
{
    md $path
}

第一行创建了一个名为$path的变量,并将字符串值"C:\temp"赋给它 第二行是If语句,它依赖于Test-Path cmdlet来检查变量$path是否不存在。不存在的是合格的使用!的象征。 第三行:如果没有找到上面字符串中存储的路径,将运行花括号之间的代码。

md是输入out的简称:New-Item -ItemType Directory -Path $path

注意:我没有使用下面的-Force参数进行测试,以查看如果路径已经存在,是否存在不期望的行为。

New-Item -ItemType Directory -Path $path

我希望能够方便地让用户为PowerShell创建一个默认配置文件来覆盖一些设置,并以以下一行程序结束(多个语句是可以的,但可以粘贴到PowerShell并立即执行,这是主要目标):

cls; [string]$filePath = $profile; [string]$fileContents = '<our standard settings>'; if(!(Test-Path $filePath)){md -Force ([System.IO.Path]::GetDirectoryName($filePath)) | Out-Null; $fileContents | sc $filePath; Write-Host 'File created!'; } else { Write-Warning 'File already exists!' };

为了可读性,以下是我在.ps1文件中如何做的:

cls; # Clear console to better notice the results
[string]$filePath = $profile; # Declared as string, to allow the use of texts without plings and still not fail.
[string]$fileContents = '<our standard settings>'; # Statements can now be written on individual lines, instead of semicolon separated.
if(!(Test-Path $filePath)) {
  New-Item -Force ([System.IO.Path]::GetDirectoryName($filePath)) | Out-Null; # Ignore output of creating directory
  $fileContents | Set-Content $filePath; # Creates a new file with the input
  Write-Host 'File created!';
}
else {
  Write-Warning "File already exists! To remove the file, run the command: Remove-Item $filePath";
};
$path = "C:\temp\NewFolder"
If(!(test-path -PathType container $path))
{
      New-Item -ItemType Directory -Path $path
}

Test-Path -PathType容器检查路径是否存在,是否为目录。如果没有,它将创建一个新目录。如果路径存在但是一个文件,New-Item将引发一个错误(如果您有风险,可以使用-force参数覆盖该文件)。

当指定-Force标志时,如果文件夹已经存在,PowerShell将不会报错。

一行程序:

Get-ChildItem D:\TopDirec\SubDirec\Project* | `
  %{ Get-ChildItem $_.FullName -Filter Revision* } | `
  %{ New-Item -ItemType Directory -Force -Path (Join-Path $_.FullName "Reports") }

顺便说一下,要调度任务,请查看这个链接:调度后台任务。