Batch file to search directory for file type and copy to new location


  1. Posts : 1
    Windows 10 Enterprise
       #1

    Batch file to search directory for file type and copy to new location


    Hi all,

    Not sure if posting in correct area, I would like to have a batch file that can search a directory or drive for file type and copy that file into a new directory renaming the file to reflect what users folder it came out of.

    To be a little more specific, I have multiple systems all Windows 10 ver 1909, most likely multiple user profiles on each. My task is to get all the old PST files from these systems copy to a central location then use Azure import to upload and archive into Office 365 users online archive.

    I am confident that the first part can be done through Batch file, as most pst files are labelled outlook.pst, I would like it to copy to new location and rename the file to the username of the users profile the pst file came out of, but also if there are multiple outlook.pst for same user it might be wise to append 1,2,3 etc.

    E.g.

    Old system: New location:
    c:\users\Joe.Blogs\outlook files\outlook.pst F:\PSTfiles\Joe.blogs\joe.blogs.pst
    c:\users\Jane.Blogs\outlook files\outlook.pst F:\PSTfiles\Jane.blogs\jane.blogs.pst
    c:\users\Joe.Blogs\outlook files\office.pst F:\PSTfiles\Joe.blogs\joe.blogs1.pst
    c:\users\Joe.Blogs\outlook files\joe.blogs.pst F:\PSTfiles\Joe.blogs\joe.blogs2.pst
    c:\users\Joe.Blogs\outlook files\joe.blogsnewprofile.pst F:\PSTfiles\Joe.blogs\joe.blogs3.pst

    Ideally, it would be really good to be able to run this batch file and it stores pst to a network location (permissions would be configured to allow this)

    I also have a cupboard of drives that I would ideally like to connect to a desktop machine and target the search on that drive.

    I hope this makes sense? Can anyone help?

    Many thanks

    John
      My Computer

  2. Samuria's Avatar
    Posts : 6,211
    windows 10
       #2

    The main problem is have permission to read files in a user's profile as by default they will be protected
      My Computer

  3. KeithM's Avatar
    Posts : 826
    Microsoft Windows 10 Home
       #3

    There's no point in dealing with batch at this point. Learn PowerShell. A relatively simple copy/rename is a great place to start

    Code:
    gci c:\users -dir | ? Name -notMatch 'Public|Administrator' | ForEach{
        gci (Join-Path $_.FullName 'outlook files') *.pst | FOrEach{
            [PSCustomObject]@{
                'FullName' = $_.FullName
                'UserName' = $_.directory.Parent.Name
            }
        }
    } | group UserName | ForEach{
        $DestFolder = Join-Path 'F:\PSTFiles' $_.Name
        If ( Test-Path $DestFolder ) {
                [ref]$i = 0
                $_.Group | ForEach {
                    $Splat = @{
                        'Path'        = $_.FullName
                        'Destination' = Join-Path $DestFolder ( $_.UserName + $i.Value++ + '.pst' )
                    }
                    Copy-Item @Splat
                }
                gci $DestFolder -File | ? Name -like '*0.pst' | Ren -NewName { $_.Name -replace '0.pst', '.pst' }
        }
    }
      My Computer

  4. idgat's Avatar
    Posts : 970
    Windows 10 Pro
       #4

    Windows built-in Robocopy - fast.

    robocopy {source}:\ {destination}:\ /copy:DAT *.type1 *.type2 /s /xd recyc* $recyc* syst* /a-:rsh /r:0 /w:0

    Meaning : Using Robocopy, from the source drive/folder, to the destination drive/folder, copy all (*) files with the extension type1, and all (*) files with the extension type1, /s = search subdirectories on the source drive, /a - reset file atributes on completion, /r:0 = if it doesn't work, dont bother keep trying, move on; /w:0 don't wait 0 seconds for anything to happen, just get on with it.

    In your case, I'd be looking at keeping the full path of the source, because there are files of identical names, so ...

    robocopy c:\ f:\pstfiles /copy:DAT *.pst /s /a-:rsh /r:0 /w:0

    Save the above text to notepad and save the file as a .bat file, or copy and paste the text into a command prompt window and run.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 02:12.
Find Us




Windows 10 Forums