powershell - 创建目录结构(如果Powershell中不存在)

标签 powershell

我试图创建一个目录结构,如果它不在另一个位置。它可以工作,但是在名称中带有方括号的任何目录上都会出现错误。我想我必须以某种方式逃避那,但不知道如何。

脚本代码:

$source = "c:\data"
$destination = "c:\scrap\data"

Get-ChildItem -Path $source -Recurse -Force |
  Where-Object { $_.psIsContainer } |
  ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |
  ForEach-Object {
    if (!(Test-Path -path $_ )) { $null = New-Item -ItemType Container -Path $_ }
  }

最佳答案

得到它了。该错误是由于方括号引起的。它们用于模式匹配(请参阅here),因此Test-Path实际上检查

C:\scrap\data\Evernote\backup\Untitled note 2_files



不存在。您必须使用-LiteralPath避免这种情况。

关于powershell - 创建目录结构(如果Powershell中不存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12448513/

相关文章:

Powershell 和 .txt 格式

powershell - 为什么我的脚本 block 在执行时被视为字符串,而不是作为脚本执行?

azure - Powershell 和 Azure 资源资源管理器显示不同的属性集

powershell - 通过 .cmd 文件安装 "azure-powershell.0.8.7.msi"时出现错误

Powershell:文件元数据中的 NTFS 路径与 New-ItemProperty、Set-ItemProperty?

python - Pywinrm 和 Active Directory PowerShell cmdlet

powershell - 如何使用Powershell根据员工ID修改Active Directory用户属性?

powershell - 控制 Powershell 控制台的窗口位置

powershell - Powershell脚本来过滤Windows事件日志

Powershell 替换 csv 中的一个实例