我正在寻找使用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 ?!请参见下面的屏幕截图该模式似乎显示所有可用的模式?
从截图中,我可以看到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
}
希望有所帮助
无论出于什么原因(可能是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
}