0

This batch script is supposed to detect removable drives, and loop though the following process for each drive: format, wrtie files to. Problem is that it only does this for one drive. For example, I can put two drives in, it will detect them both and process the loop on on of them two times. The issue has something to do with the variable of the drive name being updated, but I am not all that savy with batch script. If there is another answer already on this site, I wouldn't know what it looks like because I don't know everything about the commands (i have look at all of the suggestions given). I appologize if this is a repeat question. Just point me in the right direction and I will be on my way!

    @echo off
    setlocal enabledelayedexpansion
    for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
       for %%c in (%%b) do (
          echo Starting loop with %yyy%
          for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
             if %%d equ Removable (
                echo Drive written to is %yyy%
                format /y %yyy:~0,2% /fs:FAT32 /v:LCM2014 /q
             )
             set yyy=%%c
             echo Ending loop with %yyy%
          )
       )
    )
flockerman
  • 15
  • 2

1 Answers1

2

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 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.

You are obviously having a problem with yyy from your posted code. Try replacing %yyy% with !yyy! and see the difference.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • it doesn't change anything. I replaced all of the %yyy% with !yyy!. Still the same issue. Any thoughts? – flockerman Jun 16 '14 at 15:08
  • Did you also change `%yyy:~0,2%` to `!yyy:~0,2!`? The point is that any `%var%` is replaced by the contents of `var` at the time the command is parsed. `!var!` in delayedexpansion mode accesses te value as it changes - ie the run-time value. Beyond that, you'd need to show what is displayed by your echoes. – Magoo Jun 16 '14 at 15:14
  • Starting loop with F:\ Ending loop with F:\ Starting loop with F:\ Ending loop with F:\ Starting loop with F:\ Drive written to is F:\ The type of the file system is FAT32. QuickFormatting 7.6 GB Initializing the File Allocation Table (FAT)... Ending loop with F:\ Starting loop with F:\ Drive written to is F:\ The type of the file system is FAT32. QuickFormatting 7.6 GB Initializing the File Allocation Table (FAT)... Format complete. Ending loop with F:\ – flockerman Jun 16 '14 at 15:33
  • That is what it spits out. If I add the !yyy! in the middle and initialize it with setlocal enabledelayedexpansion echo !yyy! setlocal disabledelayedexpansion... it includes the drive before it (in this case the cd drive and one of the usb drives, while detecting all of them – flockerman Jun 16 '14 at 15:36
  • While you're experimenting, it might be a really good idea to `echo` the `format...` line. The fact that **every** mention of the value of `yyy` appears to be `F:\` means that `yyy` is not being changed. **note** that you are changing `yyy` **AFTER** the `if .. removable` block - this should be before. That probably explains the `drive before it` phenomenon. `setlocal enable...` .. `setlocal disable...` does **not** reverse the `local` environment, it creates a new copy of the environment. To terminate a local environment, you need to execute an `endlocal` command or reach physical end-of-file – Magoo Jun 16 '14 at 16:05
  • The fact that `yyy` isn't changing at all indicates that you may have not changed all `%yyy%` to `!yyy!`. The fact that it starts at `F:\` means that it was set in an earlier run and will remain set until it is explicitly deleted from the environment. (Quick-and-easy: close that `cmd` instance and start a new one.) – Magoo Jun 16 '14 at 16:08