作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个脚本,该脚本在给定路径(第一个参数)中生成每个月的文件夹(格式 yyyy_mm
),并在每个文件夹中生成每天的子文件夹(格式 yyyy_mm_dd
)。
该代码有效,但有没有更简单的解决方案?
param(
[string]$inppath = '',
[string]$inpyear = '0'
)
function dayfolder
{
1..[DateTime]::DaysInMonth($inpyear,$month) | ForEach-Object {
$day = $_
New-Item -ItemType directory -Force -Path ($inppath + '\' + $inpyear + '_' + ("{0:D2}" -f $month) + '\' + $inpyear + '_' + ("{0:D2}" -f $month) + '_' + ("{0:D2}" -f $day) ) }
}
if ($inppath -eq '')
{
echo 'No path in input! First parameter!'
}
else
{
if ($inpyear -eq '0')
{
echo 'No year in input! Second parameter! Format: YYYY'
}
else
{
1..12 | ForEach-Object {
$month = $_
New-Item -ItemType directory -Force -Path ($inppath + '\' + $inpyear + '_' + ("{0:D2}" -f $month))
dayfolder
}
}
}
最佳答案
我认为您通过为参数提供默认值然后在使用默认值时给出错误消息来使事情变得过于复杂。如果您希望参数是强制性的,您应该这样声明它们。事实上,您可以更进一步,将它们声明为有效路径和特定范围内的数字。
否则我的主要观察是 New-Item 创建它需要的任何父项,因此您不需要单独创建月份文件夹。
有多种方法可以构建路径字符串,但我认为在这种情况下,最简单的方法是格式化月份和日期,然后只使用单个字符串(请注意,_
是变量名中的有效字符在某些情况下使用 ${...}
形式的变量扩展):
param(
# Path to an existing folder.
[Parameter(Mandatory=$True)]
[ValidateScript({Test-Path $_ -PathType 'Container'})]
[string]$inppath,
# Year must be fairly recent or in the future. Warranty expires 2100
[Parameter(Mandatory=$True)]
[ValidateRange(1999,2100)]
[int]$inpyear
)
1..12 | ForEach-Object {
$month = "{0:D2}" -f $_
1..[DateTime]::DaysInMonth($inpyear,$month) | ForEach-Object {
$day = "{0:D2}" -f $_
New-Item -ItemType directory -Force -Path "$inppath\${inpyear}_$month\${inpyear}_${month}_${day}"
}
}
关于powershell - 生成一整年的月文件夹和日子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27500025/
我正在寻找一种在Shamsi日历中找到假期的方法,我尝试了很多方法。 但最后得出的结论是我必须手动输入信息 我尝试的另一种方法是使用矩库(波斯语和贾拉利版 - ساعت ایران) 这是我的代码:
我是一名优秀的程序员,十分优秀!