1

I am trying to find drive letters and create one tmp.txt file in each drive if i able to create file it should print file created else not. Below what i did but didnt work as expected.

del volumes.txt
del test.log
mountvol | findstr :\ >> volumes.txt
for /F "delims= " %%b in (volumes.txt) do (
  for /f  "usebackq tokens=* delims=" %%a in (`fsutil file createnew %%btmp.txt 1 2^>^&1`) do (
    for /f "tokens=4 delims= " %%# in ("%%a") do set "result=%%~#" (
      if %result% equ "created" (
        echo File creted >> test.log
      ) else (
        echo Failed to create >> test.log
      )
    )
  )
)

it shows Failed to create for all drives in test.log even if tmp.txt created in drives

anoop
  • 3,229
  • 23
  • 35
Dipak
  • 672
  • 3
  • 9
  • 26

1 Answers1

1

Change

for /f "tokens=4 delims= " %%# in ("%%a") do set "result=%%~#" (
  if %result% equ "created" (

to

for /f "tokens=4 delims= " %%# in ("%%a") do (
  if "%%~#" equ "created" (

There is no apparent reason for assigning the value to result and even then, you'd need to invoke delayedexpansion and use !result! instead of %result% (see endless So items about delayedexpansion.

Also, as a matter of style, whereas %%# appears to work, only (case-sensitive) alphabetics are supported by the documentation, and there's always a possibility that Microsoft may "fix" the "problem".

Magoo
  • 77,302
  • 8
  • 62
  • 84