Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Hello I am trying to create multiple copies of a single file in multiple directories?

for example I have 6 folders with a single file in each folder named lp.html, How can I run a batch to create 100 copies of said files in each directory.

The code I have creates multiple copies but I have to do it one by one and it becomes tedious when I have over 100 folders to do.

So how can I turn this batch file to do what I am trying to accomplish. [I Am Running Windows 7]

ADDED INFO: All folders are in the same parent folder, files in the folders are different from one another

   @echo off

   for /L %%i IN (1,1,100) do call :docopy %%i
   goto end

   :docopy
   set FN=00%1
   set FN=%FN:~-3%

   copy source-file.html poll%FN%.html

   :end
share|improve this question
3  
What exact version of Windows are you dealing with? In the 21st century, batch is almost never the answer. –  Ryan Ries Aug 30 '13 at 14:49
    
I am using windows 7 –  Jesus Landra Aug 30 '13 at 14:50
    
Are all of those folders in the same parent folder ? –  Lorenz Meyer Aug 30 '13 at 15:16
    
Yes all of the folders are in the same parent folder –  Jesus Landra Aug 30 '13 at 15:27

2 Answers 2

Use the forfiles command. Run forfiles /? for help.

If you are concerned about processing the copies in parallel, you can create 6 different scripts, one for each directory, and then run them all from a single script using start scriptname.bat which won't wait for a return value like call does.

share|improve this answer

Try this. It ain't pretty, cause this *$#? editor has lost the indentation, line breaks, Etc.

--- REVISED SCRIPT

@echo off

setlocal

set ROOT_DIR=c:\temp\test

for /f "tokens=*" %%D in ('dir /ad /b "%ROOT_DIR%"') do call :PROCESS_DIR "%ROOT_DIR%" "%%D"

endlocal

goto END


:PROCESS_DIR

set PD_ROOT=%1
set PD_ROOT=%PD_ROOT:"=%
set PD_DIR=%2
set PD_DIR=%PD_DIR:"=%
set PD_FIRST_FILE=
set PD_SOURCE_FILE=

REM ***
REM *** GET THE NAME OF THE FIRST FILE IN THIS FOLDER.
REM ***
for /f "tokens=*" %%F in ('dir /o-n /b "%PD_ROOT%\%PD_DIR%"') do set PD_FIRST_FILE=%%F

if "%PD_FIRST_FILE%" == "" (
    echo WARNING : No files found in [%PD_DIR%] - skipping.
    goto END
    )

set PD_SOURCE_FILE=%PD_ROOT%\%PD_DIR%\%PD_FIRST_FILE%

REM ***
REM *** CREATE FILES.
REM ***
for /l %%I in (1,1,100) do call :CREATE_FILE_COPY "%PD_ROOT%\%PD_DIR%" "%PD_SOURCE_FILE%" 00%%I

goto END

:CREATE_FILE_COPY

set CFP_DIR=%1
set CFP_DIR=%CFP_DIR:"=%
set CFP_FILE=%2
set CFP_INDEX=%3
set CFP_FILE_NO_EXT=

REM ***
REM *** GET FILE NAME WITHOUT EXTENSION.
REM ***
for /f %%F in ("%CFP_FILE%") do set CFP_FILE_NO_EXT=%%~nF

REM ***
REM *** COPY THE FILE.
REM ***
copy /y %CFP_FILE% "%CFP_DIR%\%CFP_FILE_NO_EXT%.%CFP_INDEX:~-3%

goto END


:END
share|improve this answer
    
This did the job, great. If I can ask for this modification as I dont know how to do it myself. How can I change it so it dont matter what the source file name is, as the files in different folders are different names? Also the files end in .001 for example and not the file type that they are originally. Just asking if it is possible if not not a problem as this save alot of time thanks. –  Jesus Landra Aug 31 '13 at 20:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.