将文件从子文件夹复制到父文件夹,并使用父文件夹名称重命名该文件

时间:2019-02-28 04:50:29

标签: powershell batch-file

我在桌面上有以下5个文件夹。

enter image description here

这些文件夹中的每个文件夹都有一个子文件夹词,并且该子文件夹词包含文件document.xml。例如,这是文件的路径

(C:\Users\u0119342\Desktop\LEG_DOWNLOAD\BACK UP DOCX\document\Australian Citizenship (Transitionals and Consequentials) Act 2007\word\document.xml).

我想将document.xml从子文件夹中移到父文件夹"Australian Citizenship (Transitionals and Consequentials) Act 2007"并将其重命名为"Australian Citizenship (Transitionals and Consequentials) Act 2007"

我要对其余四个文件夹中的所有document.xml文件执行相同的过程。

如果可以使用Power Shell或批处理脚本执行此操作,请告知我。

谢谢

Venkat

1 个答案:

答案 0 :(得分:1)

我相信这会做好您的工作。试试看。

$Root = "C:\Users\u0119342\Desktop\LEG_DOWNLOAD\BACK UP DOCX\document" 

$Folders = Get-ChildItem -Path $Root

Foreach($Fld in $Folders)
{
    If(Test-Path "$($Fld.FullName)\word\document.xml")
    {
        # Move the file document.xml and rename it
        Move-Item -Path "$($Fld.FullName)\word\document.xml" -Destination "$($Fld.FullName)\$($Fld.Name).xml"

        #Deletes Word folder
        Remove-Item "$($Fld.FullName)\word"
    }
}
相关问题