各位,我是新手,现在有如下问题:
文件夹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……
4 Answers
Best Answer
$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)" }
}
gci d:\test -File -Recurse | %{
$newname = $_.DirectoryName.Split("\")[-2] + $_.BaseName + $_.Extension
Copy-Item $_.FullName -Destination d:\test1\$newname
}
忘了改名
gci d:\test -File -Recurse | %{
$newname = $_.DirectoryName.Split(“\”)[-2] + $_.BaseName + $_.Extension
Copy-Item $_.FullName -Destination d:\test1\$newname
Rename-Item -Path $_.fullname -NewName $newname
}

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