如何批量的去修改文件名并且将文件集中复制到新的地方


PowerShell交流中心分类: Powershell基础如何批量的去修改文件名并且将文件集中复制到新的地方
0
redsand asked 5年 ago

各位,我是新手,现在有如下问题:
文件夹test下有子文件夹001、002、……,每个子文件夹下都有一个images的子文件夹,images下有00001.jpg,00002.jpg……等文件
现在的目标是将test\001\images\00001.jpg,00002.jpg…… 改名为00100001.jpg,00100002.jpg……
test\002\images\00001.jpg,00002.jpg…… 改名为00200001.jpg,00200002.jpg……
最后将所有的改名后的文件复制到newimage文件夹下,即newimage文件夹下包含:
00100001.jpg,00100002.jpg……00200001.jpg,00200002.jpg……

Ircon replied 5年 ago

这个还是用程序比较方便,如果是用脚本反而写起来有点麻烦。

4 Answers
1
Best Answer
nutix answered 5年 ago
$root = '.\test'
$new = '.\newimage'
if(!(Test-Path $root)) {
ForEach-Object { 1..9 } |
ForEach-Object {
$folder = "$root\{0:000}\images" -f $_
New-Item $folder -ItemType Directory |
Out-Null
ForEach-Object { 1..100 } |
ForEach-Object {
New-Item ("$folder\{0:0000}.jpg" -f $_) |
Out-Null
}
}
New-Item $new -ItemType Directory | Out-Null
}

Get-ChildItem $root -Directory -Name -PipelineVariable d |
ForEach-Object {
Get-ChildItem "$root\$d\images\*.jpg" -File -PV f |
Copy-Item -Destination { "$new\$d$($f.Name)" }
}
0
Ircon answered 5年 ago

建议使用 python 或java

0
nutix answered 5年 ago

这插入代码功能怎么搞的

1
神经元短路 answered 5年 ago
gci d:\test  -File  -Recurse | %{
$newname = $_.DirectoryName.Split("\")[-2] + $_.BaseName + $_.Extension
Copy-Item $_.FullName -Destination d:\test1\$newname
}
神经元短路 replied 5年 ago

忘了改名
gci d:\test -File -Recurse | %{
$newname = $_.DirectoryName.Split(“\”)[-2] + $_.BaseName + $_.Extension
Copy-Item $_.FullName -Destination d:\test1\$newname
Rename-Item -Path $_.fullname -NewName $newname
}