Hey everyone.
Earlier I had some trouble with the fact I wanted my USB HDD Mounted at on every PC I used (several). With some handy tips from the folks on this board and a little bit of research and some practice, I've made a simple batch file that does this for me, that I put into an autorun file. I would like to share this, as well as the other file used ( a link to it) which was shared with me.
My Solution:
With the ReMount utility from http://www.uwe-sieber.de/usbstick_e.html...
(It's a bit down the page.)
Direct link: http://www.uwe-sieber.de/files/remount.zip
(not sure if the direct link is okay, if not I'll edit, just tell me.)
This is in the root of my drive (call it G: to start, a drive letter that mocks me along with H: and I:! They're evil I tell you, EVIL! )
My new batch file is also in the root. I called it REMOUNTX.BAT
Simple, eh?
I would just like to share my code for the batch file with you. If you can use it, good, and if you improve it, great! I would not have been able to figure this out without PortableNotepad2. I think that one was by nm35, and it was EXTREMELY helpful. Syntax highlighting helped me figure out my errors.
The Batch File...
REM REMOUNTX.BAT
REM MADE BY JAKE RICHARDS
REM WEDNESDAY, MAY 03, 2006
REM THIS BATCH FILE REMOUNTS THE USB DRIVE IT RESIDES ON AS DRIVE X.
REM THIS IS ACCOMPLISHED BY USER INPUT (CURRENT DRIVE LETTER).
REM THIS BATCH FILE USES THE REMOUNT UTILITY FROM
REM "TIPS FOR USB DRIVES" @ http://www.uwe-sieber.de/usbstick_e.html
@ECHO OFF
CLS
@ECHO OFF
SET /P DRIVE=PLEASE ENTER THE CURRENT DRIVE LETTER OF THE USB DEVICE...
ECHO.
ECHO ARE YOU SURE YOU WISH TO REMOUNT DRIVE %DRIVE% AS DRIVE X?
SET /P YESCON=([Y],[y] FOR YES / ANY OTHER KEY TO CANCEL AND EXIT)
IF %YESCON%==Y GOTO REMOUNT
IF %YESCON%==y GOTO REMOUNT
ECHO.
ECHO COMMAND ABORTED!
:REMOUNT
ECHO FINAL CHANCE TO ABORT! PRESS [CTRL] + [C] TO ABORT!
ECHO.
ECHO PRESS ANY OTHER KEY TO CONTINUE.
PAUSE > NUL
@ECHO ON
REMOUNT %DRIVE%
I know its probably sloppy and not as efficient as some of you could do, but hey, I'm just figuring this out for the first time. Hope it helps someone else. Thanks for the previous tips that helped me figure this out
Jacoby
Comment:
IF %YESCON%==Y GOTO REMOUNT
use
IF /I "%YESCON%"=="Y" GOTO REMOUNT
for capital+small letters and empty input. BTW you don't abort:
after
ECHO COMMAND ABORTED.
enter
goto :EOF
There is a tool called Unlocker, which tells youthe open files on the drive: http://ccollomb.free.fr/unlocker/
My solution for some time allows mount points (for NTFS formatted usb HDDs or mounting usbdrives on c:), prints result and can be integrated into another batch (that's how i do it, i cut it out there ):
@echo off
:Mount_Remount
set /p MountPointSource=Please enter Source MountPoint a=Abort [%MountPointSource%] :
if "%MountPointSource%" EQU "a" (
echo --- Aborted.
goto :EOF
)
set /p MountPointDest=Please enter Destination MountPoint a=Abort [%MountPointDest%] :
if "%MountPointDest%" EQU "a" (
echo --- Aborted.
goto :EOF
)
:Mount_Remounting
echo ~ ReMounting Drive [%MountPointSource%] to [%MountPointDest%]
Remount %MountPointSource% %MountPointDest%
@echo off
set Mount_DeviceResult=%ERRORLEVEL%
echo Mount_DeviceResult: %Mount_DeviceResult%
if "%Mount_DeviceResult%" EQU "1" goto Mount_RemountOK
rem color 4f
echo ----- The drive could not be Remounted [%Result%]. Files are open on the drive. -----
:Mount_RemountOK
rem color f2
if "%Device%" EQU "1" echo * # * The Drive has been successfully remounted from [%MountPointSource%] to [%MountPointDest%]* # *
goto Ende
:Ende
pause
I made a couple of changes:
- Automatically gets the current drive letter so user doesn't have to input it.
- Cleared environment variables at end
- Got rid of the ALL CAPS comments and prompts which IMHO makes things easier to read.
@ECHO OFF
ECHO REMOUNTX.BAT
ECHO Original: Jake Richards - 5/3/2006
ECHO Modified: Jerry Riechert - 5/4/2006
ECHO This batch file remounts the USB drive it resides on as drive X.
ECHO This batch file uses the remount utility from
ECHO "Tips For Usb Drives" @ Http://Www.Uwe-Sieber.De/Usbstick_E.Html
ECHO and Timo Salmi's excellent batch file FAQ at http://garbo.uwasa.fi/
REM get current drive letter
ECHO @echo off> volume.bat
ECHO set drive=%%3>> volume.bat
DIR | FIND "Volume"> go.bat
CALL go
IF EXIST volume.bat DEL volume.bat
IF EXIST go.bat DEL go.bat
ECHO.
ECHO Are you sure you wish to remount drive "%drive%" as drive "X"?
SET /P YESCON='Y' to remount, Any other key to Cancel:
IF /i x%YESCON%==xY GOTO REMOUNT
ECHO.
ECHO COMMAND ABORTED!
goto :EOF
:REMOUNT
ECHO.
ECHO Final chance to Abort! Press Ctrl-C to Abort!
ECHO Press any other key to continue.
PAUSE > NUL
REMOUNT %DRIVE% X:
:EOF
SET drive=
SET yescon=
Thanks guys! The auto get drive letter is really cool and I'll definately be using it. And thanks for the cleanup and corrections. I'm new to batchfiles that do more that subst A: c:\Floppy\, and subst /d A:, heheh. I don't have a floppy drive on my main computer (which I HATE!)
Maybe we can make a really cool batch file collaboratively.
Questions though,
Is there any way to get Pstart to autorun from the new drive (X) after the batch runs from autorun on Drive , say G or H? I'm thinking of chaining batch files, the first to wait 5 seconds, then call another batch once drive X is mounted and autorun PSart with it.
Or is something like this feasible?
Something like:
REMOUNT DRIVE G:
GOTO WAIT5
:WAIT5
REM This is here because I dont know any other way to wait for a few REM seconds
PING 127.0.0.1 -n 1 -w 5000 >NUL
GOTO PStart
:PStart
X:\PStart.exe
Note: I know it's sloppy, I just wrote it in about 2 minutes and have not tested it. It's meant to find feasability. PLEASE Comment and tell me if and where I go wrong and any ways to optimize or clean it up.
Jacoby
---
Posted in Mozilla PowerSkunk. Other Browsers run in fear!
(Brought to you by Firesomething.)
I have 3 files on my USB hard drive: autorun.bat, autorun.inf and autorun.vbs. They use several utility programs (mnt, mount and deveject) also.
- The .inf runs the .bat
- The .bat remounts the drive (if necessary) and then runs the .vbs
- The .vbs starts PStart and waits. When PStart closes, the .vbs continues to unmount and safely eject the USB drive.
While this works on my USB hard drive I can't get autorun to work on my USB flash drive. Apparently this is how windows XP works.
autorun.inf:
[autorun]
shellexecute=autorun.bat
;open=autorun.vbs
icon=programs\pstart\pstart.exe
action=Run PStart
autorun.bat:
@echo off
:: get current drive letter
ECHO @echo off> volume.bat
ECHO set drive=%%3>> volume.bat
DIR | FIND "Volume"> go.bat
CALL go
IF EXIST volume.bat DEL volume.bat
IF EXIST go.bat DEL go.bat
:: check if drive Y
IF %drive% EQU Y GOTO :STARTVBS
remount %drive% Y
:STARTVBS
start autorun.vbs
goto end
:END
autorun.vbs:
' Name: autorun.vbs
' Author: Grant Emsley
' Date: Jan 28, 2006
'
' What it does:
'
' This script takes the full path/name of where the script was run from, strips
' off everything except the drive letter and colon, and sets it to the environmental
' variable "portabledrive". It sets this in the Volatile Environment so it will
' remain set between different programs, but gets cleared when you logout/reboot.
' This allows you to make all your shortcuts and such in the format:
' %portabledrive%\programs\programname\program.exe
' This means that most programs that have trouble with relative paths will work
' ---------------------------------------------------------
' Required Programs: deveject.exe (http://www.heise.de/ct/03/16/links/208.shtml)
' unmount.exe and mnt.exe (http://www.uwe-sieber.de/files/unmnt10.zip)
'
' These are required if you want to automatically "safely remove hardware"
' -----------------------------------------------------------
Dim objShell
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
' -------------------------------------------------------------
' get the drive letter
arrPath = Split(Wscript.ScriptFullName, "\")
strdriveletter = arrPath(0)
' create a collection to access the Volatile environment, then set the portabledrive
set colVolatileEnvVars = objShell.Environment("Volatile")
colVolatileEnvVars("portabledrive") = strdriveletter
' ----------------------------------------------------------
' I have a folder called "Portable Documents" on my drive. Lets make a shortcut
' in "My Documents" to my portable documents folder. This way I can just click
' there on any save/open dialogue box.
strMyDocs = objShell.SpecialFolders("MyDocuments")
set objSLink = objShell.CreateShortcut(strMyDocs & "\Portable Documents.lnk")
objSLink.TargetPath = strdriveletter & "\Documents"
objSlink.Save
' ----------------------------------------------------------------------------------------------------
' NOTE: if you want to run something before starting pstart, do it below this
' line. (guide to running programs is at the end of the script)
'objShell.Run strdriveletter & "\usbdiskguard.exe", 1, FALSE
objShell.Run strdriveletter & "\Programs\KeePass\KeePass.exe", 1, FALSE
' Now lets run pstart, and wait for it to exit before we continue
objShell.Run strdriveletter & "\programs\PStart\pstart.exe", 1, TRUE
' NOTE: If you want to run something AFTER running pstart, do it below this line.
' Pstart is done, so lets clean up
' Delete the shortcut to Portable Documents
objFSO.DeleteFile(strMyDocs & "\Portable Documents.lnk")
' close any programs started above
' first attempt a gracefull close and then force it, if necessary
'objShell.Run "\programs\MyTaskkill.bat keepass.exe", 0, true
'objShell.Run "\programs\MyTaskkill.bat nsis.exe", 0, true
'objShell.Run "\programs\MyTaskkill.bat freeCommander.exe", 0, true
'objShell.Run "\programs\MyTaskkill.bat usbdiskguard.exe", 0, true
objShell.Run "taskkill /t /im keepass.exe", 0, true
objShell.Run "taskkill /t /im /f keepass.exe", 0, true
objShell.Run "taskkill /t /im nsis.exe", 0, true
objShell.Run "taskkill /t /im /f nsis.exe", 0, true
objShell.Run "taskkill /t /im freeCommander.exe", 0, true
objShell.Run "taskkill /t /im /f freeCommander.exe", 0, true
objShell.Run "taskkill /t /im usbdiskguard.exe", 0, true
objShell.Run "taskkill /t /im /f usbdiskguard.exe", 0, true
objShell.Run "taskkill /t /im bcarchive.exe", 0, true
objShell.Run "taskkill /t /im /f bcarchive.exe", 0, true
' Safely remove hardware
' Probably won't work if you aren't an administrator
' it WONT work if you have open files on the drive (probably a good thing)
' Warn the user that we are going to dismount the drive
Wscript.Echo "Ready to remove portable drive. Please close all programs/files on the drive, then click ok."
' First we have to change the current directory so we aren't holding the drive open.
objShell.CurrentDirectory = "C:\"
' Now we copy the files to the c drive
objFSO.CopyFile strdriveletter & "\programs\unmount.exe", "c:\", true
objFSO.CopyFile strdriveletter & "\programs\mnt.exe", "c:\", true
objFSO.CopyFile strdriveletter & "\programs\deveject.exe", "c:\", true
objFSO.CopyFile strdriveletter & "\removedrive.exe", "c:\", true
' First we run unmount. It waits until all files are closed on the drive then removes the drive letter.
objShell.Run "c:\unmount -l -a -e " & strdriveletter, 1, TRUE
' Now we run deveject to unmount the drive.
' You will have to modify the following line to get this to work. Run deveject
' with no options and it will list all the devices that can be removed. Change
' the device ID to the one that's listed for your drive.
objShell.Run "c:\deveject -EjectId:""USB\VID_05E3&PID_0702\6&AC549B9&0&3""", 0, TRUE ' example
objShell.Run "c:\deveject -EjectId:""USB\VID_067B&PID_2507""", 0, TRUE ' Cornice 1G HD
'alternate to unmount and eject the drive
'objShell.Run "c:\removedrive y: -l", 0, TRUE ' Cornice 1G HD
' Then we clean up after ourselves
objFSO.DeleteFile("C:\unmount.exe")
objFSO.DeleteFile("C:\mnt.exe")
objFSO.DeleteFile("C:\deveject.exe")
objFSO.DeleteFile("C:\removedrive.exe")
Wscript.Echo "Your drive should be safely removed."
Wscript.Quit
' ----------------------------------------------------------------------------------------------------
' objShell.run , ,
'
' Some good window states:
' 0 - hidden
' 1 - normal
' 2 - minimized forground
' 3 - maximized
' 7 - minimized background
'
' The last parameter takes either true or false. If true, it pauses the script
' and waits for it to exit before continuing. Default is false.
' ----------------------------------------------------------------------------------------------------
Please see my post "Suggest AutoDriveLetter" for easier Drive letter finding.
For short:
%CD% points to the corrent Directory which yould be in case of a usb-drive root the original driveletter.
BTW:
:EOF means EnfOfFile, it's an auto Jump mark. This is also used for returns from "call :Jumpmark":
--
call :Jumpmark
echo After Jump
:Jumpmark
echo This is the jump
goto :EOF
pause
--
This would output:
This is the jump
After Jump
This is the jump
Note that the pause is not displayed since the 2nd time the jumpmark is passed (the 2nd "This is the jump") the goto :EOF command is effectifely exiting the batch. Not to mistake it for exit whcih would normally not return to the calling batch.
-------------------------------
Insert the following line directly after into my first script
suggestion @echo off:
set MountPointSource=%CD%
to get the MountPoint suggested.
For Driveletter only use:
set MountPointSource=%CD:~0,3%
or use the following script:
The command "call :Mount f x" mounts f to x.
The new batch accepts cmdline parameters if you use "call :Mount %1 %2".
If 0 parameter entered: Suggest actual drive as source and ask for second, allows change.
If 1 parameter entered: ReMount actual drive as 1st parameter, does not allow change during runtime.
If 2 parameters entered: ReMount 1st parameter as 2nd parameter, does not allow change during runtime.
Feel free to add additional "Are you sure"s
--------- Begin RemountMe.bat ---------
@echo off
call :Mount f x
goto Ende
:Mount
if "%1" NEQ "" set MountPointSource=%1
if "%2" NEQ "" (set MountPointDest=%2&goto Mount_Remounting ) else (set MountPointSource=%CD%&set MountPointDest=%1)
:Mount_Remount
set /p MountPointSource=Please enter Source MountPoint a=Abort [%MountPointSource%] :
if "%MountPointSource%" EQU "a" (
echo --- Aborted.
goto :EOF
)
set /p MountPointDest=Please enter Destination MountPoint a=Abort [%MountPointDest%] :
if "%MountPointDest%" EQU "a" (
echo --- Aborted.
goto :EOF
)
:Mount_Remounting
echo ~ ReMounting Drive [%MountPointSource%] to [%MountPointDest%]
Remount %MountPointSource% %MountPointDest%
@echo off
set Mount_DeviceResult=%ERRORLEVEL%
echo Mount_DeviceResult: %Mount_DeviceResult%
if "%Mount_DeviceResult%" EQU "1" goto Mount_RemountOK
rem color 4f
echo ----- The drive could not be Remounted [%Result%]. Files are open on the drive. -----
:Mount_RemountOK
rem color f2
if "%Device%" EQU "1" echo * # * The Drive has been successfully remounted from [%MountPointSource%] to [%MountPointDest%]* # *
goto :EOF
:Ende
pause
--------- End RemountMe.bat ---------
Sorry for the excurse to batch language
for pointing out the %CD% autodrive letter. I hadn't known about that. It certainly speeds things up.
The reason I use a :END instead of :EOF is that I typically set all environment variables in the .bat file back to nothing. Otherwise I sometimes run into problems running the file multiple times.
So, my new autorun.bat is:
@echo off
:: get current drive letter
set drive=%CD:~0,3%
:: check if drive Y
IF %drive% EQU Y GOTO :STARTVBS
remount %drive% Y
:STARTVBS
start autorun.vbs
goto end
:END
set drive=
For using "Local" variable space there is a command setlocal
the correcponding command for ending this space would be endlocal.
See setlocal /? for help, the important things:
NABLEEXTENSIONS / DISABLEEXTENSIONS
should be used always since it enhances funcionality for batch files.
ENABLEDELAYEDEXPANSION / DISABLEDELAYEDEXPANSION
Can be used to evaluate variables inside command blocks things in (...)
You do this by changing % to ! like:
---
(Usually the default):
setlocal DISABLEDELAYEDEXPANSION
set var=1
(set var=2
echo VAR:%var%
)
prints: VAR: 1
---
(can be set as default in registry):
setlocal ENABLEDELAYEDEXPANSION
set var=1
(set var=2
echo VAR:!var!
)
prints: VAR: 2
---
Useful in bigger FOR-loops
Is there any way to make PStart AND Remount appear at the autorun menu of windows XP? I can only see one at a time...
BTW- THANKS FOR THE BATCH FILE!!! I'M USING IT!
Lo importante es no dejar de hacerse preguntas. -- Albert Einstein
(What's important is not finishing making questions)
Lo importante es no dejar de hacerse preguntas. -- Albert Einstein
(What's important is not finishing making questions)
Heh, I'm working on it, give me time. I have a few ideas.
---
Posted in Mozilla PowerSkunk. Other Browsers run in fear!
(Brought to you by Firesomething.)
Your Autorun.inf to have 2 menu entries:
[autorun]
open=Pstart\Pstart.exe
shell=open
action=Start Pstart
shell\open\command=Pstart\Pstart.exe
shell\open=Pstart
shell\Remount\command=Remount\Remount.bat
shell\Remount=Remount
It just added some shell options that don't work and removed the shell "Open" option which i use a lot.
Maybe if someone developed a small launcher which would remount if the drive letter is not correct and would start pstart if the drive letter is correct.
Maybe it would also close all apps opened from pstart, when pstart was closed.
anyone any ideas?
Lo importante es no dejar de hacerse preguntas. -- Albert Einstein
(What's important is not finishing making questions)
Lo importante es no dejar de hacerse preguntas. -- Albert Einstein
(What's important is not finishing making questions)
Use pre tags not code.
*John: cleanup in aisle 6*
----
R McCue
"If you're not part of the solution, you're part of the precipitate."
Yours
Steve Lamerton
Developer Profile Sync & PortaLog
EEEK!! Look what I have started. Sorry folks. Been busy getting ready for, and taking finals. Two more to go.
I see a lot of cool ideas here I'm going to try out. How well I can adapt them and implement them remains to be seen, but I'll try. Thanks for all the feedback. Like I'm not busy enough right now with finals. Heheh. j/k
---
Posted in Mozilla PowerSkunk. Other Browsers run in fear!
(Brought to you by Firesomething.)