Method to copy files by Owner.

Method to copy files by Owner.

I have a client that has thousands of files from different users of their AD domain, stored in a folder, and am loking for a method where I can move the files to the folders of the individual treams inlvolved.:

Copy s:\*.* S:\AdminStaff Whereowner = Admin
Copy S:\*.* S:\SalesStaff where owner = sales

It could be a command switch for Xcopy, Robocopy or another util where you've sucessfully used it.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
If you do a DIR /Q on the folder, is the owner displayed there the owner you want to use for the split?

Shaun VermaakTechnical Specialist/DeveloperCommented:
Very possible to do this but before posting any PowerShell etc. please check if owners are actually defined. You might find that it is not
mbkitmgrAuthor Commented:
Yes they are defined
...thousands of files from different users of their AD domain, stored in a folder
This powershell works.
Change $srcdir and $tgtdir to your needs.
It's in test mode. To run it for real, remove the write-host prefixes

$srcdir = 'c:\local'
$tgtdir = 'c:\local\test\ee'

$item = Get-ChildItem "$srcdir" | where {(! $_.PSIsContainer )} | get-acl

$groups = $item | group owner
$tgt = ""
foreach ($group in $groups) {
    $username = (($group.Values) -split '\\')[1]
    $userFiles = $group.Group | select path | Split-Path -NoQualifier
    $tgt = "$tgtdir" + "\" + $username + "Staff"
    write-host md $tgt -ErrorAction SilentlyContinue
    foreach ($file in $userfiles) {
        write-host Copy-Item "$file" -destination $tgt -ErrorAction SilentlyContinue

Open in new window

md c:\local\test\ee\user1Staff
Copy-Item C:\local\_ReadMe_XP_01.txt.bak -destination c:\local\test\ee\user1Staff
Copy-Item C:\local\_Transwiz Support Questions.txt.bak -destination c:\local\test\ee\user1Staff
Copy-Item C:\local\_User Issues - dont install yet.txt.bak -destination c:\local\test\ee\user1Staff
md c:\local\test\ee\user2Staff
Copy-Item C:\local\batchprocess.log -destination c:\local\test\ee\user2Staff
Copy-Item C:\local\CatDumpTest.bat.bak -destination c:\local\test\ee\user2Staff
Copy-Item C:\local\CatDumpTestSrc.txt -destination c:\local\test\ee\user2Staff

Open in new window

Bill PrewCommented:
And since you tagged this question in Windows Batch, here is a small BAT script that should get you started.  It uses the owner info from DIR /Q and moves to the appropriate folder.  Adjust SET statements near the top to specify your folders.

@echo off

rem Define from and to folders
set BaseDir=c:\temp
set DestDir=b:\ee

rem Remove any trailing backslashes
if "%BaseDir:~-1%" EQU "\" set BaseDir=%BaseDir:~0,-1%
if "%DestDir:~-1%" EQU "\" set DestDir=%DestDir:~0,-1%

rem Look at all files in folder, including their owner
for /f "tokens=5*" %%A in ('dir /a-d /q "%BaseDir%\*.*" ^| findstr /b /c:" " /v') do (

    rem Extract just owner (no computer / domain)
    for /f "tokens=2 delims=\" %%U in ("%%~A") do (

        rem Create destination folder if needed
        if not exist "%DestDir%\%%UStaff\" echo "%DestDir%\%%UStaff\"

        rem Copy file to destination
        copy "%BaseDir%\%%~B" "%DestDir%\%%UStaff"



Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.