Search code examples
regexwindowscmddirectoryfindstr

Copy files found by findstr?


Im trying to copy files I found with findstr to another folder. This is what I made that lists all the files I want to be copied

dir * /a /b  |findstr ^FALSE[0-9][0-9]\.txt$

How can I redirect this list to a copy command? Im trying not to use a batch file.


Solution

  • To me, this is easier in PowerShell. If you are on a supported Windows system, PowerShell will be available. When you are confident that the correct files willbe copied to the correct location, remove the -WhatIf from Copy-Item.

    Get-ChildItem -File -Path 'C:\src\t' -Filter 'FALSE*.txt' |
        Where-Object { $_.Name -match '^FALSE[0-9]{2}\.txt$' } |
        ForEach-Object { Copy-Item -Path $_.FullName -Destination 'C:\the\other\place' -WhatIf }
    

    If this is only in a command shell, you could use:

    gci -File 'C:\src\t\FALSE*.txt'|?{$_.Name -match '^FALSE[0-9]{2}\.txt$'}|%{ Copy-Item $_.FullName 'C:\the\other\place' -WhatIf }