0

I am writing simple script to check if somefolder exists in available drives. however %%G always shows empty string when concat with ":\somefolder" if I just echo %%G it shows all the available drives. I am new to batch scripting , not sure what am I missing here.

Thanks in advance.

@echo off 
   ::parse the VER command 
   FOR /F "tokens=*" %%G IN ('wmic logicaldisk get caption') DO (
    IF [%%G]==[] (
                    echo "empty string"
    ) ELSE (

        SET var="%%G\somefolderpath"
        IF EXIST %var% (
            echo %var% found
        ) ELSE (
            echo %var% not found
        )
     )
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Abhay
  • 564
  • 6
  • 14

3 Answers3

0

It's normal practice to have a setlocal command directly after the initial @echo off. This discards any changes made to the environment when the batch ends, so variables established by one batch file do not affect any further batches that may be run in the same session.

Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion (outside of the block or more usually after the initial @echo off) and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.

In your case, setting var is not required - if exist "%%G\somefolderpath" would suffice.

Note also that assigning quoted strings to variables make the variables hard to combine logically. Inserting quotes as needed is far simpler. The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

You can't set a variable and read it's contents in the loop unless you use delayed expansion or do a call echo.

Also, note that [%%G] will never = [] only a very specific scenario where you impropperly set up your For loop tokens so rhat they have ranges that overlap can create empty variolables.

In your loop it is not possible as you just select alll token, perfectly valid. For loops do not match lines that have no characters other than those that are considered delims (whitespace by default).

In any case,we can safely drop that portion.

Lets show some examples of how you might do this instead

To check directly:

 @echo off 
 Set "_Dir=Some\Folder\Path"
 FOR /F "tokens=*" %%G IN ('
   wmic logicaldisk get caption
 ') DO (
    IF EXIST "%%G\%_Dir%" (
       ECHO."%%G\%_Dir%" Exists!
    ) ELSE (
       ECHO."%%G\%_Dir%" Does Not Exist!
    )
 )

Set that varable and use it in the loop:

 @(
   SetLocal EnableDelayedExpansion
   echo off
 )
 Set "_Dir=Some\Folder\Path"
 FOR /F "tokens=*" %%G IN ('
   wmic logicaldisk get caption
 ') DO (
    SET "_Path=%%G\%_Dir%"
    IF EXIST "!_Path!" (
       ECHO."!_Path!" Exists!
    ) ELSE (
       ECHO."!_Path!" Does Not Exist!
    )
 )

Notice that in order to use rhe variable in the loop we need to use ! instead of %

Ben Personick
  • 3,074
  • 1
  • 22
  • 29
  • When checking for the existence of a directory, I would recommend that you use a trailing back slash, i.e. `IF EXIST "%%G\%_Dir%\"` and `IF EXIST "!_Path!\"`. – Compo Jul 01 '19 at 15:43
0

Here is another way to do it. The WMIC command is deprecated. https://itknowledgeexchange.techtarget.com/powershell/wmic-deprecated/

As others have mentioned, setting and using a variable inside a FOR loop requires ENABLEDELAYED EXPANSION or CALL.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "delims=" %%G IN ('powershell -NoLogo -NoProfile -Command ^
        "(Get-CIMInstance -ClassName Win32_LogicalDisk).DeviceID"') DO (
    SET "VAR=%%G\somefolderpath"
    IF EXIST "!VAR!" (
        echo "!VAR!" found
    ) ELSE (
        echo "!VAR!" not found
    )
)
lit
  • 14,456
  • 10
  • 65
  • 119