PowerShell脚本:生成500个文件夹和500个txt文本,并按序号把文本放在对应的文件夹中

===============第一步生成文件夹=====以下脚本可以直接复制运行========================

# 设置文件夹的基础名称和数量
$baseName = "ks"
$folderCount = 500

# 获取当前目录
$currentDirectory = Get-Location

# 循环创建文件夹
for ($i = 1; $i -le $folderCount; $i++) {
# 构建文件夹名称,例如 ks001, ks002, ...
$folderName = $baseName + $i.ToString("D3") # D3表示至少3位数,不足的前面补0
# 创建文件夹的完整路径
$folderPath = Join-Path -Path $currentDirectory -ChildPath $folderName
# 如果文件夹不存在,则创建它
if (-not (Test-Path -Path $folderPath)) {
New-Item -Path $folderPath -ItemType Directory
Write-Host "Created folder: $folderPath"
} else {
Write-Host "Folder already exists: $folderPath"
}
}

====================生成500个文件夹完成===============================

 

=======第二步创建文本并移动到指定文件夹下=============以下脚本可以直接复制运行===============================

# 设置基础名称和数量
$baseName = "HF"
$destBaseName = "ks"
$fileCount = 500

# 获取当前目录
$currentDirectory = Get-Location

# 循环创建文本文件并移动到对应的文件夹
for ($i = 1; $i -le $fileCount; $i++) {
# 构建文件和文件夹名称,例如 HF001, ks001
$fileName = $baseName + $i.ToString("D3") + ".txt"
$destFolderName = $destBaseName + $i.ToString("D3")

# 创建文件夹的完整路径
$folderPath = Join-Path -Path $currentDirectory -ChildPath $destFolderName

# 如果文件夹不存在,则创建它
if (-not (Test-Path -Path $folderPath)) {
New-Item -Path $folderPath -ItemType Directory
Write-Host "Created folder: $folderPath"
}

# 创建文本文件的完整路径
$filePath = Join-Path -Path $currentDirectory -ChildPath $fileName

# 创建文本文件
New-Item -Path $filePath -ItemType File
Write-Host "Created file: $filePath"

# 移动文件到对应的文件夹
Move-Item -Path $filePath -Destination (Join-Path -Path $folderPath -ChildPath $fileName)
Write-Host "Moved file to: $folderPath\$fileName"
}

 

 

 

====================第二步完,文本移到到文件夹中成功===============================

posted @ 2024-11-29 17:44  zhg1016  阅读(58)  评论(0)    收藏  举报