locked
Script to find multiple extension file by name and content inside files and relocate RRS feed

  • Question

  • Hi friends,

    Sorry i am with one more big hurdle...

    My problem is, I been given a task to filter file by its name and  filter if contains the string and will be in multiple extensions, if found need to move to server.

    I have 3 different scripts all are working fine as per the expectation, before the above task said i been given only one task to perform later on one more said and one more included, now the situation are all the 3 script do the same result i wanted to merge all the 3 task and get moved all the desired file to one location, can anyone please please help me out to acquire this...

    Files have saved by names like Security,Subscription,Resource also files have particular strings like Security,Subscription,Resource - Files are saved with multiple extension like .txt, .doc, .docx, .xls, .xlsx formats.. Also same file name are locating in multiple drive like "D:\security Also in E:\security".

    Because of this case i included a $nextname option to automatically rename file to security_1, Security_2 as all the files are getting saved under Username share only like $share:\S_1,S_2,s_3

    Below is my 3 script wanting to make it as one as ultimate process of the task to move file by name,content with any extension...

    -------------------------------------------------------

    (New-object -comobject wscript.network).mapnetworkdrive('L:', '\\10.0.0.5\My Files', $false, 'domain\Username','password')
    $share = $env:username
    $sharepath = "\\10.0.0.5\My Files\$Share"

    $src = "C:\Users\$env:username\desktop","C:\users\$env:username\Documents","C:\Users\$env:username\Downloads", 'D:\', 'E:\'
    $num=1
    Clear

    If (!(Test-Path $Sharepath)) {
    New-Item $Sharepath -type Directory
    }

    Get-ChildItem -Path $src -Include *.txt, *.doc, *.docx, *.docx, *.xlsx, *.xls -Recurse |
          Where-Object {$_.Name -match '^security.|^subscription.|^Resource.'}| ForEach-Object {

        $nextName = Join-Path -Path $Sharepath -ChildPath $_.name

        while(Test-Path -Path $nextName)
        {
          $nextName = Join-Path $Sharepath ($_.BaseName + "_$num" + $_.Extension)  
          $num+=1
        }

        $_ | Move-Item -Destination $nextName -verbose
    }


                                        **********************************************

    $sharepath = "\\10.0.0.5\My Files\$Share"
    $src = "C:\Users\$env:username\desktop","C:\users\$env:username\Documents","C:\Users\$env:username\Downloads", 'D:\', 'E:\'

    $word = New-Object -ComObject word.application

    $docs = Get-ChildItem -Path $src -Include *.docx, *.doc -Recurse
    foreach ($doc in $docs)
    {
    if($word.Documents.Open($doc.FullName).Range().text -match 'security' -or 'subscription' -or 'Resource')
      {
      $word.Application.ActiveDocument.close()
      Write-Host "$doc contains 'mentioned wording'"
      Move-Item -Path $doc.FullName -Destination $sharepath -Verbose
      }
      else
      {
      $word.Application.ActiveDocument.close()
      }
        }

                                                   **********************************

    $sharepath = "\\10.0.0.5\My Files\$Share"
    $src = "C:\Users\$env:username\desktop","C:\users\$env:username\Documents","C:\Users\$env:username\Downloads", 'D:\', 'E:\'

    Get-ChildItem -Path "$src" -Include *.doc, *.xls, *.txt -Recurse |Select-String -List -Pattern "subscription", "security", “Resource” |Move-Item -Destination "$sharepath" -Verbose

    Please help out!

    Thanks in advance

     




    Mohammed...

    Saturday, November 4, 2017 4:13 PM

All replies