Search code examples
windowsbatch-filedir

Remote server DIR, only part of file name?


I want to query three folders (on three separate servers) for a list of client names.

Basically, in each of these folders, I have a bunch of .EXE's that are named as follows:

ClientName_ProductName.exe

From my main server, I'd like to output JUST the "ClientName" part to a ClientNames.txt file.

Here is what I had so far, but it is pulling ALL information in (all files in the directory):

:: Setting  the client names file

set clientnamefile="C:\ClientNames.txt"


:: Copying directory information to ClientNames.txt

dir *_ProductName.exe "\\server1\c$\Program Files (x86)\Folder\" /B > %clientnamefile%
dir *_ProductName.exe "\\server2\c$\Program Files (x86)\Folder\" /B >> %clientnamefile%
dir *_ProductName.exe "\\server3\d$\Program Files (x86)\Folder\" /B >> %clientnamefile%

I want to end up with the following inside of a .txt file (which will then be used by another one of my scripts):

ClientName1
ClientName2
ClientName3
ClientName4
ClientName5

Solution

  • With the help of the internet, changing some things to fit my needs, and also help from MrLister in pointing out my rookie syntax mistake, I was able to get this working.

    I now have a single text file which contains only the ClientName portion of my ClientName_ProductName.exe files from three different network locations.

    The code is below:

    :: Setting  the client names file
    set textfile="C:\Util\TFProAdmin Update\ClientName_Dirty.txt"
    set textfile2="C:\Util\TFProAdmin Update\ClientName.txt"
    
    set server1="\\clientimport01\c$\Program Files\Transfinder\TransfinderPro
    set server2="\\clientimport02\c$\Program Files (x86)\Transfinder\Routefinder Pro
    set server3="\\clientimport03\d$\Program Files (x86)\Transfinder\Routefinder Pro
    set fileextension=*_ProductName.exe"
    set searchline=_ProductName.exe
    
    :: *********************************************************************************
    :: ******************* DO !NOT! CHANGE ANYTHING BELOW THIS POINT ******************* 
    :: *********************************************************************************
    
    
    @Echo off
    cls
    @Echo.
    @Echo *********************************************************
    @Echo.
    @Echo        Starting client name collection process
    @Echo.
    @Echo *********************************************************
    @Echo.
    @Echo off
    
    :: Gathering the list of _ProductName.exe files from each of the import servers and writing it to a 'dirty' (temp) text file
    dir %server1%\%fileextension% /B > %textfile%
    dir %server2%\%fileextension% /B >> %textfile%
    dir %server3%\%fileextension% /B >> %textfile%
    
    :: Cleaning up the original text file to remove _ProductName.exe from each line
    setlocal enabledelayedexpansion
    
    @Echo. > %textfile2%
    FOR /F "usebackq delims=" %%G IN (%textfile%) DO (
      Set "line=%%G" & echo !line:%searchline%=!
    ) >> %textfile2%
    
    :: Deleting temporary 'dirty' text file
    del %textfile%
    
    end