Solved

Replicate folder structure and move files based on Extension

Posted on 2014-03-25
10
736 Views
Last Modified: 2014-03-29
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
0
Comment
Question by:Jeremyw
  • 4
  • 3
  • 3
10 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39954733
You can try this:
# recreate structure and move files
# script created for E-E, question Q_28397320

$inputPath = "c:\Folder\Data"
$outputPath = "d:\ArchiveFolder\ArchiveData"

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

#recreate the folder structure
gci $inputPath -Directory -Recurse | %{

    #remove the original folder from the folder name; that's why the "//"  was needed
    $folderName = $_.FullName -replace $inputPath, ""
    New-Item -ItemType Directory -Path "$outputPath\$folderName"
}

#move the files
gci $inputPath -Include @("*.l03", "*.l04", "*.l05", "*.l06", "*.l07", "*.l08", "*.l09", "*.l10") -Recurse | %{
    $fileName = $_.FullName -replace $inputPath, ""
    Move-Item $_.FullName -Destination "$outputPath\$fileName"
}

Open in new window

HTH,
Dan
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39954834
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.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39954838
@Raheman Mohammed Abdul: it's 5 lines above :)
Use -Include @()

The OP did require a move, though, not just a copy...
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39954879
It's late night here in london so thought will work on it tomorrow. We can replace with move instead of copy. Thanks Dan.
0
 
LVL 3

Author Comment

by:Jeremyw
ID: 39954940
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
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39955081
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.
0
 
LVL 3

Author Comment

by:Jeremyw
ID: 39955089
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
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39955571
Ignore this please.
0
 
LVL 3

Author Comment

by:Jeremyw
ID: 39964019
I apologize for the delay Dan.  After a few tests, this worked exactly as requested.   Thanks so much for your help.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39964146
Glad I could help!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question