PowerShell:根据参数创建一定数量的文件,使用 运行 数字来区分文件
PowerShell: Creating a set amount of files based on a parameter, with a running number to differentiate the files
所以我有一个任务,我必须创建一个带有三个参数的 PowerShell 脚本,“$foldername”、“$filename”和“$number”。
脚本检查文件夹“$foldername”是否存在,如果不存在,则创建它。之后,它会创建与“$number”指定的一样多的名为“$filename”的新文件。之后它会报告已创建了多少文件并列出它们。
到目前为止我有什么。
Param (
[string]$foldername,
[string]$filename,
$number=1
)
if ((Test-Path -Path $foldername) -ne $true) {
new-item -path $foldername -ItemType directory #if the folder doesn't exist, create it.
}
$new_file= $foldername+"$_"+$filename #save the path and name of the new file to an variable
if ((Test-Path -Path $new_file* -PathType leaf) -eq $true) {
Write-Host "$filename already exists in $foldername"
break #if a file with a name that contains $filename in it exists in $foldername, break and do not create any new files.
}
$null=1..$number | foreach { new-item -path $foldername -name $_$filename } #create new files using foreach.
write-host ("Created $number new files") #tell the user how many files were created
Get-ChildItem -path $foldername | where-object Name -like *$filename* | format-table Name #show the created files in a table format, formatted by name
此脚本中存在一些问题和损坏的解决方案,但主要问题是新文件的创建。由于新文件的名称来自 $filename,因此只需 运行 脚本如下:
./script.ps1 -foldername C:\users\example\testing -filename "test.txt" -number 5
不会工作,因为它会尝试创建 5 个名为“test.txt”的文件,并且只会 return 错误。
我通过使用“foreach”并将创建的文件命名为 $_$filename 来解决它
1test.txt
2test.txt
...
5test.txt
但我发现正确的方法是:
test1.txt
test2.txt
...
test5.txt
文件名中的数字应该是 运行,但我不知道该怎么做。
如果您知道如何检查 $filename 文件是否已存在于目标文件夹中,可加分。
使用 Test-Path
很好,但我认为这里没有必要,您可以使用 $ErrorAction = 'Stop'
这样如果文件夹存在,脚本将立即停止并显示一条警告消息。另一方面,如果文件夹是新文件夹,则文件不可能已经存在。
Param (
[parameter(Mandatory)]
[string]$FolderName,
[parameter(Mandatory)]
[string]$FileName,
[int]$Number = 1
)
$ErrorActionPreference = 'Stop'
try {
$newFolder = New-Item -Path $FolderName -ItemType Directory
}
catch {
# If the folder exists, show this exception and stop here
Write-Warning $_.Exception.Message
break
}
$files = 1..$Number | ForEach-Object {
# If this is a new Folder, there is no way the files already exist :)
$path = Join-Path $newFolder.FullName -ChildPath "$FileName $_.txt"
New-Item -Path $path -ItemType File
}
Write-Host 'Script finished successfully.'
$newFolder, $files | Format-Table -AutoSize
编辑:我可能错过了要在文件夹中创建文件的地方,即使该文件夹已经存在,在这种情况下,您可以使用以下内容:
Param (
[parameter(Mandatory)]
[string]$FolderName,
[parameter(Mandatory)]
[string]$FileName,
[int]$Number = 1
)
$ErrorActionPreference = 'Stop'
$folder = try {
# If the Folder exists get it
Get-Item $FolderName
}
catch {
# If it does not, create it
New-Item -Path $FolderName -ItemType Directory
}
$files = 1..$Number | ForEach-Object {
$path = Join-Path $folder.FullName -ChildPath "$FileName $_.txt"
try {
# Try to create the new file
New-Item -Path $path -ItemType File
}
catch {
# If the file exists, display the Exception and continue
Write-Warning $_.Exception.Message
}
}
Write-Host "Script finished successfully."
Write-Host "Files created: $($files.Count) out of $Number"
$files | Format-Table -AutoSize
所以我有一个任务,我必须创建一个带有三个参数的 PowerShell 脚本,“$foldername”、“$filename”和“$number”。
脚本检查文件夹“$foldername”是否存在,如果不存在,则创建它。之后,它会创建与“$number”指定的一样多的名为“$filename”的新文件。之后它会报告已创建了多少文件并列出它们。
到目前为止我有什么。
Param (
[string]$foldername,
[string]$filename,
$number=1
)
if ((Test-Path -Path $foldername) -ne $true) {
new-item -path $foldername -ItemType directory #if the folder doesn't exist, create it.
}
$new_file= $foldername+"$_"+$filename #save the path and name of the new file to an variable
if ((Test-Path -Path $new_file* -PathType leaf) -eq $true) {
Write-Host "$filename already exists in $foldername"
break #if a file with a name that contains $filename in it exists in $foldername, break and do not create any new files.
}
$null=1..$number | foreach { new-item -path $foldername -name $_$filename } #create new files using foreach.
write-host ("Created $number new files") #tell the user how many files were created
Get-ChildItem -path $foldername | where-object Name -like *$filename* | format-table Name #show the created files in a table format, formatted by name
此脚本中存在一些问题和损坏的解决方案,但主要问题是新文件的创建。由于新文件的名称来自 $filename,因此只需 运行 脚本如下:
./script.ps1 -foldername C:\users\example\testing -filename "test.txt" -number 5
不会工作,因为它会尝试创建 5 个名为“test.txt”的文件,并且只会 return 错误。
我通过使用“foreach”并将创建的文件命名为 $_$filename 来解决它
1test.txt
2test.txt
...
5test.txt
但我发现正确的方法是:
test1.txt
test2.txt
...
test5.txt
文件名中的数字应该是 运行,但我不知道该怎么做。
如果您知道如何检查 $filename 文件是否已存在于目标文件夹中,可加分。
使用 Test-Path
很好,但我认为这里没有必要,您可以使用 $ErrorAction = 'Stop'
这样如果文件夹存在,脚本将立即停止并显示一条警告消息。另一方面,如果文件夹是新文件夹,则文件不可能已经存在。
Param (
[parameter(Mandatory)]
[string]$FolderName,
[parameter(Mandatory)]
[string]$FileName,
[int]$Number = 1
)
$ErrorActionPreference = 'Stop'
try {
$newFolder = New-Item -Path $FolderName -ItemType Directory
}
catch {
# If the folder exists, show this exception and stop here
Write-Warning $_.Exception.Message
break
}
$files = 1..$Number | ForEach-Object {
# If this is a new Folder, there is no way the files already exist :)
$path = Join-Path $newFolder.FullName -ChildPath "$FileName $_.txt"
New-Item -Path $path -ItemType File
}
Write-Host 'Script finished successfully.'
$newFolder, $files | Format-Table -AutoSize
编辑:我可能错过了要在文件夹中创建文件的地方,即使该文件夹已经存在,在这种情况下,您可以使用以下内容:
Param (
[parameter(Mandatory)]
[string]$FolderName,
[parameter(Mandatory)]
[string]$FileName,
[int]$Number = 1
)
$ErrorActionPreference = 'Stop'
$folder = try {
# If the Folder exists get it
Get-Item $FolderName
}
catch {
# If it does not, create it
New-Item -Path $FolderName -ItemType Directory
}
$files = 1..$Number | ForEach-Object {
$path = Join-Path $folder.FullName -ChildPath "$FileName $_.txt"
try {
# Try to create the new file
New-Item -Path $path -ItemType File
}
catch {
# If the file exists, display the Exception and continue
Write-Warning $_.Exception.Message
}
}
Write-Host "Script finished successfully."
Write-Host "Files created: $($files.Count) out of $Number"
$files | Format-Table -AutoSize