Jump to content

D:\ partition related command


Recommended Posts

hey guys :)

here's what i wanna do : i have somethings related with the existance of the D:\ partition. that is i need a cmd format which check if a D:\ partition exist, if so then apply or run any other command if not then it will ignore.

example:

if D:\ partition exist then move "my documents" folder to D:\my documents. if not exist then ignore or do nothing. in this example i may change "move my documents folder" command with any other D:partition related commands so i need a template. it must be something like below

if exist D:\

then start %CDROM%\mydocs.cmd

if not >null

can you help me about this template please.

thanks in advance :)

Link to comment
Share on other sites


Here is a template for you to use. You will have to add what ever you need to the

function called Work

@Echo Off
CLS
Color 9f
Mode 55,11

If Exist D: Goto Work
If Not Exist D: Goto Quit

:: Place What Ever Coding You Need Below Work
:Work
CLS
Echo.
Echo Found D Drive Preparing Next Cmd
ping -n 4 127.0.0.1>nul
Goto TheEnd


:Quit
CLS
Echo.
Echo Missing D Drive Exiting Cmd
ping -n 4 127.0.0.1>nul
Exit

:TheEnd
Exit

Link to comment
Share on other sites

Basic Example

mountvol|find "D:\">nul&&<place your command here>

Depending upon the exact use required, there may be a problem with the above solutions!

D: may exist but may be allocated to a non-writable or non-fixed drive! Not the most appropriate of places to be trying to issue a move command to for instance.

Link to comment
Share on other sites

Create a vb script (I called it t.vbs)

On Error Resume Next
const wbemFlagReturnImmediately = &h10
const wbemFlagForwardOnly = &h20
set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
for each objItem In colItems
WScript.Echo "Found " & objItem.Name & " " & objItem.Description
if objItem.Name = "D:" then
WScript.Echo "Drive type is " & objItem.Description
if objItem.DriveType=3 then
WScript.Quit(0) 'This is a Local Fixed Disk
else
WScript.Quit(1) 'D: is not a Local Fixed Disk no need to continue
end if
end if
next
WScript.Quit(1)

Call this from a batch file and use the return value.

@echo off
cscript -nologo "%~dp0t.vbs"
if not errorlevel 1 (
echo Moving my documents to the D: drive
)

You could also modify the script to use WshShell.Run to directly call a batch file.

set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

Note: You will also need to modify the "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Personal" registry key so Windows knows where you put the My Documents folder.

Edited by FrankE9999
Link to comment
Share on other sites

@gunsmokingman

@Yzöwl

i knew that i can trust you :=) you have helped me before. thank you very much for replies. they worked.

@FrankE9999

i also thank you :) but vbs scripts are not as familiar as cmd scripts so i allways prefer cmd scripts. thanks a lot anyway.

Link to comment
Share on other sites

I would check for the existing of D:\nul.ext :

http://msmvps.com/blogs/martinzugec/archiv...-same-name.aspx

http://xset.tripod.com/tip5.htm

it works with mounted drives too.

However, as always best method is Yzöwl's one, I may add that what I would do is to also try "probing" destination drive, using a "tagfile" like in:

@ECHO OFF
IF %1.==. ECHO NO PARAMETER SUPPLIED&GOTO :EOF
SET Targetdrive=%1
ECHO tagfile > %~d0\tagfile.tag
copy /y %~d0\tagfile.tag %~d1>nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
ECHO ERROR DRIVE %~d1 NOT RESPONDING, ERROR!
) ELSE (
ECHO ERROR DRIVE %~d1 IS RESPONDING, OK!
)
IF EXIST %~d1\tagfile.tag del %~d1\tagfile.tag
IF EXIST %~d0\tagfile.tag del %~d0\tagfile.tag

(this example must be run from a R/W enabled drive ;) )

jaclaz

Link to comment
Share on other sites

If you are running under an Administrator account you could always extend the line I gave to check for the type of drive too.

Mountvol|Find "D:\">Nul&&(FSUtil FSInfo DriveType D:|Find "Fixed Drive">Nul 2>&1&&(<place your command here>))

Which could then be reduced due to the nature of the FSUtil command to this single line:

FSUtil FSInfo DriveType D:|Find "Fixed Drive">Nul
2>&1&&<place your command here>

I would check for the existing of D:\nul.ext :

http://msmvps.com/blogs/martinzugec/archiv...-same-name.aspx

Not really relevant to the thread but thanks for the reminder to read Martins blog, I haven't seen much of him around here for some time!
Link to comment
Share on other sites

Which could then be reduced due to the nature of the FSUtil command to this single line:
FSUtil FSInfo DriveType D:|Find "Fixed Drive">Nul
2>&1&&<place your command here>

Will have a look at this to see if I can put this to good use. I would like to redirect the folder My Documents to another drive. I could use this script for this purpose perhaps. Nice, thank you! :)

Link to comment
Share on other sites

  • 3 weeks later...

I would like to ask batch expert to improve my script. Most parts are copy and paste from others scripts and probably not optimized!

I use it to set %temp% to a temp folder on Ramdisk drive.

to find the Ramdisk drive, i check Volume Name rather than Drive Letter because sometime i set Drive Letter to R: and sometime to Z:

i call it with:

FindRD.cmd RamDisk

@ECHO OFF&SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
call :__preinit

set vn=
set dl=

:Variables
@Rem -------------------------------------------
@Rem ! Variables
@Rem ! You may edit or pass-in vlabel
@Rem -------------------------------------------
Set vlabel=%1
Set L-Name=%0

For /F "skip=1 delims= tokens=3" %%a in ('REG QUERY "HKCU\Control Panel\Desktop" /v MultiUILanguageId ^2^> Nul') do call :__%%a

::# make sure all external utilities are accessible
call :__checkExtUtils
if "%INST_ABORT%" == "1" exit /b 1

::# http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/fsutil.mspx
::# fsutil (WinXP/2003 Server)
FOR /F "tokens=*" %%i in ('fsutil fsinfo drives ^| FIND /V ""') DO (
set dl=%%i
SET dl=!dl:~-3,3!
for /f "tokens=2* delims=:" %%j in ('fsutil fsinfo volumeinfo !dl!^|find /i %v%') do (
::# Remove trailing reverse solidus
if "!dl:~-1!"=="\" (set dl=!dl:~0,-1!)
::# Remove leading and trailing spaces if any
for %%k in (%%j) do SET vn=%%k
)
::# Debug infos
for /f "tokens=2* delims=-" %%j in ('fsutil fsinfo drivetype !dl! ^| FIND /V "CD-ROM"') do (
echo/%f% = !dl! %v%= !vn!
)
::# Function call
if "!vn!"=="%vlabel%" call :__found
set vn=
)
goto :eof

::##############################
::#
::# functions
::#

:__preinit
set INST_ABORT=
set INST_Dir=%~dp0
set INST_Dir=%INST_Dir:~0,-1%
set BIN_Dir=%~d0\bin
goto :eof

:__0000040c
set desc="Gestion du volume"
set v="Nom du Volume"
set f="Lecteur fixe"
set c="Lecteur de CD-ROM"
goto :eof

:__00000409
set desc="Volume management"
set v="Volume Name"
set f="Fixed Drive"
set c="CD-ROM Drive"
goto :eof

:__checkExtUtils
::# make sure external utils are accessible and throw an error if not
::# http://barnyard.syr.edu/~vefatica/
::# setenv set environment variables
%BIN_Dir%\setenv -u Temp >nul 2>&1 || goto __checkExtUtilsError
goto :eof

:__checkExtUtilsError
echo Some of the external utilities are not accessible.
echo Make sure you extracted the archive correctly and confirm
echo that the utilities are in bin directory under root
set INST_ABORT=1
goto :eof

:__found
::# set volative environment variable RamDisk
%BIN_Dir%\setenv -v !vn! !dl!
@Echo ! ----------------------------------------
echo volative environment variable !vn! set to !dl!
set vn=
goto :eof

::#
::# functions end
::#
::##############################

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...