Copy each folder from 2020 named (NAME) to its mirror location in 2021

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
molosev
Posts: 4
Joined: 22 Jan 2021 03:23

Copy each folder from 2020 named (NAME) to its mirror location in 2021

#1 Post by molosev » 22 Jan 2021 04:00

Hello,

Let me explain what I am trying to do:
I want to copy each folder called "Reference_asset_folder" that is present in folder "2020" to its mirror location in folder "2021". (the folder 2021 already exists before the script is executed)

---------------------------------------------------------------------
Original tree structure:

2020
|_Address 1
---|_Reference_asset_folder
------|_Content_file
---|_Project 01
---|_Project 02
|_Address 2
---|_Reference_asset_folder
------|_Content_file
---|_Project 01
---|_Project 02
|_Address 3
---|_Project 01
---|_Project 02


Status of the 2021 folder after execution of the script:

2021
|_Address 1
---|_Reference_asset_folder
------|_Content_file
|_Address 2
---|_Reference_asset_folder
------|_Content_file

Note that nothing has been copied for Address 3 because it does not include a "Reference_asset_folder" folder in 2020.

---------------------------------------------------------------------
Does it seem possible to do this using a batch?
Could you provide me with some first steps so that I can try to develop this script?

Thank you very much in advance !

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copy each folder from 2020 named (NAME) to its mirror location in 2021

#2 Post by aGerman » 22 Jan 2021 11:50

Give that a go.

Code: Select all

@echo off
for /d %%i in ("2020\*") do (
  if exist "%%i\Reference_asset_folder\" (
    robocopy "%%i\Reference_asset_folder" "2021\%%~nxi\Reference_asset_folder" /s /e
  )
)
Steffen

molosev
Posts: 4
Joined: 22 Jan 2021 03:23

Re: Copy each folder from 2020 named (NAME) to its mirror location in 2021

#3 Post by molosev » 25 Jan 2021 05:58

Wow that's powerful !

I still have to adapt the code so that it also runs through the subfolders.

Have a great day Steffen !

Chris

molosev
Posts: 4
Joined: 22 Jan 2021 03:23

Re: Copy each folder from 2020 named (NAME) to its mirror location in 2021

#4 Post by molosev » 12 Sep 2022 02:53

Hello Steffen, god among gods, :D

I have not been able to elaborate further... I must misunderstand the use of the parameters.
Can you help me get this tree structure a little deeper, i.e. the code goes through the whole of 2020 before copying the necessary into 2021?

From:

2020
|_Address 1
---|_Reference_asset_folder
------|_Content_file
---|_Project 01
------|_SubProject 01
---------|_Reference_asset_folder
------------|_Content_file
---|_Project 02
|_Address 2
---|_Reference_asset_folder
------|_Content_file
---|_Project 01
---|_Project 02
|_Address 3
---|_Project 01
---|_Project 02

To:

2021
|_Address 1
---|_Reference_asset_folder
------|_Content_file
---|_Project 01
------|_SubProject 01
---------|_Reference_asset_folder
------------|_Content_file
|_Address 2
---|_Reference_asset_folder
------|_Content_file

Thank you Thank you!

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copy each folder from 2020 named (NAME) to its mirror location in 2021

#5 Post by aGerman » 12 Sep 2022 12:40

An unknown depth of subfolder levels to be searched is a completely different story. A simple FOR loop is not sufficient anymore. It needs recursion (that is, a subroutine calls itself for each found subfolder in a certain branch of the folder tree).
Something about like that might be working in this case:

Code: Select all

@echo off &setlocal
set "src=2020"
set "dst=2021"
set "find=Reference_asset_folder"

set "subpath="
for /d %%i in ("%src%\*") do call :searchRecursively "%%~nxi"
goto :eof


:searchRecursively
setlocal
set "subpath=%subpath%\%~1"
if /i "%~1"=="%find%" (
  robocopy "%src%%subpath%" "%dst%%subpath%" /s /e
) else (
  for /d %%i in ("%src%%subpath%\*") do call :searchRecursively "%%~nxi"
)
endlocal
exit /b
Steffen

molosev
Posts: 4
Joined: 22 Jan 2021 03:23

Re: Copy each folder from 2020 named (NAME) to its mirror location in 2021

#6 Post by molosev » 14 Sep 2022 08:53

It works like a charm and is super fast!
I only had to add the network paths to my folders, it's not a glorious contribution, but it's something ;)
Thanks a lot for your help.

Post Reply