Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Copy files based on content command line utility

    I have a list of more than 10,000 excel files in location A. I need a free utility (preferably a command line utility since I need to use windows task scheduler to run this every x minutes) which will scan these files and search for a particular string/content in the file. The file should also be created/modified after a specific date. If the file meets these criteria then it must be copied in Location B. I found a free program called Ultrafile Search which managed to identify the files but it does not run with parameters etc so i cannot use it in the scheduler. I also tried WinGrep but it freezes continuously.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,499
    Thanks
    212
    Thanked 852 Times in 784 Posts
    Elstiv,

    Welcome to the Lounge as a new poster.

    Here's some PowerShell code which does what I think you are asking. I have an Excel workbook to calculate Net Worth. In 2009 we bought a motorhome (Fleetwood Expedition). So the code tests for this and just copies the year files containing that string.
    Code:
    $SourceDrivePath = "G:\BEKDocs\Excel\Finances\Net Worth Files"  #Replace w/your drive\dir
    $DestDrivePath = "G:\Test"   #Replace with your drive\dir
    $FileTypeToCopy = "xls"
    $MatchString = "Expedition"
    
    $FileCnt = 0
    
    Get-ChildItem "$SourceDrivePath\*.$FileTypeToCopy"  | `
      Foreach-Object {
          $Test = Select-String -Path $_.FullName -Pattern $MatchString
    
          if ($Test.Filename.Length -gt 0) {
            Write-Host $Test.Filename
            $FileCnt += 1
            $NewFN = $_.BaseName.$FileTypeToCopy
            Copy-Item  $_.FullName "$DestDrivePath\$NewFN"
          }
    
      }
    
      Write-Host "$FileCnt Files Processed"
    Source Directory
    PSStart.JPG
    Destination Directory
    Results.JPG

    All you need to do is change the values in the first 4 lines then create your scheduled task to call powershell.
    On the Action Tab:
    Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powersh ell.exe
    Add Arguments: "Drive\Path to PowerShell program\PS ProgramName.ps1"

    If you don't know how to setup powershell (you have it if you are running Win 7+) see this Thread post #2.

    HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  3. #3
    New Lounger
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Many thanks. I have never used Powershell but will give this a try. One other question if you don't mind please ... is there a way how I can adjust the code to select only those files that were created and accessed before a specific date? I have more than 10,000 files in the directory and I am only interested to apply this script to those files that were created and/or modified after 01/01/2014. Many thanks once again

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,499
    Thanks
    212
    Thanked 852 Times in 784 Posts
    Elstiv,

    Sorry, I forgot to add that test.
    Here's the revised code.
    Code:
    $SourceDrivePath = "G:\BEKDocs\Excel\Finances\Net Worth Files"  #Replace w/your drive\dir
    $DestDrivePath = "G:\Test"   #Replace with your drive\dir
    $FileTypeToCopy = "xls"
    $MatchString = "Expedition"
    $FilterDate = get-date 1/1/2014
    
    $FileCnt = 0
    
    Get-ChildItem "$SourceDrivePath\*.$FileTypeToCopy"  | `
      Foreach-Object {
          $FileDate = $_.CreationTime
          $Test = Select-String -Path $_.FullName -Pattern $MatchString
    
          if ($Test.Filename.Length -gt 0 -and `
              $FileDate -gt $FilterDate) {
            $FileCnt += 1
            $NewFN = $_.BaseName.$FileTypeToCopy
            Copy-Item  $_.FullName "$DestDrivePath\$NewFN"
          }
    
      }
    
      Write-Host "$FileCnt Files Processed"
    Note: Be careful with this because it uses Date Created which CAN be later than Date Modified if you have copied the files from another source. To use the Date Modified for the comparison change
    This: $FileDate = $_.CreationTime
    To This: $FileDate = $_.LastWriteTime

    datecreated.JPG
    HTH
    Last edited by RetiredGeek; 2014-07-09 at 08:46.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  5. #5
    New Lounger
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Many many thanks. It works 100% ! Powershell is really powerful ...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •