Search code examples
batch-filecopyxcopy

Batch file copy similar files except a certain one


I have code that copies certain types from one directory to another. Below is not the exact code but something similar since it has some personal information.

for /R Documents\OtherFiles\BatTest\ %%f in (
    *.aspx, *.xml, *.asax, *.asmx, *.config, *.Master
) do copy %%f Documents\OtherFiles\DestinationTest\

However in the folder there could be multiple config files.

BatTest\web.config
BatTest\web2.config
BatTest\test.config

How do I transfer all config files such as web.config and test.config but not include the web2.config? I understand that I can simply manually type it all in but in case there's more config files in the future, would it be possible to avoid typing it all again? Such as if there's a site1.config and prod1.config that I need to transfer as well.


Solution

  • for /R Documents\OtherFiles\BatTest\ %%f in (
     *.aspx, *.xml, *.asax, *.asmx, *.config, *.Master
    ) do echo %%~nxf|findstr /x /L /i /g:"myexcludefile.txt" >nul&if errorlevel 1 copy %%f Documents\OtherFiles\DestinationTest\
    

    Should work for you (I'd test it first by using echo copy...)

    findstr would read in the filename and extension of %%f, and try to find and exact (/x) literal (/L) case-insensitive (/i) match in the file referenced by /g:. If the name is found in the file, errorlevel will be set to 0. if not errorlevel 0 tests errorlevel for being non-zero and executes the copy if the filename is not found in the file.

    All you'd need to to is set up myexcludefile.txt with the excluded filenames, one to a line, like

    web2.config
    excludemetoo.config
    

    Your existing batch would appear to "flatten" the source tree, producing a single directory. I suppose that's what you want.


    Here is a working version

    @ECHO OFF
    SETLOCAL
    SET "targetdir=U:\documents\OtherFiles\BatTest\GOSCCMWeb"
    SET "backupdir=U:\documents\OtherFiles\DestinationTest"
    
    :: This part is simply to force the two directories to exist and put some dummy files into them
    
    MD %targetdir% 2>nul
    MD %backupdir% 2>nul
    FOR /L %%a IN (1,1,5) DO COPY nul "%targetdir%\file%%a.config" >NUL 2>nul
    FOR /L %%a IN (1,1,5) DO COPY nul "%targetdir%\web%%a.config" >NUL 2>nul
    
    for /R "%targetdir%" %%f in (
        *.aspx, *.xml, *.asax, *.asmx, *.config, *.Master
    ) do echo %%~nxf|findstr /x /L /i /g:"q27990439.txt" >nul&if errorlevel 1 ECHO copy "%%f" "%backupdir%\"
    GOTO :EOF
    

    The exclude file q27990439.txt contains

    web2.config
    

    Listing of the target directory

     Volume in drive U has no label.
     Volume Serial Number is 02DD-1000
    
     Directory of U:\documents\OtherFiles\BatTest\GOSCCMWeb
    
    18/01/2015  03:50    <DIR>          .
    18/01/2015  03:50    <DIR>          ..
    18/01/2015  03:51                 0 file1.config
    18/01/2015  03:51                 0 file2.config
    18/01/2015  03:51                 0 file3.config
    18/01/2015  03:51                 0 file4.config
    18/01/2015  03:51                 0 file5.config
    18/01/2015  03:51                 0 web1.config
    18/01/2015  03:51                 0 web2.config
    18/01/2015  03:51                 0 web3.config
    18/01/2015  03:51                 0 web4.config
    18/01/2015  03:51                 0 web5.config
                  10 File(s)              0 bytes
                   2 Dir(s)   2,126,544,896 bytes free
    

    Result of executing this batch:

    copy "U:\documents\OtherFiles\BatTest\GOSCCMWeb\file1.config" "U:\documents\OtherFiles\DestinationTest\"
    copy "U:\documents\OtherFiles\BatTest\GOSCCMWeb\file2.config" "U:\documents\OtherFiles\DestinationTest\"
    copy "U:\documents\OtherFiles\BatTest\GOSCCMWeb\file3.config" "U:\documents\OtherFiles\DestinationTest\"
    copy "U:\documents\OtherFiles\BatTest\GOSCCMWeb\file4.config" "U:\documents\OtherFiles\DestinationTest\"
    copy "U:\documents\OtherFiles\BatTest\GOSCCMWeb\file5.config" "U:\documents\OtherFiles\DestinationTest\"
    copy "U:\documents\OtherFiles\BatTest\GOSCCMWeb\web1.config" "U:\documents\OtherFiles\DestinationTest\"
    copy "U:\documents\OtherFiles\BatTest\GOSCCMWeb\web3.config" "U:\documents\OtherFiles\DestinationTest\"
    copy "U:\documents\OtherFiles\BatTest\GOSCCMWeb\web4.config" "U:\documents\OtherFiles\DestinationTest\"
    copy "U:\documents\OtherFiles\BatTest\GOSCCMWeb\web5.config" "U:\documents\OtherFiles\DestinationTest\"
    

    Note the absence of "web2.config"

    The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files.

    Changing /g: to /g/c: in the original should cause findstr to ignore the /g switch as /c:... is not a valid filename. The /c:"whatever" would then have established whatever as a string-to-match. This would not have been matched by any entry in the stream of filenames, so errorlevel would be set to 1 each time and the copy command would be generated.

    The location of the file must be supplied explicitly to the /g: switch, but will be implicitly relative to the directory from which the command to execute the batch file is run if it is not provided as an absolute location. As such, it can be placed wherever you like - just provide the absolute path if required.