任务描述:请采用 powershell 脚本,实现快速批量的操作。
(1) 在 windows7 上编写 C:\createfile.ps1 的 powershell 脚本,创建 20个文件 C:\file\file00.txt 至 C:\file\file19.txt,如果文件存在,则删除后,再创建;每个文件的内容同主文件名,如 file00.txt 文件的内容为“file00”。
第一步,先创建file目录,和powershell脚本
脚本的内容如下:
# 创建文件夹(如果不存在)
$folderPath = "C:\file"
if (-not (Test-Path $folderPath)) {
New-Item -ItemType Directory -Path $folderPath | Out-Null
}
# 循环创建 20 个文件
for ($i=0; $i -le 19; $i++) {
# 构建文件路径
$fileName = "file{0:D2}.txt" -f $i
$filePath = Join-Path -Path $folderPath -ChildPath $fileName
# 如果文件存在,则删除
if (Test-Path $filePath) {
Remove-Item -Path $filePath -Force
}
# 创建文件并写入内容
Add-Content -Path $filePath -Value $fileName
}
powershell脚本与shell脚本都需要给所写的脚本执行权限
请将上述脚本保存为 C:\createfile.ps1 文件,并在 PowerShell 中执行以下命令以启用脚本执行权限:
Set-ExecutionPolicy Unrestricted
然后在 PowerShell 中执行脚本:
c:\createfile.ps1
脚本将在 C:\file 文件夹下创建 20 个文件,每个文件的内容与文件名相同(如 file00.txt 内容为 "file00")。如果文件已存在,则会先删除再创建。可以自己去实验一下效果
给迷途的小弟弟一点光,让你在服务的道路,不至于迷茫