0

On my Windows machine, S drive, under the OF folder I have the following structure :

S://OF
├── FolderA
│   ��── FolderA1
│        └── FolderA2
│           └── FolderX
|                 └── myFolder  
│   
│             
├── FolderB
│   └── FolderB1
│        └── FolderB2
|              └── FolderX
|                    └── myFolder
│                
│             
├── FolderC
│   └── FolderC1
│        └── FolderC2
               └── FolderX
                     └── myFolder

I would like to copy all of the myFolder directories into a new directory (AH) on my R drive

R://AH
├── myFolder1
├── myFolder2   
├── myFolder3  

I have tried the following but with no success!

For /D %G In ("OF\*") Do @%__AppDir__%Robocopy.exe "%G\myFolder" ".Destination\%~nxG_myFolder">NUL
5
  • Why not PowerShell? Do you need to search the whole drive or just sepecific folders? Does (gci s:\ MyFolder -ad -s).FullName return the desired paths to the folders you want copied? Commented Aug 8, 2021 at 17:43
  • @KeithMiller, Nothing happening after running your command. Powershell just freezes and I have to close it!
    – Haribo
    Commented Aug 9, 2021 at 8:12
  • 1
    Try step-by-step. S:<enter> should change the PowerShell prompt to the S:\ drive. sl 0F<enter> should navigare to the 0F directory. Now try gci MyFolder -Directory -recurse<enter>. The folder name will need quotes if it contains spaces or special characters. Commented Aug 9, 2021 at 12:05
  • @KeithMiller, Nothing ! same as before. It freezes in the last step and does not show any results when I run gci MyFolder -Directory -recurse<enter>
    – Haribo
    Commented Aug 10, 2021 at 11:00
  • what about one level at a time -- just gci -directory; sl directory, gci -directroty , etc. Maybe a permissions issue.... Commented Aug 10, 2021 at 18:57

1 Answer 1

0

This should do it for you, it traverses the folder tree using dir, when it finds a match creates the folder.

If you want to copy over or move the contents as well, replace md with any of xcopy or copy or robocopy depending on what you want to do.

@echo off

setlocal enabledelayedexpansion
set "_pattern=MyFolder"

rem change this line to the fully qualified root of the tree where where your targets are:
set "_path=S drive path"

rem change this line to the fully qualified new location they will be copied to:
set "_newroot=R drive path"

rem loop through the tree where your the folders are:
for /f "delims="  %%i in ('dir "%_path%" /b /ad /s') DO (

  rem this is what you will compare to:
  @set "_foundpath=%%i"
  
  rem extract the last 'n' characters of the path found:
  call set _compareto=!_foundpath:~-8!

  rem if the folder pattern matches, perform the action:
  IF !_compareto!@ equ %_pattern%@ (
    set /a _cnt+=1
    rem  remove the echo in this line to make the directory:
    echo md %_newroot%\%_pattern%!_cnt!
  )
)
endlocal
exit /b
1
  • The answer assumes that there are no spaces in your folder names. If there are, place double quotes around the variables in the IF statement.
    – ilinkcs
    Commented Aug 11, 2021 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .