Page 1 of 1

help with using substr

Posted: 02 Aug 2009 07:50
by dragovian
hey, i'm trying to "automate" the defrag command and i'm trying to make a choice menu, but i need to get the drives right.

so i tried to get a substr from the "fsutil fsinfo drives" command, but i just can't succeed :s.

i'm using the set mysubstr=%VARIABLE:~8% but it doesn't work ... :(

it keeps echoing ~8

Code: Select all

@echo off
set VARIABLE=%fsutil fsinfo drives%
set mysubstr=%VARIABLE:~8%
echo %mysubstr%
echo %VARIABLE%
pause


when i take away the %% from the second line and make it

Code: Select all

set VARIABLE=fsutil fsinfo drive


it does make the right echo :( so it has something to do with using the % ... % to get an input :/ [/code]

Posted: 02 Aug 2009 08:24
by dragovian
^^ yay ^^ !

i managed to do the substring, now all i need to do is use a for to split all the different drives and put em in a variable ^^ and use echo commands to make a choice menu :D

here's the code i used

Code: Select all

@echo off
fsutil fsinfo drives >> temp.txt
for /f "delims=" %%A in ('findstr /b "Drives" temp.txt') do set YourVar=%%A
set mysubstr=%YourVar:~8%
echo %YourVar%
echo %mysubstr%
del temp.txt
pause

Posted: 02 Aug 2009 10:12
by dragovian
ok guys, so here's the code, but i can't figure out how to do the count :/ ?

Code: Select all

@echo off
fsutil fsinfo drives >> temp.txt
for /f "delims=" %%A in ('findstr /b "Drives" temp.txt') do set YourVar=%%A
set mysubstr=%YourVar:~8%
echo %YourVar%
echo %mysubstr%
del temp.txt
set number=1
for %%A in (%mysubstr%) do (
set /a number=number+1
echo %number% - %%A
)


for some reason the "set /a number=number+1" doesn't work inside the for command.

if i just do this in cmd it works, but i want to do it automated :/ ( using for loop etc ... )

anyone got any idea as to why this keeps giving

1 - C:\
1 - D:\
... ?

i want it to count up

1 - C:\
2 - D:\
...

so i can make a choicemenu :).

cheers

peter

Posted: 03 Aug 2009 00:51
by Billy885
Try these:

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

then use something like this inside the for command:

set /A count=!count!+1

Posted: 03 Aug 2009 05:00
by dragovian
nope :( still gives only 1 :'(

Posted: 03 Aug 2009 07:38
by dragovian
i figured it out ;D

i made two batch files

Main batch file

Code: Select all

@echo off
fsutil fsinfo drives >> temp.txt
for /f "delims=" %%A in ('findstr /b "Drives" temp.txt') do set YourVar=%%A
set mysubstr=%YourVar:~8%
echo %YourVar%
echo %mysubstr%
del temp.txt
set /a count=1
for %%A in (%mysubstr%) do (
call countIncrement.bat %count% %%A
)
pause


secondary batch file

Code: Select all

@echo off
echo %count% - %2
set increment = 1
set /a increment=increment+1
set /a count=%1%+increment

Posted: 03 Aug 2009 13:48
by avery_larry
Billy885 wrote:Try these:

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

then use something like this inside the for command:

set /A count=!count!+1

Right idea -- close on the implementation. The delayedexpansion is needed on the echo, not the set command:

set /a number+=1

echo !number! - %%A

Oh -- and you don't need setlocal enableextensions, just the setlocal enabledelayedexpansion

Posted: 03 Aug 2009 13:55
by avery_larry
dragovian wrote:i figured it out ;D

i made two batch files

Main batch file

Code: Select all

@echo off
fsutil fsinfo drives >> temp.txt
for /f "delims=" %%A in ('findstr /b "Drives" temp.txt') do set YourVar=%%A
set mysubstr=%YourVar:~8%
echo %YourVar%
echo %mysubstr%
del temp.txt
set /a count=1
for %%A in (%mysubstr%) do (
call countIncrement.bat %count% %%A
)
pause


secondary batch file

Code: Select all

@echo off
echo %count% - %2
set increment = 1
set /a increment=increment+1
set /a count=%1%+increment


You can put the 2nd batch file inside the 1st batch file and call it the same way, only using a label:

Code: Select all

@echo off 
fsutil fsinfo drives >> temp.txt
for /f "delims=" %%A in ('findstr /b "Drives" temp.txt') do set YourVar=%%A
set mysubstr=%YourVar:~8%
echo %YourVar%
echo %mysubstr%
del temp.txt
set /a count=1
for %%A in (%mysubstr%) do (
call :countIncrement %count% %%A
)
pause
goto :eof

:countIncrement
echo %count% - %2
set increment = 1
set /a increment=increment+1
set /a count=%1%+increment
goto :eof

but that doesn't seem to match up with your earlier post. Perhaps this:

Code: Select all

@echo off 
fsutil fsinfo drives >> temp.txt
for /f "delims=" %%A in ('findstr /b "Drives" temp.txt') do set YourVar=%%A
set mysubstr=%YourVar:~8%
echo %YourVar%
echo %mysubstr%
del temp.txt
set number=1
for %%A in (%mysubstr%) do call :process %%A
)
goto :eof

:process
set /a number+=1
echo %number% - %1
goto :eof

Posted: 10 Aug 2009 22:00
by DosItHelp
There is something fishy going on with the output of fsutil, see earlier post for details: http://www.dostips.com/forum/viewtopic.php?t=230

Here is a function to choose a drive:

Code: Select all

@echo off
call:DriveChoice dr
echo.Your choice: '%dr%'
goto:eof


:DriveChoice drive
SETLOCAL ENABLEDELAYEDEXPANSION
set /a n=0
for /f "tokens=*" %%a in ('fsutil fsinfo drives^|find /v ""') do (
    set "dr=%%a"
    set /a n+=1
    call set !n!=%%dr:~-3%%
    call echo. !n! - %%dr:~-3%%
)
set /p "num=Make a Choice: "
set "drv=!%num%!"
ENDLOCAL&set %~1=%drv%
goto:eof

myfsutil.cmd
1 - C:\
2 - E:\
3 - F:\
Make a Choice: 1
Your choice: 'C:\'



DosItHelp?