이 PowerShell 스크립트는 숫자 순차적 이름을 가진 디렉터리를 만듭니다.
function New-SequentialDirectory
{
Param
(
[string] $Path = (Get-Location -PSProvider FileSystem).Path,
[UInt32] $StartAt = 1,
[UInt32] $EndAt
)
$Path = (Get-Location -PSProvider FileSystem).Path
if (-not (Test-Path -PathType Container $Path))
{
$exception = New-Object System.IO.DirectoryNotFoundException "The specified directory does not exist: $Path"
throw $exception
}
Write-Debug "Path: $Path"
foreach($number in $StartAt..$EndAt)
{
$itemPath = Join-Path -Path $Path -ChildPath $number
New-Item -Path $itemPath -ItemType Directory
}
<#
.SYNOPSIS
Creates a series of directories with numerically sequential names.
.PARAMETER Path
The path at which the sequence should be created. This must be an existing directory.
.PARAMETER StartAt
The number at which the sequence should start. This value must be positive and may be greater than the value of the EndAt parameter.
.PARAMETER EndAt
The number at which the sequence should end. This value must be positive and may be less than the value of the StartAt parameter.
.EXAMPLE
New-SequentialDirectory -StartAt 1 -EndAt 10
Directory: C:\Users\TestUser\Desktop\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 5/10/2014 2:18 p.m. 1
d---- 5/10/2014 2:18 p.m. 2
d---- 5/10/2014 2:18 p.m. 3
d---- 5/10/2014 2:18 p.m. 4
d---- 5/10/2014 2:18 p.m. 5
d---- 5/10/2014 2:18 p.m. 6
d---- 5/10/2014 2:18 p.m. 7
d---- 5/10/2014 2:18 p.m. 8
d---- 5/10/2014 2:18 p.m. 9
d---- 5/10/2014 2:18 p.m. 10
This example shows the creation of a sequence of ten folders in the current directory.
.EXAMPLE
New-SequentialDirectory -StartAt 10 -EndAt 1
Directory: C:\Users\TestUser\Desktop\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 5/10/2014 2:57 p.m. 10
d---- 5/10/2014 2:57 p.m. 9
d---- 5/10/2014 2:57 p.m. 8
d---- 5/10/2014 2:57 p.m. 7
d---- 5/10/2014 2:57 p.m. 6
d---- 5/10/2014 2:57 p.m. 5
d---- 5/10/2014 2:57 p.m. 4
d---- 5/10/2014 2:57 p.m. 3
d---- 5/10/2014 2:57 p.m. 2
d---- 5/10/2014 2:57 p.m. 1
This example shows that the EndAt parameter can be less than the StartAt parameter.
#>
}
아래 코드를 사용하려면 다음을 수행해야합니다.
- 위의 코드를 이름이 지정된 파일로 복사하십시오.
New-SequentialDirectory.ps1
- 이 명령으로 세션에 함수를 추가하십시오. 1
. <path-to-New-Sequential-Directory.ps1>
- 함수를 호출하십시오. 이 작업을 수행하는 방법에 대한 몇 가지 예가 다음 명령을 통해 사용할 수있는 명령에 대한 도움말에 나와 있습니다.
Get-Help New-SequentialDirectory -Examples 명령을 사용하십시오.
1 기본적으로 PowerShell은 파일에서 스크립트를 실행할 수 없으므로 2 단계가 실패 할 수 있습니다. 이 문제를 해결하려면 실행 정책을 약간 덜 제한적인 것으로 변경해야합니다. 이에 대한 더 자세한 정보는 Get-Help about_Execution_Policies