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 !
Copy each folder from 2020 named (NAME) to its mirror location in 2021
Moderator: DosItHelp
Re: Copy each folder from 2020 named (NAME) to its mirror location in 2021
Give that a go.
Steffen
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
)
)
Re: Copy each folder from 2020 named (NAME) to its mirror location in 2021
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
I still have to adapt the code so that it also runs through the subfolders.
Have a great day Steffen !
Chris
Re: Copy each folder from 2020 named (NAME) to its mirror location in 2021
Hello Steffen, god among gods,
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!
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!
Re: Copy each folder from 2020 named (NAME) to its mirror location in 2021
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:
Steffen
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
Re: Copy each folder from 2020 named (NAME) to its mirror location in 2021
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.
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.