user3205316
user3205316

Reputation: 11

How to get name of particular file in a folder and assign to variable using batch script

I want to create a Windows batch file that take the file name with particular pattern (say test*.zip ) and assign it to variable. so that I can check/validate whether the file is exists in other location or not.

How to do this ?

I tried below code.. its executing as expected but its executing twice .

@echo on

call :sub "D:\temp\test*.zip"

if exist "D:\temp\Updates\%filename%" (set flag="true") else (set flag="false")

echo %flag%

:sub
set filename=%~nx1
GOTO :EOF

Upvotes: 1

Views: 75

Answers (1)

Magoo
Magoo

Reputation: 80033

Unlike many languages, batch has no concept of the end of a "procedure" - it simply continues execution line-by-line until it reaches the end-of-file. Consequently, you need to goto :eof after completing the mainline, otherwise execution will continue through the subroutine code. :EOF is a predefined label understood by CMD to mean end of file. The colon is required.

Upvotes: 1

Related Questions