• 0

Script to detect current drive letter


Question

Hi,

I'm currently using a batch file to run SUBST to load a program off my USB drive

SUBST Z: "G:\FolderName"
Z:\program.exe
SUBST Z: /D

However, since the USB drive letter changes when I plugin to different computers, I'm having to edit the batch file every single time. Is it possible to write a script/program/something which can detect the current drive letter (of the USB drive) that it is run from and pass this drive letter to the SUBST command to load a virtual drive off a folder on the USB drive?

I can't just run the program off the USB drive as the setting file it uses has hard-coded links.

I'd really appreciate it if someone could help me write up a quick program. A script would not be all that preferable as a lot of the computers that I use block the use of vbs scripts. Best would be something which can be compiled into an exe. I myself am vaguely familiar with AHK scripts but I can't seem to find a command to detect current drive letter in AHK. What scripting language should I be looking at then? C#? VB?

Thanks!

6 answers to this question

Recommended Posts

  • 0

Try this VBS Script it should do what.

Save As SUBST.vbs

 Dim Act :Set Act = CreateObject("Wscript.Shell")
 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
 Dim Target
 Dim D1, D2, D3
'/-> Set The Target Folder Path Without The Drive Letter
  Target = "FolderName"
   For Each D1 In Fso.Drives
	If D1.IsReady Then
'/-> Add The Drive Letter To Target
	 D2 = D1.DriveLetter & ":\"
	 D3 = D2 & Target
'/-> If Folder Found Run The Subst
	  If Fso.FolderExists(D3) Then
	   Act.Run(_
	   "%Comspec% /C @ECHO OFF && CLS && COLOR F9 && MODE 55,7 && " &_
	   "TITLE SUBST && Echo. && Echo Processing  SUBST cmd && " &_
	   "SUBST Z: " & Chr(34) & D3 & Chr(34) & " && Z:\program.exe && " &_
	   "SUBST Z: /D && Exit"),1,True 
	  End If
	End If 
   Next
  • 0

Hi jake1eye,

Your script works beautifully. I also managed to figure out how to do the same thing in a batch file.

SUBST Z: "%~d0\FolderName"
Z:\program.exe
SUBST Z: /D

It seems to be a bit quicker than your vbs.

Now, if only there was a way of converting either the bat or the vbs file into an exe so that it would work on systems which block scripts from running. I know there any many pay/shareware programs available which claim to do this, but is there a free one that works?

Or can I do the same thing in VB?

Edit: the vbstoexe program available here works great. But bat 2 exe converter also available from there seems to process the whole bat file without waiting for the program to open and close (like how the bat and vbs files work).

I'd still like to be able to edit the icon of the exe file produced. So, I'd still like to know if this thing is possible in VB? I haven't touched VB for about 6 years and so am not sure if it'll work.

Thanks!

Edited by ramian
  • 0

Myself if I want the script to act like a exe, I just use Winrar Sfx.

I added couple of lines to the original Vbs file so it runs from temp

after it has been extracted, fom the Sfx.

 Dim Act :Set Act = CreateObject("Wscript.Shell")
 Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
 Dim Target,Tmp
 Dim D1, D2, D3
'-> Set The Temp Loation For The Sile
 Tmp = Act.ExpandEnvironmentStrings("%Temp%")
'/-> Set The Target Folder Path Without The Drive Letter
  Target = "FolderName"
   For Each D1 In Fso.Drives
	If D1.IsReady Then
'/-> Add The Drive Letter To Target
	 D2 = D1.DriveLetter & ":\"
	 D3 = D2 & Target
'/-> If Folder Found Run The Subst
	  If Fso.FolderExists(D3) Then
	   Act.Run(_
	   "%Comspec% /C @ECHO OFF && CLS && COLOR F9 && MODE 55,7 && " &_
	   "TITLE SUBST && Echo. && Echo Processing  SUBST cmd && " &_
	   "SUBST Z: " & Chr(34) & D3 & Chr(34) & " && Z:\program.exe && " &_
	   "SUBST Z: /D && Exit"),1,True 
	  End If
	End If 
   Next
'/-> Delete The File 
  Fso.DeleteFile(WScript.ScriptFullName)

This a Link to my custom Winrar Sfx with the above code changes

Subst Sfx

  • 0
Myself if I want the script to act like a exe, I just use Winrar Sfx.

I added couple of lines to the original Vbs file so it runs from temp

after it has been extracted, fom the Sfx.

This a Link to my custom Winrar Sfx with the above code changes

Subst Sfx

Hi everyone,

I know this post is a few months later, but I really need a fast response. I'm doing the same thing as ramian except with a DVD, so it should be the exact same idea. I am fairly new to programming but am very computer literate. Anyway, I am creating DVDs to sell with videos on them, and a PDF file with links to open the videos. The guide will be in the root directory and the videos will be in a folder named "videos". I didn't think this would be necessary to do since the PDF was on the same drive as the videos, but Adobe 6.0 doesn't let you create relative links, only absolute ones.

So anyway, I am going to make an autorun file that will execute these scripts and assign a drive letter such as Z, and then I can hopefully get Adobe to do that, which it should. I tried copying and pasting these scripts into notepad, saving them with the .vbs or .bat file extensions and adjusting them accordingly, but I was unsuccessful. I had it working once, but then it stopped? When I executed the scripts, it would either continuously open new windows until I deleted the file, freeze and say "loading subst", or the cursor would load and nothing would come up (as desired), but the new drive letter wouldn't be in My Computer. I did much better with the vbs than bat. Here is what I tried with the provided scripts:

Dim Act :Set Act = CreateObject("Wscript.Shell")

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")

Dim Target

Dim D1, D2, D3

'/-> Set The Target Folder Path Without The Drive Letter

Target = "Videos"

For Each D1 In Fso.Drives

If D1.IsReady Then

'/-> Add The Drive Letter To Target

D2 = D1.DriveLetter & ":\"

D3 = D2 & Target

'/-> If Folder Found Run The Subst

If Fso.FolderExists(D3) Then

Act.Run(_

"%Comspec% /C @ECHO OFF && CLS && COLOR F9 && MODE 55,7 && " &_

"TITLE SUBST && Echo. && Echo Processing SUBST cmd && " &_

"SUBST Q: " & Chr(34) & D3 & Chr(34) & " && Q:\readme.txt && " &_

"SUBST Q: /D && Exit"),1,True

End If

End If

Next

I was hoping to have the virtual drive assigned and stay assigned until the computer is off, and then also have the readme.txt pop up. What am I doing wrong? Need help fast. Thanks!

Kris

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • FastStone Image Viewer 8.5 by Razvan Serea FastStone Image Viewer is a fast, stable, user-friendly image browser, converter and editor. It has a nice array of features that include image viewing, management, comparison, red-eye removal, emailing, resizing, cropping, retouching and color adjustments. Its innovative but intuitive full-screen mode provides quick access to EXIF information, thumbnail browser and major functionalities via hidden toolbars that pop up when your mouse touches the four edges of the screen. Other features include a high quality magnifier and a musical slideshow with 150+ transitional effects, as well as lossless JPEG transitions, drop shadow effects, image annotation, scanner support, histogram and much more. It supports all major graphic formats (BMP, JPEG, JPEG 2000, animated GIF, PNG, PCX, PSD, EPS, TIFF, WMF, ICO and TGA) and popular digital camera RAW formats (CRW, CR2, NEF, PEF, RAF, MRW, ORF, SRF, ARW, SR2, RW2 and DNG). FastStone Image Viewer features: Image browser and viewer with a familiar Windows Explorer-like user interface Support for many popular image formats and PDF viewing True Full Screen viewer with convenient image zoom support and unique fly-out menu panels Crystal-clear and customizable one-click image magnifier Powerful image editing tools: Resize/resample, rotate/flip, crop, sharpen/blur, adjust lighting/colors/curves/levels etc. Eleven re-sampling algorithms to choose from when resizing images Image color effects: gray scale, sepia, negative, Red/Green/Blue adjustment Image special effects: drop shadow, framing, bump map, sketch, oil painting, lens Draw texts, lines, highlights, rectangles, ovals and callout objects on images Clone Stamp and Healing Brush Superior red-eye effect removal/reduction with completely natural looking end result Multi-level Undo/Redo capability Single click to switch between best fit and actual size mode Image management, including file tagging, rating and drag-and-drop to copy/move/re-arrange files Histogram display with color counter feature Compare images side-by-side (up to 4 at a time) to easily cull those forgettable shots Image EXIF metadata support (plus comment editing for JPEGs) Configurable batch processing to convert/rename large or small collections of images Slideshow with 150+ transition effects and music support (MP3, WMA, WAV...) Create efficient image attachments for emailing to family and friends Print images with full page-layout control Create fully configurable contact sheets Create memorable artistic image montages from your family photos for personalized desktop wallpapers (Wallpaper Anywhere) Acquire images from scanners. Support batch scanning to PDF, TIFF, JPEG and PNG Versatile screen capture capability Powerful Save As interface to compare image quality and control generated file size Run favorite external editors with one keystroke from within Image Viewer Offer portable version of the program which can be run from a removable storage device Configurable mouse wheel support Support themes (bright, gray and dark) Support dual-monitor configurations Support touch interface (tap, swipe, pinch) Support dual instances Play video and audio files (Third party codecs may be required for old versions of Windows) And much more... FastStone Image Viewer 8.5 changelog: Added support for SVG format Added Start importing automatically and Handle duplicate file names automatically options to the Import Photos and Videos tool WebP files can now be rotated and saved with a single click Enhanced dark theme support in the PDF viewer Fixed a bug where some links in PDF files were not clickable Other improvements and bug fixes Download: FastStone Image Viewer 8.5 | Portable | ~15.0 MB (Freeware) View: FastStone Image Viewer Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yup, broke my comp… again. its times like this when I regret AMD. This just never happens on NV.
    • Huh? You're delusional calling the Steam Deck dead. It is so successful that it has sold out multiple times. Even after the price hike this year it sold out again with 24 hours of being back in stock. The demand is real and has not died down even after four years.
    • Same place "Unreal III" is, in everyone's thoughts!
    • So how much water is used in that "initial charge" and how often will it need to be recharged?
  • Recent Achievements

    • Rookie
      DaviKar went up a rank
      Rookie
    • Dedicated
      HidekoYamamoto94 earned a badge
      Dedicated
    • One Month Later
      timbobit earned a badge
      One Month Later
    • One Month Later
      nates earned a badge
      One Month Later
    • Week One Done
      Almohandis earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      460
    2. 2
      +Edouard
      160
    3. 3
      PsYcHoKiLLa
      110
    4. 4
      Michael Scrip
      85
    5. 5
      Steven P.
      69
  • Tell a friend

    Love Neowin? Tell a friend!