Soldato
- Joined
- 22 Aug 2005
- Posts
- 8,907
- Location
- Clydebank
Hi I'm working on something and at one point I have to lookup a table for a barcode, but I have to remove the last 2 digits from the barcode as the table doesn't hold the last 2 digits of info.
Problem is the barcode could be anything, as it's an automatically generated bc from a scanned image.
This function will chop both digits off and return either a possible lookup value or not of the bc is less than 2 chars.
Is this the accepted way to pass variables to and from a batch function? calling the function with the variable name and with the variable itself? i.e BARCODE %BARCODE% ? The valid variable only returns a number.
sample output
Problem is the barcode could be anything, as it's an automatically generated bc from a scanned image.
This function will chop both digits off and return either a possible lookup value or not of the bc is less than 2 chars.
Is this the accepted way to pass variables to and from a batch function? calling the function with the variable name and with the variable itself? i.e BARCODE %BARCODE% ? The valid variable only returns a number.
Code:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
SET BARCODE=EDDIE-MURPHY-600273\O
echo.
echo %BARCODE% & call :demanglebarcode BARCODE VALID %BARCODE%
ECHO [II] VALUE AFTER FUNCTION %BARCODE% %VALID%
SET BARCODE=EDDIE-MURPHY-600273
echo.
echo %BARCODE% & call :demanglebarcode BARCODE VALID %BARCODE%
ECHO [II] VALUE AFTER FUNCTION %BARCODE% %VALID%
SET BARCODE=EDD
echo.
echo %BARCODE% & call :demanglebarcode BARCODE VALID %BARCODE%
ECHO [II] VALUE AFTER FUNCTION %BARCODE% %VALID%
SET BARCODE=ED
echo.
echo %BARCODE% & call :demanglebarcode BARCODE VALID %BARCODE%
ECHO [II] VALUE AFTER FUNCTION %BARCODE% %VALID%
SET BARCODE=E
echo.
echo %BARCODE% & call :demanglebarcode BARCODE VALID %BARCODE%
ECHO [II] VALUE AFTER FUNCTION %BARCODE% %VALID%
goto:eof
:deMangleBarcode
:: 3 arguments expected,
:: %1 - Variable name to hold chopped barcode,e.g. BARCODE
:: %2 - Variable name to hold 1 for valid, 0 for invalid (can happpen if 2 or less digits get submitted e.g. ISVALID
:: %3 - Variable containing barcode to be checked for e.g. %BARCODE%
@echo off
SETLOCAL
SET MANGLED=%~3
rem REMOVE THE SLASH-O or SLASH-B FROM THE END OF THE BARCODE FOR LOOKUP AND CHECK THERE IS STILL A VALUE TO CHECK
SET LOOKUPVALUE=!MANGLED:~0,-2!
IF NOT "%LOOKUPVALUE%"=="" (
ECHO [II] BARCODE SUPPLIED %3
ECHO [II] AFTER CHOPPING %LOOKUPVALUE%
SET LOOKUPVALUE=%LOOKUPVALUE%
SET ISVALID=1
) ELSE (
ECHO [WW] Lookup input appears to be empty, barcode wasn't valid.
SET LOOKUPVALUE=""
SET ISVALID=0
)
(ENDLOCAL & REM -- RETURN VALUES
SET %~1=%LOOKUPVALUE%
SET %~2=%ISVALID%
)
GOTO:EOF
sample output
Code:
sample output :
EDDIE-MURPHY-600273\O
[II] BARCODE SUPPLIED EDDIE-MURPHY-600273\O
[II] AFTER CHOPPING EDDIE-MURPHY-600273
[II] VALUE AFTER FUNCTION EDDIE-MURPHY-600273 1
EDDIE-MURPHY-600273
[II] BARCODE SUPPLIED EDDIE-MURPHY-600273
[II] AFTER CHOPPING EDDIE-MURPHY-6002
[II] VALUE AFTER FUNCTION EDDIE-MURPHY-6002 1
EDD
[II] BARCODE SUPPLIED EDD
[II] AFTER CHOPPING E
[II] VALUE AFTER FUNCTION E 1
ED
[WW] Lookup input appears to be empty, barcode wasn't valid.
[II] VALUE AFTER FUNCTION "" 0
E
[WW] Lookup input appears to be empty, barcode wasn't valid.
[II] VALUE AFTER FUNCTION "" 0
Last edited: