[Ri...] Posted Wednesday at 12:01 PM Share Posted Wednesday at 12:01 PM 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. 1 Link to comment Share on other sites More sharing options...
[DW...] Posted Wednesday at 01:28 PM Share Posted Wednesday at 01:28 PM 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 More sharing options...
[Ma...] Posted Wednesday at 03:15 PM Share Posted Wednesday at 03:15 PM (edited) I should correct that - it will create two folders in same directory - not a subfolder. Script will do this: kill EXCEL if running read file "setID.txt" in same directory as script ( or from actual directory ) for each line in file from step2 will perform this: read content of the line into variable "%%a" change actual directory to defined "_target" make new directory with name stored in "%%a" print content of "%%a" after step3 cycle is this cycle: read content of directory "_source" of files with extension ".xls" for FIRST entry from step5 it will perform move that file into directory "_target" 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 Wednesday at 03:19 PM Link to comment Share on other sites More sharing options...
[DW...] Posted Wednesday at 04:30 PM Share Posted Wednesday at 04:30 PM 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 More sharing options...
[Ri...] Posted Wednesday at 05:03 PM Author Share Posted Wednesday at 05:03 PM 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 More sharing options...
[Ma...] Posted Wednesday at 05:09 PM Share Posted Wednesday at 05:09 PM Your script is not returning to initial directory - it stays in last created directory ( last "cd" command ) Link to comment Share on other sites More sharing options...
[Ri...] Posted Wednesday at 06:25 PM Author Share Posted Wednesday at 06:25 PM 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 More sharing options...
[Ma...] Posted yesterday at 04:38 AM Share Posted yesterday at 04:38 AM 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. 1 Link to comment Share on other sites More sharing options...
[Ri...] Posted yesterday at 11:02 AM Author Share Posted yesterday at 11:02 AM 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 More sharing options...
[Ma...] Posted yesterday at 11:12 AM Share Posted yesterday at 11:12 AM 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 ..." ) 1 Link to comment Share on other sites More sharing options...
[Ri...] Posted 19 hours ago Author Share Posted 19 hours ago (edited) 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 19 hours ago 1 Link to comment Share on other sites More sharing options...
[Je...] Posted 14 hours ago Share Posted 14 hours ago (edited) 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 14 hours ago 1 Link to comment Share on other sites More sharing options...
[Je...] Posted 13 hours ago Share Posted 13 hours ago 1 Link to comment Share on other sites More sharing options...
[Ri...] Posted 57 minutes ago Author Share Posted 57 minutes ago (edited) I would like to use python, VBS or similar, but I have IT. 😑 I can't use certain languages, but I can use VB.net and create an app. Wierd... Edited 57 minutes ago Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in