By toolshed


2012-05-11 18:33:22 8 Comments

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 comments

@Paul Sweatte 2012-07-03 21:41:37

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

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] How can I pass arguments to a batch file?

13 Answered Questions

[SOLVED] How to run multiple .BAT files within a .BAT file

  • 2009-07-09 13:47:14
  • Th3Fix3r
  • 701499 View
  • 599 Score
  • 13 Answer
  • Tags:   batch-file cmd

4 Answered Questions

[SOLVED] How to save file structure to text file?

3 Answered Questions

[SOLVED] Script to Create a Monthly folder and move files to it every month

  • 2018-03-15 21:22:23
  • Defca Trick
  • 607 View
  • -2 Score
  • 3 Answer
  • Tags:   powershell

1 Answered Questions

[SOLVED] move two different file types with same name into a new folder with same name

  • 2016-10-20 19:04:24
  • Ali_R4v3n
  • 61 View
  • 0 Score
  • 1 Answer
  • Tags:   batch-file cmd

0 Answered Questions

2 Answered Questions

Using powershell to move/make folders/subfolders based on filename

  • 2015-11-03 16:26:50
  • Clyde2409
  • 1163 View
  • -2 Score
  • 2 Answer
  • Tags:   powershell

1 Answered Questions

[SOLVED] Moving files in windows environment to folders that have part of the file name

  • 2015-03-30 20:26:46
  • Ran Perhar
  • 33 View
  • 2 Score
  • 1 Answer
  • Tags:   batch-file

2 Answered Questions

[SOLVED] Copy files from directory structure but exclude a named folder

2 Answered Questions

[SOLVED] Script to move folders with specific names and characters

  • 2013-08-12 19:26:19
  • Rob
  • 377 View
  • 0 Score
  • 2 Answer
  • Tags:   powershell

Sponsored Content