Only copy web.config files using batch

106 views Asked by At

This is the code i'm using to search for the file i want to be copied.

@echo off
setlocal disableDelayedExpansion

set "src=C:\"
set "dst=C:\test2"
set "search=Web.config"

for /r "%src%" %%F in (*%search%*) do (
set "full=%%~fF"
set "name=%%~nxF"
setlocal enableDelayedExpansion
copy "!full!" "%dst%\!name:%search%=Web.config - Datum - %date:~0,2%-%date:~3,2%-     %date:~6,4%-Tijd-%time:~0,2%-%time:~3,2%-%time:~6,2%!"
endlocal
)

But by this it copies all files with web.config in it (like DEFAULT files and COMMENTS files). and i only want the "XML Config' Files.

Someone knows a way around this ?

1

There are 1 answers

0
Endoro On

you might try this:

@ECHO OFF &SETLOCAL disableDelayedExpansion

set "src=C:\"
set "dst=C:\test2"
set "search=Web.config"
SET "XMLLine1=<?xml version="1.0" ?>"

FOR /f "delims=" %%a IN ('DIR /s /b /a-d "%search%"') DO CALL:SearchAndCopy "%%~a"
GOTO:EOF

:SearchAndCopy
SETLOCAL
FOR /f "usebackqdelims=" %%b IN ("%~1") DO SET "Line1=%%~b"&GOTO:next
:next
IF /i NOT "%Line1%"=="%XMLLine1%" EXIT /b
COPY "%~1" "%dst%\Web.config - Datum - %date:~0,2%-%date:~3,2%-     %date:~6,4%-Tijd-%time:~0,2%-%time:~3,2%-%time:~6,2%"
EXIT /b

Change XMLLine1 for your needs.
Please note, this doesn't work with UTF-8/16/… files.