使用powershell合并两个文件夹并根据源文件夹重命名文件 [英] Using powershell to merge two folders and rename files based on source folder
本文介绍了使用powershell合并两个文件夹并根据源文件夹重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组这样的文件:
2015_09_22
|____ foo
|____ common.ext
|____ common.1.ext
|____ common.2.ext
|____ common.3.ext
|____ bar
|____ common.ext
|____ common.1.ext
|____ common.2.ext
我想将它们合并成这样的结构,使用源文件夹名称作为字符串添加到文件名:
I want to merge them into a structure like this, using the source folder name as a string to prepend to the filename:
2015_09_22
|____ foo_common.ext
|____ foo_common.1.ext
|____ foo_common.2.ext
|____ foo_common.3.ext
|____ bar_common.ext
|____ bar_common.1.ext
|____ bar_common.2.ext
{date}\foo 和 {date}\bar 的格式是固定的,但内容可能有不同数量的文件.
The format of {date}\foo and {date}\bar is fixed but the contents could have a variable number of files with those names.
推荐答案
您可以使用以下内容:
cd .\2015_09_22\
Get-ChildItem *\* | ForEach {$_.MoveTo("$($_.Directory.Parent.FullName)\$($_.Directory.Name)_$($_.Name)")}
这会移动文件,但不会删除目录,并且有点难以阅读.所以也许这更合理:
This moves the files, but doesn't remove the directories, and is a little hard to read. So maybe this is more reasonable:
cd .\2015_09_22\
foreach ($dir in (Get-ChildItem -Directory)) {
foreach ($file in (Get-ChildItem $dir -File)) {
$dest = "$($file.Directory.Parent.FullName)\$($file.Directory.Name)_$($file.Name)"
$file.MoveTo($dest)
}
$dir.Delete()
}
这篇关于使用powershell合并两个文件夹并根据源文件夹重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文