Jump to content

Batch file help. Creating folders with sub folders.


---
 Share

Recommended Posts

I am expanding on a batch file form a user of this forum (Sorry I don't recall the name).

The user created "report_end.bat".

taskkill /im EXCEL.EXE

set _source="C:\Users\Public\Documents\Zeiss\CALYPSO 6.4\workarea\results"
set _target="V:\CMM Data Files\Excel Files\Shoe-Micura\"

FOR /F "delims=" %%I IN ('DIR %_source%\*.xls /A:-D /O:-D/T:C /B') DO MOVE %_source%\"%%I" %_target% & GOTO :EOF
exit

This is great for moving the Excel file created by Calypso.

Now I have created a text file with two strings, where I want to create two folders.

The first string would be a subfolder of a constant string. (See "set_target" above)

Adding this inside of the original batch file creates two folders in the "set_target" path.

for /f "delims=" %%a in (setRunID.txt) do (
cd /d %_target% 
md %%a
echo %%a
)

Then I have this.

taskkill /im EXCEL.EXE

set _source="C:\Users\Public\Documents\Zeiss\CALYPSO 6.4\workarea\results"
set _target="V:\CMM Data Files\Excel Files\Shoe-Micura\"

for /f "delims=" %%a in (setRunID.txt) do (
cd /d %_target% 
md %%a
echo %%a
)
FOR /F "delims=" %%I IN ('DIR %_source%\*.xls /A:-D /O:-D/T:C /B') DO MOVE %_source%\"%%I" %_target% & GOTO :EOF
exit

The file setRunID.txt contains two lines.

#123456789
00-569754

I want the folders created in "set_target" path to be:

#123456789
......00-569754 (make as a sub folder of #123456789)

Unfortunately, all I am currently getting is the creation of two new folders in the "set_target" path.

 

Any help is appreciated.

  • Thank you! 1
Link to comment
Share on other sites

Please sign in to view this quote.

Please sign in to view this username.

 Is this helpful? I didn't run it, but GPT says it should do what you are asking.

 

 

@echo off
REM --- Close Excel if running ---
taskkill /im EXCEL.EXE /f >nul 2>&1

REM --- Define paths (quotes only when used, not in variable values) ---
set "_source=C:\Users\Public\Documents\Zeiss\CALYPSO 6.4\workarea\results"
set "_target=V:\CMM Data Files\Excel Files\Shoe-Micura"

REM --- Read lines from setRunID.txt ---
setlocal enabledelayedexpansion
set "lineCount=0"
set "parentFolder="

for /f "usebackq delims=" %%a in ("setRunID.txt") do (
    set /a lineCount+=1

    if !lineCount! equ 1 (
        REM First line → create main folder inside target
        set "parentFolder=%%a"
        cd /d "%_target%"
        if not exist "!parentFolder!" md "!parentFolder!"
        echo Created main folder: !parentFolder!
    ) else if !lineCount! equ 2 (
        REM Second line → create subfolder inside the first folder
        if defined parentFolder (
            cd /d "%_target%\!parentFolder!"
            if not exist "%%a" md "%%a"
            echo Created subfolder: %%a inside !parentFolder!
        ) else (
            echo ERROR: No parent folder defined from first line.
        )
    )
)

REM --- Move the newest Excel file from source to the subfolder ---
if defined parentFolder (
    REM Find the newest .xls file
    for /f "delims=" %%I in ('DIR "%_source%\*.xls" /A:-D /O:-D /T:C /B') do (
        move "%_source%\%%I" "%_target%\!parentFolder!\%%a\" >nul
        echo Moved %%I to "%_target%\!parentFolder!\%%a\"
        goto :done
    )
) else (
    echo ERROR: Could not determine folder from setRunID.txt.
)

:done
echo Done.
exit /b

 

How It Works

1. Kills Excel (taskkill /im EXCEL.EXE /f) to avoid locked files.

2. Reads the two lines from setRunID.txt:

Line 1: Creates a folder under the target directory.

Line 2: Creates a subfolder inside the folder created from line 1.

3. Finds the newest .xls file in the source folder and moves it into that subfolder.

4. Exits cleanly.

 

 

Link to comment
Share on other sites

I should correct that - it will create two folders in same directory - not a subfolder.

Script will do this:

  1. kill EXCEL if running
  2. read file "setID.txt" in same directory as script ( or from actual directory )
  3. for each line in file from step2 will perform this:
    1. read content of the line into variable "%%a"
    2. change actual directory to defined "_target"
    3. make new directory with name stored in "%%a"
    4. print content of "%%a"
  4. after step3 cycle is this cycle:
  5. read content of directory "_source" of files with extension ".xls"
  6. for FIRST entry from step5 it will perform move that file into directory "_target"
  7. script ends - moved only one file from many

DIR parameters:

/A:-D -> not a directory
/O:-D -> sort by date and time descending
/T:C -> sort by time: creation
/B -> just filenames without and additional info

This script is tested on win11 pro and it's doing what i've described.

What can be different from author and me is that drive "V" can be network drive, which can act differently.

Edited
Link to comment
Share on other sites

Please sign in to view this username.

 I had a few more moments to mess with this, and created the files I needed to test this out. It works perfectly on a Windows 11 machine. The file setRunID.txt must be located in the same directory as the batch file, or you will need to hard code the location into the script. Here is the script -

 

@echo off
REM --- Close Excel if running ---
taskkill /im EXCEL.EXE /f >nul 2>&1

REM --- Define paths (quotes only when used, not in variable values) ---
set "_source=C:\PATH TO YOUR SOURCE"
set "_target=C:\PATH TO YOUR TARGET"

REM --- Read lines from setRunID.txt ---
setlocal enabledelayedexpansion
set "lineCount=0"
set "parentFolder="
set "subFolder="

for /f "usebackq delims=" %%a in ("setRunID.txt") do (
    set /a lineCount+=1

    if !lineCount! equ 1 (
        REM First line → create main folder inside target
        set "parentFolder=%%a"
        cd /d "%_target%"
        if not exist "!parentFolder!" md "!parentFolder!"
        echo Created main folder: !parentFolder!
    ) else if !lineCount! equ 2 (
        REM Second line → create subfolder inside the first folder
        set "subFolder=%%a"
        if defined parentFolder (
            cd /d "%_target%\!parentFolder!"
            if not exist "!subFolder!" md "!subFolder!"
            echo Created subfolder: !subFolder! inside !parentFolder!
        ) else (
            echo ERROR: No parent folder defined from first line.
        )
    )
)

REM --- Wait for 5 seconds to ensure folders are fully created ---
echo Waiting for 5 seconds to ensure folders are created...
timeout /t 5 /nobreak >nul

REM --- Verify if the target subfolder exists ---
if exist "%_target%\!parentFolder!\!subFolder!\" (
    REM --- Move the newest Excel file from source to the subfolder ---
    for /f "delims=" %%I in ('DIR "%_source%\*.xls" /A:-D /O:-D /T:C /B') do (
        move "%_source%\%%I" "%_target%\!parentFolder!\!subFolder!\" >nul
        echo Moved %%I to "%_target%\!parentFolder!\!subFolder!\"
        goto :done
    )
) else (
    echo ERROR: Target folder does not exist: "%_target%\!parentFolder!\!subFolder!\"
)

:done
echo Done.
exit /b

 

Link to comment
Share on other sites

I keep getting this error when running within the Calypso program: The system cannot find the file setRunID.txt.

The setRunID.txt file is in the program folder.

(cmd /k keeps the command window open for me to review the information).

Then I re-open the excel file and run the batch file without Calypso, and it works fine.

Its been about 25 years since I have worked with a complex batch file.

Link to comment
Share on other sites

martin, I am not quite following your advice.

I am using DWC's latest script.

It fails when called at the end of the Calypso run.

It works when I select (double click to run) the report_end.bat file after the run.

In my brain, probably not a place for you to be, nothing changes the execution when running from Calypso or selecting the file to run.

Link to comment
Share on other sites

Hi Richard,

well i didn't post any advice, just mere description what is script running.

So to summarize - your script is running ok when executed manually, but fails when it's running from Calypso end of inspection, right?
Also i am not quite sure if "report_end" is correct name - i am using "inspection_end" - this should be really at the end of everything ( i am waiting for qdas files )

If you want to see your current directory for debugging then in script use "echo %~dp0" - this will report current directory where is script looking for your "setId.txt" file.

If it's not correct directory, then you have to correct your path for that ID file.

  • Like! 1
Link to comment
Share on other sites

It seems all I had to do was move this line cd /d "%_target%".

It is now creating the folders and moving the excel file.

Thanks to all that helped!!

@echo off
REM --- Close Excel if running ---
taskkill /im EXCEL.EXE /f >nul 2>&1

REM --- Define paths (quotes only when used, not in variable values) ---
set "_source=C:\Users\Public\Documents\Zeiss\CALYPSO 6.4\workarea\results"
set "_target=V:\CMM Data Files\Excel Files\Sleeve-Micura\"
cd /d "%_target%"

REM --- Read lines from setRunID2.txt ---
setlocal enabledelayedexpansion
set "lineCount=0"
set "parentFolder="
set "subFolder="
set "progFolder="
set scriptFolder=%~dp0

for /f "usebackq delims=" %%a in ("%scriptFolder%\setRunID2.txt") do (
    set /a lineCount+=1

    if !lineCount! equ 1 (
        REM First line → create main folder inside target
        set "parentFolder=%%a"
        rem cd /d "%_target%"
        if not exist "!parentFolder!" md "!parentFolder!"
        echo Created main folder: !parentFolder!
    ) else if !lineCount! equ 2 (
        REM Second line → create subfolder inside the first folder
        set "subFolder=%%a"
        if defined parentFolder (
            cd /d "%_target%\!parentFolder!"
            if not exist "!subFolder!" md "!subFolder!"
            echo Created subfolder: !subFolder! inside !parentFolder!
        ) else (
            echo ERROR: No parent folder defined from first line.
        )
    )
)

REM --- Wait for 5 seconds to ensure folders are fully created ---
echo Waiting for 5 seconds to ensure folders are created...
timeout /t 5 /nobreak >nul

REM --- Verify if the target subfolder exists ---
if exist "%_target%\!parentFolder!\!subFolder!\" (
    REM --- Move the newest Excel file from source to the subfolder ---
    for /f "delims=" %%I in ('DIR "%_source%\*.xls" /A:-D /O:-D /T:C /B') do (
        move "%_source%\%%I" "%_target%\!parentFolder!\!subFolder!\" >nul
        echo Moved %%I to "%_target%\!parentFolder!\!subFolder!\"
        goto :done
    )
) else (
    echo ERROR: Target folder does not exist: "%_target%\!parentFolder!\!subFolder!\"
)

:done
echo Done.
exit /b
rem cmd /k

 

Link to comment
Share on other sites

I would say that removing "cd /d %_target%" is not a good. it will create directory elsewhere. But if you don't want to use "cd" then you have to define whole path do directory creation.

Example: "md V:\CMM Data Files\Excel Files\Sleeve-Micura\test1\test2" will create all directories which are not existing - so both test1 and test2 dirs will be created.

Thats why it's working at first sight, but you are creating parent folder elsewhere ( section of "rem cd /d ..." )

  • Like! 1
Link to comment
Share on other sites

I am still using "cd" for now, I will work on a better method later.

This is the latest version that works, less what seems like network latency causing an occasional issue.

I have added a script_log file to check results after each run.

@echo off
REM --- Initialize log file (overwrite) ---
set "scriptFolder=%~dp0"
echo Script started at %DATE% %TIME% > "%scriptFolder%\script_log.txt"

REM --- Close Excel if running ---
echo Closing Excel if running... >> "%scriptFolder%\script_log.txt"
taskkill /im EXCEL.EXE /f >nul 2>&1

REM --- Define paths ---
set "_source=C:\Users\Public\Documents\Zeiss\CALYPSO 6.4\workarea\results"
set "_target=V:\CMM Data Files\Excel Files\Shoe Sphere PrePlate-Micura\"

REM --- Check if target drive is available ---
if not exist "%_target%" (
    echo ERROR: Target path "%_target%" is not accessible. >> "%scriptFolder%\script_log.txt"
    exit /b 1
)
cd /d "%_target%"

REM --- Read lines from setRunID2.txt ---
setlocal enabledelayedexpansion
set "lineCount=0"
set "parentFolder="
set "subFolder="
set "progFolder="

REM --- Check if setRunID2.txt exists ---
if not exist "%scriptFolder%\setRunID2.txt" (
    echo ERROR: setRunID2.txt not found in %scriptFolder% >> "%scriptFolder%\script_log.txt"
    exit /b 1
)

for /f "usebackq delims=" %%a in ("%scriptFolder%\setRunID2.txt") do (
    set /a lineCount+=1

    if !lineCount! equ 1 (
        set "parentFolder=%%a"
        if not exist "!parentFolder!" md "!parentFolder!"
        echo Created main folder: !parentFolder! >> "%scriptFolder%\script_log.txt"
    ) else if !lineCount! equ 2 (
        set "subFolder=%%a"
        if defined parentFolder (
            cd /d "%_target%\!parentFolder!"
            if not exist "!subFolder!" md "!subFolder!"
            echo Created subfolder: !subFolder! inside !parentFolder! >> "%scriptFolder%\script_log.txt"
        ) else (
            echo ERROR: No parent folder defined from first line. >> "%scriptFolder%\script_log.txt"
        )
    ) else if !lineCount! equ 3 (
        set "progFolder=%%a"
        if defined subFolder (
            cd /d "%_target%\!parentFolder!\!subFolder!"
            if not exist "!progFolder!" md "!progFolder!"
            echo Created progFolder: !progFolder! inside !subFolder! >> "%scriptFolder%\script_log.txt"
        ) else (
            echo ERROR: No subfolder defined from second line. >> "%scriptFolder%\script_log.txt"
        )
    ) else (
        echo WARNING: Extra line ignored: %%a >> "%scriptFolder%\script_log.txt"
    )
)

REM --- Preserve folder names after endlocal ---
endlocal & set "parentFolder=%parentFolder%" & set "subFolder=%subFolder%" & set "progFolder=%progFolder%"

REM --- Wait for 5 seconds to ensure folders are fully created ---
echo Waiting for 5 seconds to ensure folders are created... >> "%scriptFolder%\script_log.txt"
timeout /t 5 /nobreak >nul

REM --- Verify if the target progFolder exists ---
if exist "%_target%\%parentFolder%\%subFolder%\%progFolder%\" (
    for /f "delims=" %%I in ('DIR "%_source%\*.xls" /A:-D /O:-D /T:C /B') do (
        move "%_source%\%%I" "%_target%\%parentFolder%\%subFolder%\%progFolder%\" >nul
        echo Moved %%I to "%_target%\%parentFolder%\%subFolder%\%progFolder%\" >> "%scriptFolder%\script_log.txt"
        goto :done
    )
) else (
    echo ERROR: Target folder does not exist: "%_target%\%parentFolder%\%subFolder%\%progFolder%\" >> "%scriptFolder%\script_log.txt"
)

:done
echo Done. >> "%scriptFolder%\script_log.txt"
exit /b
rem cmd /k

P.S.

If anyone wants to use/modify for your needs, feel free to do so.

Edited
  • Like! 1
Link to comment
Share on other sites

Nice progress, Richard.

This might fix the “two separate folders instead of nested” issue. It reads the first line as the parent and the second as the child, then builds the full nested path before moving the newest Excel file.
 

set "PARENT="
set "CHILD="
for /f "usebackq delims=" %%A in ("%~dp0setRunID.txt") do (
if not defined PARENT (set "PARENT=%%~A") else if not defined CHILD (set "CHILD=%%~A")
)

set "DEST=%_target%\%PARENT%"
if defined CHILD set "DEST=%DEST%\%CHILD%"
mkdir "%DEST%" 2>nul

for /f "delims=" %%I in ('dir "%_source%\*.xls" /a:-d /o:-d /tc /b 2^>nul') do (
move /y "%_source%\%%~I" "%DEST%\" >nul
goto :EOF
)

 

The reason it may fix the folder nesting problem is because the script now constructs one combined path before calling mkdir, rather than creating each folder independently with cd.

Sidenote, but I've been using Python more often lately for these types of tasks.

One I'm particularly happy with is an all-in-one script that moves the _chr.txt results from Calypso and then reformats it based on a specific template matched to a part number.

For example, "when measuring part 123-ABC take the data from the _chr.txt and based on the filename template_123-ABC and make an excel file that looks like that."

Edited
Link to comment
Share on other sites

 Share

×
×
  • Create New...