1.

Solve : Batch File to Copy into Multiple Directories with Different Names?

Answer»

It seems like a batch file would be the best for what I need to do, but I'm certainly open to other suggestions.

I need to copy a file into multiple directories. The file needs to be copied into dir1/dirA/dirB Where every instance of dirA has a different name and every instance of dirB is named the same.
The batch file can be run from dir1
There are other directories in dir1 that don't need the file copied into them and they don't have a dirB

Are there lots and lots?

Is there a reason why you can't open a text editor and create a batch file like this?

Code: [Select]copy file.ext d:\dir1\dirAaa\dirB
copy file.ext d:\dir1\dirAab\dirB
copy file.ext d:\dir1\dirAac\dirB
etc
Thanks for the reply.
There are around 45 directories, it's not prohibitive, but I'd prefer not to type them each in. I'm also worried that someone might add or remove a directory and forget to update the batch file accordingly.
Your suggestion is definitely better than doing it manually each time though.You say that all the dirB subdirectories are identically named?

In that case, executed in the top folder, dir1, this command should find them all:

dir /s /b /ad "dirB"

Can you confirm that this is so?

It WORKS on my system

Code: [Select]D:\TVE>dir /b /s /ad "PIX"
D:\TVE\1605\PIX
D:\TVE\1705\PIX
D:\TVE\1805\PIX
D:\TVE\1905\PIX
D:\TVE\2005\PIX
D:\TVE\2105\PIX
D:\TVE\2205\PIX
If it does then something like this should do it

Code: [Select]for /f "delims==" %%D in ('dir /s /b /ad "dirB"') do copy "filename.ext" "%%D"


You're a GENIUS. I really appreciate your help.

There's a final snag keeping this from being The IMMACULATE Batch.
Some directories have multiple directories called dirB (I actually hadn't noticed that before). I only need the file copied into the dirB that is in C:\dir1\dirx It looks like the batch will copy the file into every dirB. This isn't a huge deal an extra file here and there shouldn't be a problem.

All of the dirB's that don't need the file are deeper in the directories. So it looks sort of like:
dir1\dirA\dirB
dir1\dirA\dirxyz\dirB
dir1\dirA\dirlmno\dirpdq\dirv\dirB

Is there a way to ignore the deeper directories?
so the path to the folder including the drive letter will only have 2 backslashes in it?

like this?

C:\dir1\dirx

More than that, and the folder should be ignored?




It'll have 3 backslashed. Thanks for checking. The path looks like this:
C:\BatchRunsHere\VariableName\dirBTry this

Code: [Select]@echo off
set subdirname="DirB"

REM get list of subfolders
dir /b /s /ad %subdirname% > %temp%\subdirs.txt

REM kill temp files if they already exist
if exist %temp%\passdirs.txt del %temp%\passdirs.txt
if exist %temp%\faildirs.txt del %temp%\faildirs.txt

REM separate path into tokens delimited by \
REM and screen out those with more than 3
REM write pass and fail RESULTS to temp files

for /f "tokens=1,2,3,4* delims=\" %%D in (%temp%\subdirs.txt) do (

if "%%H"=="" echo %%D\%%E\%%F\%%G >> %temp%\passdirs.txt

REM you don't really need this NEXT line
if not "%%H"=="" echo %%D\%%E\%%F\%%G\%%H >> %temp%\faildirs.txt

)

REM you can delete from here
REM ------------------------
echo.
echo results...
echo.
echo folder(s) found
type %temp%\subdirs.txt
echo.
echo folder(s) to use
type %temp%\passdirs.txt
echo.
echo folder(s) to ignore
type %temp%\faildirs.txt
echo.
REM ------------------------
REM to here when you are happy

for /f %%D in (%temp%\passdirs.txt) do copy "filename.ext" "%%D"

del %temp%\subdirs.txt>nul
del %temp%\passdirs.txt>nul
del %temp%\faildirs.txt>nul







Or try this one line version - no temp files

Code: [Select]for /f "tokens=1,2,3,4* delims=\" %%D in ('dir /b /s /ad "dirB"') do if "%%G"=="dirB" copy "filename.ext" "%%D\%%E\%%F\%%G"Thank you! that works so perfectly.



Discussion

No Comment Found