How do I move files from an old folder structure t

2019-04-12 17:25发布

问题:

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
    }
}