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.
LVL 8
mbkitmgrAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
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
setlocal 

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


»bp
0
 
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?


»bp
0
 
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
0
 
mbkitmgrAuthor Commented:
Yes they are defined
0
 
NVITConnect With a Mentor Commented:
...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


Example:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.