1

I made a script that can copy certain folders, but I would like for it to also copy the directory folders where the folders were found

I know about Robocopy and Xcopy for my goal these script will not do the job because I would have to /XD many folders and I have to many folders to type in every folder to the script

I want to Copy or Move Folder 2

Here is my workstation

Tester.bat
Main Storage Folder
+ ---- Folder A
        + --- Folder 1A
              + --- Folder 2
                         |-- data.pgt
              + --- Folder 3
                         |-- data.pgt
+ ---- Folder B
        + --- Folder 1B
                + --- Folder 2
                         |-- data.pgt
                + --- Folder 3
                         |-- data.pgt

My Goal Results
Backup Storage Folder
+ ---- Folder A
        + --- Folder 1A
                + --- Folder 2
                         |-- data.pgt
+ ---- Folder B
        + --- Folder 1B
                + --- Folder 2
                         |-- data.pgt

Here is my script

for /f "tokens=*" %%G in ('dir /b /s /a:d "Grains"') do move "%%G" "Folder 2"

Right now I can only make the script MOVE the folders, I tested copy and it doesn't copy

When I run the script I get these results

@ECHO OFF
SETLOCAL


SET "sourcedir=%~dp0"
SET "destdir=%~dp0"

SET "tdnames="Main Storage Folder""
SET "leafnames="Folder 2""
SET "destsub="Backup Storage Folder""

FOR %%T IN (%destsub%) DO (
 FOR %%t IN (%tdnames%) DO (
  FOR %%s IN (%leafnames%) DO (
   FOR /f "delims=" %%b IN ('dir /b /s /ad "%sourcedir%\%%~t\%%~s" 2^>nul') DO (
    FOR %%c IN ("%%~dpb.") DO (
     MD "%destdir%\%%~T\%%~nxc\%%~nxb" 2>nul
     Xcopy "%%~b\*" "%destdir%\%%~T\%%~nxc\%%~nxb"
    )
   )
  )
 )
)
GOTO :EOF
My Goal Results
    Backup Storage Folder
            + --- Folder 1A
                    + --- Folder 2
                             |-- data.pgt
            + --- Folder 1B
                    + --- Folder 2
                             |-- data.pgt

As you can see Folder A and Folder B are missing
My actual Workstation inside Main Storage Folder I have over 60 different folders and inside those 60 folders I have at least 10 different folder, and inside each of those 10 folders I have repeated names for example Folder 2 so Folder 2 is found in each folder

I have tried 10 different ways to find a fix but nothing works, the closes I got was editing this line

FOR %%c IN ("%%~dpb..") DO (

I added an extra period, but now it skips Folders 1A and 1B and copies Folder A and B

Backup Storage Folder
    + ---- Folder A
                    + --- Folder 2
                             |-- data.pgt
    + ---- Folder B
                    + --- Folder 2
                             |-- data.pgt
JR Santos
  • 137
  • 1
  • 11
  • Please don't spam tags next time! Anyway, when you've got a definitive directory hierarchy depth you shouldn't use `dir /S` or `for /R`, rather should you nest `for /D` loops to grab the intended directories: `for /D %%J in ("%~dp0Food Network\*") do for /D %%I in ("%%~J\Grains") do if exist "%%~I\data.pgt" echo "%%~I\data.pgt"` – aschipfl Oct 11 '22 at 07:45
  • Thanks for the help but maybe my question is not clear, because your scripts appears to be focusing on `data.pgt` I need it to focus on the name of the folder – JR Santos Oct 11 '22 at 14:26
  • My code was not intended as a solution but just as an example for how to iterate a certain directory depth using `for /D`; it could be reduced to `for /D %%J in ("%~dp0Food Network\*") do if exist "%%~J\Grains\*" echo "%%~J\Grains"` to focus on the `Grains` sub-directories… – aschipfl Oct 11 '22 at 20:40

1 Answers1

1
@ECHO OFF
SETLOCAL
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"

:: Directory names to copy

SET "tdnames="Food Network""

:: Leafnames to copy

SET "leafnames="Grains""

:: Destination subdirectory

SET "destsub="All Grains""

:: %%T : Target directory
:: %%t, %%s : top and sub of source

FOR %%T IN (%destsub%) DO (
 FOR %%t IN (%tdnames%) DO (
  FOR %%s IN (%leafnames%) DO (
   FOR /f "delims=" %%b IN ('dir /b /s /ad "%sourcedir%\%%~t\%%~s" 2^>nul') DO (
    FOR %%c IN ("%%~dpb.") DO (
     ECHO MD "%destdir%\%%~T\%%~nxc\%%~nxb" 2>nul
     ECHO XCOPY "%%~b\*" "%destdir%\%%~T\%%~nxc\%%~nxb"
    )
   )
  )
 )
)

GOTO :EOF

Always verify against a test directory before applying to real data.

Please note the format of the strings assigned to the various variables. If you want to add additional directories that have data, then simply add the directory name within quotes to the space-separated list, so to add another directory to Food Network, use SET "tdnames="Food Network" "another directory""

From there, it's reasonaby simple. Each for processes a list of names, each quoted so %%~X will youeld the unquoted name.

The dir looks for directories names %%s below %%t from the source directory. If such does not exist, the 2>nul suppresses error messages (the caret escapes the > redirector, telling cmd that the > is part of the dir, not the for)

%%b is set to the full directoryname, so %%~nxb yields the leafname, and %%~nxc the parent name.

The required md and xcopy lines are simply echoed for verification. If all appears as expected, remove those echo statements to activate the copy...

====== Revision for extra subdirectory level(s) --------------

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"

:: Directory names to copy

SET "tdnames="Food Network""

:: Leafnames to copy

SET "leafnames="Grains" "Corn""
:: Destination subdirectory

SET "destsub="All Grains""

:: %%T : Target directory
:: %%t, %%s : top and sub of source

FOR %%T IN (%destsub%) DO (
 FOR %%t IN (%tdnames%) DO (
  FOR %%s IN (%leafnames%) DO (
   FOR /f "delims=" %%b IN ('dir /b /s /ad "%sourcedir%\%%~t\%%~s" 2^>nul') DO (
    SET "target=%%b"
    SET "target=!target:%sourcedir%\%%~t=!"
    ECHO MD "%destdir%\%%~T!target!" 2>nul
    ECHO XCOPY "%%~b\*" "%destdir%\%%~T!target!"
   )
  )
 )
)

GOTO :EOF

Always verify against a test directory before applying to real data.

This time, delayedexpansion is invoked to allow strings to be modified within code blocks Stephan's DELAYEDEXPANSION link

Note that leafnames includes corn for testing.

The variable target is set to the full directoryname of the grains directory, then the source directory and first child level is removed from that string, leaving the remaining direcory levels, which are then included in the md and xcopy commands to be executed.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/248830/discussion-on-answer-by-magoo-how-to-make-a-script-also-copy-a-directory-tree-wi). – Samuel Liew Oct 15 '22 at 15:10
  • @Magoo - how do I fix for the missing folder – JR Santos Dec 23 '22 at 00:00
  • You would need to explain clearly what directory is "missing". What is the name of the directory that did not get copied, and what was the name of the directory that you expected to be created? – Magoo Dec 23 '22 at 02:17
  • @Magoo - Thank you I figured it out - Original line `FOR %%c IN ("%%~dpb.") DO (` and now correction `FOR %%c IN ("%%~dpb..") DO (` - I added an extra period – JR Santos Dec 23 '22 at 04:50
  • Main Folder 1/Folder 2/Folder 3/Folder 4/data.pgt - Folder 2 would not copy - adding the extra period copies all folders needed - Took me a while to find it – JR Santos Dec 23 '22 at 04:54
  • @Magoo - Sorry looks like it's not working 100% correct – JR Santos Dec 23 '22 at 23:40
  • by adding the extra period it now skips folder 3 - I will update the question – JR Santos Dec 23 '22 at 23:49
  • also the second option gives me to choice File or Directory – JR Santos Dec 24 '22 at 00:38
  • After 10 revisions to the question, 10 weeks, changes to the naming scheme between `wheat`, etc, to a generic form and half the discussion now archived in "chat", The latest question revision appears to bear no relation to the original. How about asking the question again and keeping to one naming scheme - preferably the actual, not generics? – Magoo Dec 27 '22 at 22:55