I want my script to compare only the first part of the directory names, (up to the space), in the top level directories of a specified source directory, with the first part of the directory names of all subdirectories of another big directory, (about 400gb), excluding the source directory itself.
It should log the names of the source directories to a file, found_folders.log, within the same directory as the batch script.
In short, what I'm looking for is something like this:
foreach "folder_in_source"
source_sub_folder_name
foreach "sub_folder_in_parent"
foreach "sub_sub_folder_in_parent" (it should loop in all the sub_folders_in parent excluding the source)
if source_sub_folder_name == sub_sub_folder_in_parent (only the first part of the name up to the space)
save in log
I've tried this unsuccessfully:
@echo off
setlocal enabledelayedexpansion
rem Check if source folder argument is provided
if "%~1"=="" (
echo Usage: %0 [source_folder]
exit /b 1
)
set "source_folder=%~1"
set "log_file=%~dp0found_folders.log"
rem Check if source folder exists
if not exist "%source_folder%" (
echo Source folder does not exist.
exit /b 1
)
rem Loop through parent folders
for /d %%F in ("%source_folder%\..") do (
rem Exclude the source folder itself
if not "%%~nxF"=="%~nx1" (
rem Loop through subfolders of the parent folder
for /d %%P in ("%%~F\*") do (
rem Get the first part of the parent subfolder name (up to the space)
for /f "tokens=1,* delims= " %%a in ("%%~nxP") do set "parent_subfolder_name=%%a"
rem Loop through first level folders in the source folder
for /d %%S in ("%source_folder%\*") do (
rem Get the first part of the source folder name (up to the space)
for /f "tokens=1,* delims= " %%b in ("%%~nxS") do set "source_folder_name=%%b"
rem Check if the source folder name matches the parent subfolder name
if /i "!source_folder_name!"=="!parent_subfolder_name!" (
rem Log the matching source folder name
echo Found matching folder: %%~nxS >> "%log_file%"
)
)
)
)
)
echo Done. Please check "%log_file%" for results.
It keeps comparing the source's subdirectory with only the topmost parent name.