Powershell脚本未使用mkdir函数创建文件夹

时间:2019-03-15 11:05:14

标签: powershell

我希望使用Powershell脚本创建每日文件夹,而创建前2个文件夹没有问题,但列表中的第三个文件夹却没有创建。下面是我的代码,由于某些原因未创建原始数据文件夹-关于为什么会发生这种情况的任何建议?

$months = Get-Date -UFormat %b
$monthl = Get-Date -UFormat %B
$year = Get-Date -UFormat %Y
$timestamp = Get-Date -UFormat "%d%m%Y"
$folderstamp = Get-Date -UFormat "%d-%m-%Y"

mkdir "X:\Client Services & Fulfilment\Fulfilment\CMS\$year\$monthl $year\Investec_AML\$folderstamp"
mkdir "X:\Client Services & Fulfilment\Fulfilment\CMS\$year\$monthl $year\Investec_AML\$folderstamp\Final Output"
mkdir "X:\Client Services & Fulfilment\Fulfilment\CMS\$year\$monthl $year\Investec_AML\$folderstamp\Raw Data"

如果我在Powershell本身上写出该行代码,它将返回LastWriteTime日期为01/01/1601吗?请参阅下面的屏幕快照链接。该模式似乎显示了所有可能的模式?

powershell screenshot

2 个答案:

答案 0 :(得分:0)

从您的屏幕截图中,我可以看到Raw Data文件夹确实存在。在Mode下,您可以看到其属性:

d - Directory
a - Archive
r - Read-only
h - Hidden
s - System
l - Reparse point, symlink, etc.

也许您应该对该文件夹(或链接)进行更多调查,以查明为什么存在该文件夹(如果是符号链接),以及它指向的位置。

无论如何,这是采用PowerShell风格的代码:

$now         = Get-Date
$months      = $now.ToString("MMM")
$monthl      = $now.ToString("MMMM")
$year        = $now.Year
$timestamp   = $now.ToString("ddMMyyyy")
$folderstamp = $now.ToString("dd-MM-yyyy")

$folderName = "X:\Client Services & Fulfilment\Fulfilment\CMS\$year\$monthl $year\Investec_AML\$folderstamp"
try {
    New-Item -ItemType Directory -Path $folderName -ErrorAction Stop | Out-Null
    New-Item -ItemType Directory -Path (Join-Path -Path $folderName -ChildPath 'Final Output') -ErrorAction Stop | Out-Null
    New-Item -ItemType Directory -Path (Join-Path -Path $folderName -ChildPath 'Raw Data') -ErrorAction Stop | Out-Null
}
catch {
    Write-Error $_.Exception.Message
}

希望有帮助

答案 1 :(得分:0)

无论出于什么原因(也许是Raw单词?),用下划线替换新目录名称中的空格即有效,即“ Raw_Data”而不是“ Raw Data”,因此我将其推广到其他日常工作中使用类似文件夹结构的过程。

感谢Theo提供的整理代码!

$now         = Get-Date
$months      = $now.ToString("MMM")
$monthl      = $now.ToString("MMMM")
$year        = $now.Year
$timestamp   = $now.ToString("ddMMyyyy")
$folderstamp = $now.ToString("dd-MM-yyyy")

$folderName = "X:\Client Services & Fulfilment\Fulfilment\CMS\$year\$monthl $year\Investec_AML\$folderstamp"
try {
New-Item -ItemType Directory -Path $folderName -ErrorAction Stop | Out-Null
New-Item -ItemType Directory -Path (Join-Path -Path $folderName -ChildPath 'Final_Output') -ErrorAction Stop | Out-Null
New-Item -ItemType Directory -Path (Join-Path -Path $folderName -ChildPath 'Raw_Data') -ErrorAction Stop | Out-Null
}
catch {
Write-Error $_.Exception.Message
}
相关问题