Link to home
Start Free TrialLog in
Avatar of Jeremyw
JeremywFlag for United States of America

asked on

Replicate folder structure and move files based on Extension

I have thousands of subfolders I need to duplicate in a new location, however, I need to keep the folders structure and any files that aren't being moved in the old location.   I then need to loop through each subfolder and if any of the extensions are matched, then move those files to the same subfolder in the new location.  

Parent Folder - c:\Folder\Data\subfolders
New Folder - d:\ArchiveFolder\ArchiveData\subfolders

Extensions:
.l03, .l04, .l05, .l06, .l07, .l08, .l09, .l10

I'm a powershell newbie, so be gentle.  :)

Thanks,

Jeremy
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
copy -Recurse -Filter *.l03 -Path c:\Folder\Data\Subfolders -Destination d:\ArchiveFolder\ArchiveData -ErrorAction SilentlyContinue


and similarly replace *.l03 with other extensions. I am working on it to include all extensions in one line.
@Raheman Mohammed Abdul: it's 5 lines above :)
Use -Include @()

The OP did require a move, though, not just a copy...
It's late night here in london so thought will work on it tomorrow. We can replace with move instead of copy. Thanks Dan.
Avatar of Jeremyw

ASKER

Dan,

So yours will do the following.  

Take
C:\Folder\Data\Subfolder1
C:\Folder\Data\Subfolder2
etc.

Create the folders in
D:\ArchiveFolder\ArchiveData\Subfolder1
D:\ArchiveFolder\ArchiveData\Subfolder2
etc.

Then move any of the .lxx extension files to the the folder on D: it came from.  

Also need to make sure it keeps the original folder structure on C: as we will be leaving files in those folders.  

What exactly do these lines do?  I'm not really understanding the replace \\

#double up the "\" to use on replace  ("\" must be escaped): result will be c:\\folder\\data\\
$inputPath = $inputPath -replace "\\", "\\"
$inputPath += "\\"

Thanks,

Jeremy
I need to get rid of "C:\Folder\Data\" part from the file name, and keep only the "Subfolder1", "Subfolder1\Subfolder2" etc part.

I'm doing that by searching for the string "C:\Folder\Data\" and replacing it with nothing (effectively removing it). The problem is that "\" has a special meaning in Powershell and you need to escape it by doubling it.

My solution does not move the folders. It creates empty folders in the target path, then moves only files. The original structure will remain intact.

There is a chance that empty folders will remain in the source path. Let me know if you want to add to the script so it looks for empty folders and deletes them.
Avatar of Jeremyw

ASKER

Empty folders on either side will be ok.  Just had to make sure the original structure stayed the same since there are other files that will be used.  Will do some tests tomorrow.

Thanks,

Jeremy
Ignore this please.
Avatar of Jeremyw

ASKER

I apologize for the delay Dan.  After a few tests, this worked exactly as requested.   Thanks so much for your help.
Glad I could help!