I have 2 share folders

\testing\test1 which contain sub folders sub1,sub2,sub3,sub4,sub5
and
\testing\test2 which contain sub folders sub2,sub3
Need to know how to create a batch file on notepad to verify existance of sub folders from test1 and create them on test2 only if they do not exist in test 2

Dani AI

Generated

A compact, safe batch-only approach that complements the suggestions by and : it enumerates every directory under \testing\test1, computes the relative path, and creates the same directory under \testing\test2 only if it does not already exist. The script defaults to a dry run so the planned actions can be reviewed before anything is created.

@echo off
setlocal enabledelayedexpansion

rem Adjust these to match the actual shares
set "SRC=\\testing\test1"
set "DST=\\testing\test2"

rem Ensure both end with a backslash for predictable substitution
if not "%SRC:~-1%"=="\" set "SRC=%SRC%\"
if not "%DST:~-1%"=="\" set "DST=%DST%\"

rem Dry run: 1 = show what would be done, 0 = actually create folders
set "DRYRUN=1"

for /f "delims=" %%D in ('dir /ad /b /s "%SRC%" 2^>nul') do (
  set "FULL=%%D"
  set "REL=!FULL:%SRC%=!"
  if not "!REL!"=="" (
    set "TARGET=%DST%!REL!"
    if not exist "!TARGET!" (
      if "%DRYRUN%"=="1" (
        echo Would create: "!TARGET!"
      ) else (
        md "!TARGET!" 2>nul && echo Created: "!TARGET!" || echo Failed: "!TARGET!"
      )
    )
  )
)

endlocal

Notes and troubleshooting:

  • DRYRUN=1 is the safe default; change to 0 to perform actual creation after verifying the output.
  • The script uses UNC paths and quoted variables so spaces are handled. When running as a scheduled task, ensure the task runs under an account with the required network permissions (mapped drives may not be available to some service accounts).
  • If paths are very deep, Windows path-length limits can be encountered; keep that in mind for very long nested structures.
  • For large trees or for copying additional attributes, the tools mentioned earlier by and remain valid alternatives; this batch is focused on a simple, transparent "verify-and-create" workflow and on giving a clear dry-run before changes.

Recommended Answers

All 2 Replies

xcopy with /t and /e options will do this. Type xcopy /? for details.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.