Solved Need help condensing this script

Mobile computing solutions Mini itx pfse...
December 14, 2015 at 22:00:41
Specs: Windows XP, HexaCore AMD FX-6100, 1400 MHz (7 x 200) / 4gb DDR
I wonder if someone can assist with a problem I have with A script. ?

I have been using this in my batch file to find the CD rom mapped drive, but the following script does not work on Windows 7, farther down I found another script that works but has too much information I just want to get the CD-ROM DRIVE !!!


Script does not work on windows ABOVE XP
:: @@@@@@@@@@@@@@@@@@ START USER CD DETECT @@@@@@@@@@@@@@@@@@@
:: WE NEED TO KNOW THE CD ROM DRIVE SO WE CAN IGNORE IT ON DEFRAG
set Label=CD-ROM
set drive=0
for /F %%a in ('fsutil fsinfo drives ^| find "\" ^| find /i /v "Drives"') do fsutil fsinfo DriveType %%a | find /i "%Label%" > nul && (set drive=%%a& goto :Found)
echo No %Label% found
goto :Nocd
:Found
echo ----------------------------------------------------------
echo The %Label% is mounted on drive %drive%
echo ----------------------------------------------------------
echo.
:Nocd
:: @@@@@@@@@@@@@@@@@@@@ END USER CD DETECT @@@@@@@@@@@@@@@@@@@


I found this script online below but it is not much use as it is I just want the CD-ROM label in a VAR, and to be honest I am lost how to do this :(

@echo off
setlocal enabledelayedexpansion

rem wscript //NoLogo //B %~dp0system_profile.vbs

set textfile="%~dp0system_profile.txt"

(
echo Drive Information
echo -----------------
for /f "usebackq tokens=1*" %%a in (`fsutil fsinfo drives ^| find ":"`) do (
if /i "%%a" NEQ "Drives:" (
call set "drive=%%a"
) ELSE (
call set "drive=%%b"
)
for %%D in (!drive!) do (
for /f "usebackq delims=" %%A in (`fsutil fsinfo drivetype %%D`) do (
call set type=%%A
)
for /f "usebackq tokens=2*" %%R in (`subst 2^>nul ^| findstr /i /b "%%D\"`) do set type=%%D - SUBST : %%S
echo:
echo:!type!
if "!type:fixed=!" NEQ "!type!" (
echo:fsutil fsinfo volumeinfo %%D
fsutil fsinfo volumeinfo %%D
)
)
)
) >> %textfile%
pause

Regards, Michelle

If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://tinyurl.com/pnenqgb

message edited by Ortorea


See More: Need help condensing this script

Report •


#1
December 14, 2015 at 23:36:21
✔ Best Answer
Hello again my friend! this might help:

@echo off & setlocal
for /f "tokens=*" %%a in ('fsutil fsinfo drives') do (
call :bb %%a
)
goto :eof

:bb

shift
if "%1" equ "" goto :eof
rem echo testing: %1 for "fixed drive"
for /f %%z in ('fsutil fsinfo drivetype %1 ^| find /i "fixed"') do (
echo %%z
)
goto :bb
::======== end script
since there may be other "drives", such as usb sticks etc., this one finds ONLY the "fixed" drives. I know there are better solutions beyond fsutil, but "if it works..." This tested on win-7 but not else.


Report •

#2
December 14, 2015 at 23:39:36
I don't think it works from a non-elevated CMD. Consider:
wmic volume where "DriveType=3" get DriveLetter

EDIT: :: WE NEED TO KNOW THE CD ROM DRIVE SO WE CAN IGNORE IT ON DEFRAG
Actually, if you're rewriting a defrag script for Win7, just use:

defrag /c

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#3
December 15, 2015 at 00:01:25
Hi Razor and nbrane, Unfortunately wmic is not an option as it is not native on Windows XP which I and some friends still use as servers etc, I use an Elevated batch for this as it happens so prefer the fsutil approach, it is easy enough to check for elevation with errorlevel when doing something that needs it.

I will try this nbrane on the win7 and vista and xp and get back to you :)

Michelle xoxoxo

If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://tinyurl.com/pnenqgb


Report •

Related Solutions

#4
December 15, 2015 at 00:11:16
Hi Again,
Maybe I should have mentioned I am testing on a windows 7 64 bit Machine sadly the script failed :( does not work on XP SP3 either .

Michelle xoxoxo

If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://tinyurl.com/pnenqgb


Report •

#5
December 15, 2015 at 00:20:23
It's a custom batch file I wrote, but I had to implement avoidance for SSD drives for defragmenting it took me a few weeks to find a small tool to do this, ( small zip called IsSSD.zip @ https://www.autoitconsulting.com/si... ) but even that only works on windows 7, really trying to find generic ways that is cross platform.

Regarding the script above this is the page I found a write-up for it but I am not a member on there.

But yes Razor That code does work on Windows 7 so in theory I could do a version check and run that code on windows 7 and the old code on XP but it gets messy, it has to be drive type 5 btw for the CD-ROM

wmic volume where "DriveType=5" get DriveLetter

But works on win7


http://stackoverflow.com/questions/...


:o) Michelle

If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://tinyurl.com/pnenqgb

message edited by Ortorea


Report •

#6
December 15, 2015 at 01:11:58
Well, post-script: win-XP needed a boost from my previous post, due to char zeros that fsutil put into the output stream. I tried this and it worked ok on both win-7 and win-xp, but not tried else:

@echo off & setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('fsutil fsinfo drives^|find /i /v ""') do (
call :bb %%a
)
goto :eof

:bb
for /f %%z in ('fsutil fsinfo drivetype %1 ^| find /i "fixed"') do (
echo %%z
)
shift
if "%1" equ "" goto :eof

goto :bb
::==== end script
I know vbscript has access to drives via the filesystem object, but I assumed for the nonce that you are working with batch only. Win-8 and higher, and any 64-bit platforms are beyond my reach.


Report •

#7
December 15, 2015 at 01:59:12
Thanks nbrane unfortunately it only finds hard drives and not the cd-rom drive but the script works for the hard drives but ommits the drives I need, but thanks for trying :o)

Michelle

If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://tinyurl.com/pnenqgb


Report •

#8
December 16, 2015 at 02:47:54
I did some more research online I found this script, it is somewhat cumbersome because it takes a while to run but once the CD drives is found I can store it locally on the machine in a text file so the checking only needs to be done one time unless the text file goes missing.

http://ss64.org/viewtopic.php?id=327

I added one line to the bottom, as I only want the very first CD ROM DRIVE so if their are more it should ignore those if the text file has already been created.

if not exist my-cdrom.txt if "!type!" equ "CD-ROM Drive" set result=!drive!&&> my-cdrom.txt echo !result!

Here is the modified code I found with my extra line on the bottom.

:: begin

@echo off & setlocal enabledelayedexpansion
if exist my-cdrom.txt goto :found

set drive=
echo:
for /f "usebackq tokens=1*" %%a in (`fsutil fsinfo drives ^| find ":"`) do (
if /i "%%a" NEQ "Drives:" (
set "drive=%%a"
) ELSE (
set "drive=%%b"
)
set label=
for /f "usebackq tokens=1* delims=\- " %%A in (`fsutil fsinfo drivetype !drive!`) do (
for /f "usebackq tokens=5*" %%U in (`vol %%A 2^>nul ^| findstr "^.Volume.in.drive...is" ^| findstr /v "^.Volume.Serial"`) do set label=%%V
set type=%%B

)
set "label=!label! "
set type=!type:~0,-1!
set drive=!drive:\=!
for /f "usebackq tokens=2*" %%R in (`subst 2^>nul ^| findstr /i /b "!drive!"`) do set type=SUBST : %%S
echo:!drive! [!label:~0,32!] !type!
if not exist my-cdrom.txt if "!type!" equ "CD-ROM Drive" set result=!drive!&&> my-cdrom.txt echo !result!
)
echo:
:found
if exist my-cdrom.txt (echo CD-ROM DRIVE IS KNOWN) else (echo CD-ROM DRIVE IS STILL UNKNOWN)
pause

:: end

But does not seem to work on windows 7 :(

Michelle

If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://tinyurl.com/pnenqgb

message edited by Ortorea


Report •

#9
December 16, 2015 at 05:36:59
Hi nbrane and Razor2.3

I managed to figure out that on Windows 7 they use Driv instead of Drive as a label this was buggering up my find but now the script below works on both XP and Windows 7, still have to test on Vista etc.

ADDENDUM: Works on Vista too

:: start

@echo off & setlocal enabledelayedexpansion
:usercd
set mypath=%~dp0
if not exist %mypath%flags md %mypath%flags
:: @@@@@@@@@@@@@@@@@@ START USER CD DETECT @@@@@@@@@@@@@@@@@@@
:: WE NEED TO KNOW THE CD ROM DRIVE SO WE CAN IGNORE IT ON DEFRAG
:: AND SEARCH FOR SFC WINDOWS REPAIR FILES IF REQUIRED ...
if exist "%mypath%flags\my-cdrom.txt" goto :fndisk

set drive=
echo:
for /f "usebackq tokens=1*" %%a in (`fsutil fsinfo drives ^| find ":"`) do (
if /i "%%a" NEQ "Drives:" (
set "drive=%%a"
) ELSE (
set "drive=%%b"
)
set drive=!drive:\=!
for %%D in (!drive!) do (
set label=
for /f "usebackq tokens=1* delims=\- " %%A in (`fsutil fsinfo drivetype %%D`) do (
for /f "usebackq tokens=5*" %%U in (`vol %%A 2^>nul ^| findstr "^.Volume.in.drive...is" ^| findstr /v "^.Volume.Serial"`) do set label=%%V
set type=%%B
)
set "label=!label! "
set type=!type:~0,-1!
for /f "usebackq tokens=2*" %%R in (`subst 2^>nul ^| findstr /i /b "%%D"`) do set type=SUBST : %%S
echo:%%D [!label:~0,32!] !type!
if not exist "%mypath%flags\my-cdrom.txt" if "!type!" equ "CD-ROM Driv" set result=%%D && > "%mypath%flags\my-cdrom.txt" echo !result!
if not exist "%mypath%flags\my-cdrom.txt" if "!type!" equ "CD-ROM Drive" set result=%%D && > "%mypath%flags\my-cdrom.txt" echo !result!


)
)
echo:

:fndisk
echo ----------------------------------------------------------
set /p cddrive=< "%mypath%flags\my-cdrom.txt"
if "%cddrive%" equ "" goto :Nocd
if "%cddrive%" equ "ECHO is off." goto :error2
echo The CD-ROM is mounted on drive %cddrive%
echo ----------------------------------------------------------
if exist "%mypath%flags\my-cdrom.txt" (echo CD-ROM DRIVE APPEARS KNOWN ON Drive: %cddrive%) else (echo CD-ROM DRIVE IS STILL UNKNOWN)
echo.
echo CDROM FINDING TOOL
pause
goto :end
:Nocd
ECHO.
ECHO NO CD-DRIVE %cddrive%
pause
:: @@@@@@@@@@@@@@@@@@@@ END USER CD DETECT @@@@@@@@@@@@@@@@@@@
:error2
echo BAD RESULT NO DATA WAS ECHOED
echo.
ECHO NO CD-DRIVE %cddrive%
pause
goto :end

:end
:: FOR TESTING REASONS WILL DELETE THE STORED FLAG FILE
if exist "%mypath%flags\my-cdrom.txt" del "%mypath%flags\my-cdrom.txt"
endlocal

:: end

Thanks, Ortorea

PS, I hope my discovery in the way it differs on XP v Windows 7 will help others too ?

If Dreams Come True Oleg Would be Famous so far he's very shy, so much for Being Famous ;) http://tinyurl.com/pnenqgb

message edited by Ortorea


Report •

Ask Question