Search code examples
batch-filecommand-linecopysubdirectory

Trying to copy a file to certain subfolders using command line


I'm trying to find out how to use command line (with the intent to create a batch file so I can do this automatically) to copy a single file into multiple subfolders,

The current folder setup is:

C:\MainFolder:

DestinationFolder1
       Sending
       Receiving
DestinationFolder2
       Sending
       Receiving
DestinationFolder3
       Sending
       Receiving

How would I send C:\Example.txt to the Sending directory in each Destination folder?


Solution

  • cmdline version:

    for /f "delims=" %i in ('dir /b /s /ad C:\Mainfolder ^| findstr /i "Sending"') do copy "C:\Example.txt" "%~i" /Y
    

    Batch file version (only difference is double %:

    for /f "delims=" %%i in ('dir /b /s /ad C:\Mainfolder ^| findstr /i "Sending"') do copy "C:\Example.txt" "%%~i" /Y
    

    It simply runs a dir command with search function on each folder inside of C:\Mainfolder. Using findstr it only gets the folders that contains Sending then copies the file to it.

    for more help on the above commands, open cmd and type:

    for /?
    dir /?
    findstr /?