0

I wrote this .BAT script, pulling snippets from all over stackoverflow about 2 years ago.

:BOF
@echo off
set upper=
set /p CDNUM=Enter NEW CD number:
set superpath=f:\prints\"CD Numbers"

for /f "skip=2 delims=" %%I in ('tree "\%CDNUM%"') do if not defined upper set "upper=%%~I"
set "upper=%upper:~3%"

ECHO.%upper% | FIND /I "CD">Nul && ( goto:Found ) || ( goto:notfind )

:Found
if exist %superpath%\17000s\%upper%\ goto ALLMADE

ECHO.%upper% | FIND /I "REV">Nul && ( goto:NEWREV ) || ( goto:norev )

:norev
md %superpath%\17000s\%upper%\"Rev -"\Cust
set ultrapath=%superpath%\17000s\%upper%\"Rev -"
copy %superpath%\START.dxf %ultrapath%
ren %ultrapath%\START.dxf "%upper% ASSEMBLY.dxf"
echo %upper% has been successfully created.
PAUSE
goto:eof
:ALLMADE
echo That CD number already exists!
PAUSE
goto:bof
:notfind
set upper="CD%upper%"
goto:Found
:NEWREV
echo Revision's not yet supported!
PAUSE
goto:bof

It prompts the user for a 'CD NUMBER', checks to make sure the folder does not already exist, creates it along with some sub directories, then copies and renames a .dxf file.

Currently the script can see if the folder already exists, returning an error message. And if only a number is given it adds a 'CD' to it, as well as making them all caps.

It's been so long since I put this together, that when I look at it I no longer remember how exactly some parts of the script work or what they do.

What I would like to add to the script is the following:

  • Return an error if the input is less than 5 numbers or does not begin with '17*', 'cd17*', or 'CD17*'.
  • If the input includes a 'REV' (ex. rev b), change the Rev directory accordingly. Add REV B to the filename of the .dxf (ex. CD17003 REV B ASSEMBLY)
  • Common sense error checking...

This script is amazing for what it does right now, I'm just trying to improve it. Any help is greatly appreciated.

malfy
  • 815
  • 1
  • 10
  • 13
  • Insert above `set /P "CDNUM=Enter NEW CD number: "` the line `set "CDNUM="` to always undefine `CDNUM` if defined already and after the user prompt first `if not defined CDNUM goto BOF` (user hit just RETURN or ENTER), second `set "CDNUM=%CDNUM:"=%"` (removes all `"`), third `if not defined CDNUM goto BOF` (used entered just one or more `"` by mistake), fourth `if /I "%CDNUM:~0,2%" == "CD" set "CDNUM=%CDNUM:~2%"` (removes `cd` or `CD` from beginning), fifth `if not defined CDNUM goto BOF` (user entered just `cd`), `if not "%CDNUM:~0,2%" == "17" goto BOF` (number doesn't begin with `17`), ... – Mofi Aug 26 '20 at 05:18
  • sixth `if "%CDNUM:~4,1%" == "" goto BOF` (number has less than five digits), seventh `if not "%CDNUM:~5,1%" == "" goto BOF` (number has more than five digits), eighth `for /F delims^=1234567890^ eol^= %%I in ("%CDNUM:~2,3%") do goto BOF` (number does not have three digits after `17`). That eight lines after user prompt would be for your first requirement. Well, your second requirement cause some lines to change, but that should be no problem for you after running in a [command prompt](https://www.howtogeek.com/235101/) `for /?` and `if /?` and `set /?` and reading each output help completely. – Mofi Aug 26 '20 at 05:26
  • `set superpath=f:\prints\"CD Numbers"` is not correct although working because of error detection and automatic correction by Windows command processor because of an entire folder/file name should be enclosed in `"` and not just parts of it. Correct would be `set "superpath=f:\prints\CD Numbers"` and using this environment variable for example with `if exist "%superpath%\17000s\%CDNUM%\" echo %CDNUM% is already used.& goto BOF` which checks if a folder with input CD number exists already in which case the user has to input the CD number once again. – Mofi Aug 26 '20 at 05:32
  • You can of course `echo` on all `if` conditions with `goto BOF` and information for the user like `if not "%CDNUM:~0,2%" == "17" echo CD number does not begin with 17.& goto BOF` or `if "%CDNUM:~4,1%" == "" echo CD number has less than five digits.& goto BOF`. That's up to you. See also [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) and [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564). – Mofi Aug 26 '20 at 05:35
  • what is the purpose for either `cd17*` `CD17*` or just `17*`? why not just standardize it to `CD17*` and add that as a prefix. For instance: `set /p "CDNUM=Enter NEW CD number:"` then `set CDNUM=CD17%CDNUM%` – Gerhard Aug 26 '20 at 06:41
  • I would even change the prompt to make it clear to the user, what they are expected to input: `set /p "CDNUM=Enter NEW CD number: CD17"` – Stephan Aug 26 '20 at 07:56

0 Answers0