.Bat File calling with other .bat file with paramater isue

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rajnishjc_27
Posts: 17
Joined: 16 Aug 2019 23:35

.Bat File calling with other .bat file with paramater isue

#1 Post by Rajnishjc_27 » 21 Aug 2019 00:21

Hi Friends,

I have Script below in one .bat file and am calling other .bat file which contained the function
but it is not working.

-------Bat1.bat
@echo off
setlocal enableExtensions disableDelayedExpansion
set file=D:\Rajnish_GTT\R1Test.txt

for /f "tokens=1 delims=, " %%a in (%file% ) do (
setlocal enabledelayedexpansion

SET "string1=%%a" & CALL D:\Rajnish_GTT\islength.bat result1 !string1! -----> Calling other .bat file with parameter.
if "!result1!" GTR "6" (
echo(field "%%~a" Not a 6 Digit Long)
)
)

---------------Bat2.bat

@echo off
setlocal enableExtensions disableDelayedExpansion

(
SET S="S=!%~2!#"
SET "LEN=0"
FOR %%P IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) DO (
IF "!S:~%%P,1!" NEQ "" (
SET "S=!S:~%%P!"
)
)
)
(
ENDLOCAL
SET "%~1=%LEN%"
EXIT /B
)

Example data as below.
id
001 --- return 4 digit.
A002 --- return 6 digit.

Second .bat file checking the length of fields.

Kindly help.

SIMMS7400
Posts: 443
Joined: 07 Jan 2016 07:47

Re: .Bat File calling with other .bat file with paramater isue

#2 Post by SIMMS7400 » 22 Aug 2019 03:05

I see you copied in the string length function wrong. Review your other thread and try again. You're missing an entire parameter.

jeb
Expert
Posts: 914
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: .Bat File calling with other .bat file with paramater isue

#3 Post by jeb » 22 Aug 2019 13:25

Hi ,

your strlen batch is wrong, you doubled S="S=

Code: Select all

SET S="S=!%~2!#"
The next problem is, that your parameters are wrong when calling the batch.

Code: Select all

CALL ...\islength.bat result1 !string1!
The !string1! results into the expansion to the content, but the batch file expects only a variable name, not the content itself.

You have to call it

Code: Select all

CALL ...\islength.bat result1 string1
There is a good reason for the use of variable names instead of the content.
It's impossible to use CALL with an arbitrary content. The CALL command modifies the content.

Btw. I build a quite better version of the strLen function, this version can handle strings up to the maximum possible length of 8191 characters

Code: Select all

(   
    setlocal EnableDelayedExpansion
    (set^ tmp=!%~2!)
	if defined tmp (
		set "len=1"
		for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
			if "!tmp:~%%P,1!" NEQ "" ( 
				set /a "len+=%%P"
				set "tmp=!tmp:~%%P!"
			)
		)
	) ELSE (
		set len=0
	)
)
( 
    endlocal
    set "%~1=%len%"
    exit /b
)

Post Reply