biz00ka
biz00ka

Reputation: 25

if else block is not working properly in batch file

@echo off
set /p filename=please enter name of file..:

echo "%filename%"

(
 echo %filename% 
 if exist { C:\Windows\System32\%filename% }
 (
 wmic datafile where name="C:\\Windows\\System32\\%filename%" get version 
 wmic datafile where name="C:\\Windows\\System32\\%filename%" get path
 )
else
 (
 wmic datafile where name="C:\\Windows\\System32\\drivers\\%filename%" get version
 wmic datafile where name="C:\\Windows\\System32\\drivers\\%filename%" get path
 )



) >output.txt

start notepad.exe output.txt

I have made this batch file to search files,name the files,get the version of files and print the path of the existing file in a text file.but if else block is not working properly. Help me and thanks in advance. :-)

Upvotes: 0

Views: 4335

Answers (1)

foxidrive
foxidrive

Reputation: 41234

A) Set /p has quotes to allow long filenames, as good practice.
B) if exist line has quotes for the same reason, plus the parenthesis is on the end of the line
C) ) else ( must be on one line

@echo off
set /p "filename=please enter name of file..: "

echo "%filename%"

(
 echo %filename% 
 if exist "C:\Windows\System32\%filename%" (
      echo block 1
   wmic datafile where name="C:\\Windows\\System32\\%filename%" get version 
   wmic datafile where name="C:\\Windows\\System32\\%filename%" get path
 ) else (
      echo block 2
   wmic datafile where name="C:\\Windows\\System32\\drivers\\%filename%" get version
   wmic datafile where name="C:\\Windows\\System32\\drivers\\%filename%" get path
 )

) >output.txt

start notepad.exe output.txt

Upvotes: 5

Related Questions