1

I have a batchscript mybatch in which I try to store the first user argument in a variable called FILE

set FILE = %1 
if defined FILE (
  echo defined
  echo do something with %1
  ) else (
   echo not defined %1 
  )

If I execute my batch via mybatch test1 I get always not defined test1. Why is variable FILE not defined?

knowledge
  • 941
  • 1
  • 11
  • 26

2 Answers2

5

You have unwanted spaces in your variable assignment, so you have defined a variable with a space in the name that always has a value beginning with a space. Your IF statement is checking if a variable without a space exists.

See Declaring and using a variable in Windows batch file (.BAT)

I recommend your first line should be:

set "FILE=%~1"
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

You can try like this :

@echo off
set "FILE=%~1" 
if Exist "%FILE%" (
    echo.
    echo "%FILE%" Exist
    echo do something with "%FILE%"
  ) else (
   echo "%FILE%" is not defined 
)
Pause
Hackoo
  • 18,337
  • 3
  • 40
  • 70