How do I move files from an old folder structure to new?

懵懂的女人 提交于 2019-12-21 21:43:01

问题


I want to modify my existing folder structure. I had a file tree that was organized in the following way:

Client Name
  State/Province
    City
      Order Number

But I have modified it to add an address before the order number separated by a hyphen, as such:

Client Name
  State/Province
    City
      Order Number - Address

I created the new folder structure with a macro I had that generated the original, and I figured this was much easier than renaming the existing folders.

So now I want to upload the empty folders to my server, but before I do that I want to take the files from all the old structure and put them into the new one.

Simply, I am trying to write a script to match the folder name in the original hierarchy to the new hierarchy that contains the original name plus address, and then copy the files in the original folder to the similarly named one in the new structure.

How would I do this? VBA, Powershell, Batch Command? I'm not very well versed in PS.


回答1:


Use a foreach loop with the Get-ChildItem, Copy-Item, and Test-Path Powershell cmdlets:

#Get all address subfolders
$addr_folders = Get-ChildItem c:\"Client Name"\State/Province\City\ -recurse | Where-Object {$_.PSIsContainer -eq $True}

#Loop through all address subfolders
foreach ($address in $addr_folders)
{
  #Copy contents from existing subfolder path to new folder
  if (Test-Path c:\"Client Name"\State/Province\City\"Order Number"\$address)
    {
   Copy-Item c:\"Client Name"\State/Province\City\"Order Number" c:\"Client Name"\State/Province\City\"Order Number - " $address
    }
}


来源:https://stackoverflow.com/questions/10556768/how-do-i-move-files-from-an-old-folder-structure-to-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!