Search code examples
cmdwindows-server-2008remote-server

How to recursively search for a file in a remote server


I am using dir \\pwdf1280\rep\"*.txt" /S to search for all the txt files in the folder rep. I used the /S argument to search for the same in the sub directories, but the problem is the command runs forever. I am basically writing a perl script which has to find these files along with their timestamp.

Is there any other approach to solve this, or can I improve on the above command?

The problem is the dir command I mentioned above runs forever and does not display files present in rep's sub directories. It just displays files present in rep directory. I want to search for a file in the rep's sub directories.


Solution

  • One way that might speed the process up is to run the directory search on the remote machine using PowerShell.

    C:\src\powershell> type .\rc001.ps1
    Invoke-Command `
        -ComputerName 'pwdf1280' `
        -Command { Get-ChildItem '\\pwdf1280\rep' -Recurse -Filter '*.txt' } |
        ForEach-Object { $_.FullName }
    

    Then, from a cmd script run:

    C:\src\powershell> powershell -NoProfile -File .\rc001.ps1
    

    Alternatives might be to run the dir command remotely using psexec from SysInternals or plink from PuTTY.