我正在寻找一种批量创建文件夹和子文件夹的方法。
我正在看类似的东西
c:\users\user\desktop\sample\1
c:\users\user\desktop\sample\2
ETC。
有任何想法吗?
我更喜欢使用 .cmd/PowerShell 方法来做到这一点。
如果可以通过终端进行操作,我很乐意通过 USB 启动 Linux 实时光盘来尝试任何想法/解决方案。
答案1
您可以像这样使用命令 md:
for /l %a in (1,1,2) do md "c:\users\user\desktop\sample\folder %a"
这将创建如您的示例中的两个文件夹。
答案2
该 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 不允许您从文件运行脚本。要解决此问题,您需要将执行策略更改为稍微不那么严格的策略。有关此内容的更多信息,请参阅Get-Help about_Execution_Policies