help with using substr

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dragovian
Posts: 17
Joined: 30 Jul 2009 09:43
Location: Belgium - Leuven
Contact:

help with using substr

#1 Post by dragovian » 02 Aug 2009 07:50

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]

dragovian
Posts: 17
Joined: 30 Jul 2009 09:43
Location: Belgium - Leuven
Contact:

#2 Post by dragovian » 02 Aug 2009 08:24

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

dragovian
Posts: 17
Joined: 30 Jul 2009 09:43
Location: Belgium - Leuven
Contact:

#3 Post by dragovian » 02 Aug 2009 10:12

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

Billy885
Posts: 5
Joined: 01 Aug 2009 12:22

#4 Post by Billy885 » 03 Aug 2009 00:51

Try these:

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

then use something like this inside the for command:

set /A count=!count!+1

dragovian
Posts: 17
Joined: 30 Jul 2009 09:43
Location: Belgium - Leuven
Contact:

#5 Post by dragovian » 03 Aug 2009 05:00

nope :( still gives only 1 :'(

dragovian
Posts: 17
Joined: 30 Jul 2009 09:43
Location: Belgium - Leuven
Contact:

#6 Post by dragovian » 03 Aug 2009 07:38

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#7 Post by avery_larry » 03 Aug 2009 13:48

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#8 Post by avery_larry » 03 Aug 2009 13:55

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#9 Post by DosItHelp » 10 Aug 2009 22:00

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?

Post Reply