Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Find my current drive letter and execute a program from it while i

Reply
 
Old 03-23-2009   #1 (permalink)
Luka1993


 
 

Find my current drive letter and execute a program from it while i

HI!

The problem is that when i change the directory to usbletter:\SYSTEM i can't
find a way to execute a program from the source directory where this script
is located.

Thank you in advance.

If this helps, this script was meant to be ran from a CD. Then it would find
the USB and so on.

Sorry for my bad writing, im only 15 and im not a programmer i just want to
edit this script and learn.

Luka

Here is the code: Look at the comments marked with '

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set colDrives = objFSO.Drives
Set objEnv = objShell.Environment("PROCESS")


objEnv("Year") = Year(now())
objEnv("Month") = Right("0" & Month(now()), 2)
objEnv("Day") = Right("0" & Day(now()), 2)
objEnv("Hour") = Right("0" & Hour(now()), 2)
objEnv("Minute") = Right("0" & Minute(now()), 2)
objEnv("Second") = Right("0" & Second(now()), 2)

For Each objDrive in colDrives
If objFSO.FileExists(objDrive.DriveLetter & ":\usbishere.txt") Then
'find my USB
strPath = objDrive.Driveletter & ":" 'sets the USB letter
sPathSpec = strPath & "\SYSTEM" 'change the path into a path on
the USB
CreateObject("Wscript.Shell").CurrentDirectory = sPathSpec

'now this is where i want to execute a program from the folder this script
is located WHILE in the \SYSTEM folder on the usb.

End If
Next


My System SpecsSystem Spec
Old 03-23-2009   #2 (permalink)
Sam Hays


 
 

Re: Find my current drive letter and execute a program from it while i

Just to make sure I understand: you want to run a file at the location where
the VBScript is running from - not necessarily on the USBdrive?

If that's the case - the path of the running vbscript can be obtained with
the following script:

lenScriptFullName= len(wscript.scriptfullname)
b = 0

for l = lenScriptFullName to 1 step -1
if b <> 1 then
if mid(Wscript.scriptfullname, l, 1) <> "\" then
a=a+1
else
b=1
end if
end if
next

totrim = len(wscript.scriptfullname) - a
wscript.echo mid(wscript.scriptfullname, 1, totrim)


Good luck sir
Sam

"Luka1993" <Luka1993@xxxxxx> wrote in message
news:0CB252CE-0A69-4C3B-8811-51383D17767F@xxxxxx
Quote:

> HI!
>
> The problem is that when i change the directory to usbletter:\SYSTEM i
> can't
> find a way to execute a program from the source directory where this
> script
> is located.
>
> Thank you in advance.
>
> If this helps, this script was meant to be ran from a CD. Then it would
> find
> the USB and so on.
>
> Sorry for my bad writing, im only 15 and im not a programmer i just want
> to
> edit this script and learn.
>
> Luka
>
> Here is the code: Look at the comments marked with '
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objShell = CreateObject("Wscript.Shell")
> Set colDrives = objFSO.Drives
> Set objEnv = objShell.Environment("PROCESS")
>
>
> objEnv("Year") = Year(now())
> objEnv("Month") = Right("0" & Month(now()), 2)
> objEnv("Day") = Right("0" & Day(now()), 2)
> objEnv("Hour") = Right("0" & Hour(now()), 2)
> objEnv("Minute") = Right("0" & Minute(now()), 2)
> objEnv("Second") = Right("0" & Second(now()), 2)
>
> For Each objDrive in colDrives
> If objFSO.FileExists(objDrive.DriveLetter & ":\usbishere.txt") Then
> 'find my USB
> strPath = objDrive.Driveletter & ":" 'sets the USB letter
> sPathSpec = strPath & "\SYSTEM" 'change the path into a path
> on
> the USB
> CreateObject("Wscript.Shell").CurrentDirectory = sPathSpec
>
> 'now this is where i want to execute a program from the folder this script
> is located WHILE in the \SYSTEM folder on the usb.
>
> End If
> Next
>
My System SpecsSystem Spec
Old 03-23-2009   #3 (permalink)
James Whitlow


 
 

Re: Find my current drive letter and execute a program from it while i

"Luka1993" <Luka1993@xxxxxx> wrote in message
news:0CB252CE-0A69-4C3B-8811-51383D17767F@xxxxxx
Quote:

> HI!
>
> The problem is that when i change the directory to usbletter:\SYSTEM i
> can't
> find a way to execute a program from the source directory where this
> script
> is located.
Since you are already instantiating 'Scripting.FileSystemObject', use the
'GetParentFolderName' method:

sScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName)


My System SpecsSystem Spec
Old 03-23-2009   #4 (permalink)
Luka1993


 
 

Thank you

Thanks to both of you.

Yes, you both understood what i was trying to say but can you tell me where
to put this code and how to use it because im 15 and a beginner :P.

You alredy helped me alot

Thank you!
My System SpecsSystem Spec
Old 03-23-2009   #5 (permalink)
Luka1993


 
 

Thank you

Thanks! You both understood me.

I tried to put this in the code but i got and error. I am a beginner, so,
what am i doing wrong?

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set colDrives = objFSO.Drives
Set objEnv = objShell.Environment("PROCESS")

objEnv("Year") = Year(now())
objEnv("Month") = Right("0" & Month(now()), 2)
objEnv("Day") = Right("0" & Day(now()), 2)
objEnv("Hour") = Right("0" & Hour(now()), 2)
objEnv("Minute") = Right("0" & Minute(now()), 2)
objEnv("Second") = Right("0" & Second(now()), 2)

For Each objDrive in objFSO.Drives
If objDrive.IsReady Then
If objFSO.FileExists(objDrive.DriveLetter & ":\flashdrive.txt") Then
strPath = objDrive.Driveletter & ":"
sPathSpec = strPath & "\SYSTEM\LOGS"
CreateObject("Wscript.Shell").CurrentDirectory = sPathSpec
sScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName)
objShell.Run sScriptPath & "\example.exe"
End If
End If
Next
My System SpecsSystem Spec
Old 03-23-2009   #6 (permalink)
James Whitlow


 
 

Re: Thank you

"Luka1993" <Luka1993@xxxxxx> wrote in message
news:F67378F9-8DDE-4176-9F3C-5D4F062B30BD@xxxxxx
Quote:

> Thanks! You both understood me.
>
> I tried to put this in the code but i got and error. I am a beginner, so,
> what am i doing wrong?
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objShell = CreateObject("Wscript.Shell")
> Set colDrives = objFSO.Drives
> Set objEnv = objShell.Environment("PROCESS")
>
> objEnv("Year") = Year(now())
> objEnv("Month") = Right("0" & Month(now()), 2)
> objEnv("Day") = Right("0" & Day(now()), 2)
> objEnv("Hour") = Right("0" & Hour(now()), 2)
> objEnv("Minute") = Right("0" & Minute(now()), 2)
> objEnv("Second") = Right("0" & Second(now()), 2)
>
> For Each objDrive in objFSO.Drives
> If objDrive.IsReady Then
> If objFSO.FileExists(objDrive.DriveLetter & ":\flashdrive.txt") Then
> strPath = objDrive.Driveletter & ":"
> sPathSpec = strPath & "\SYSTEM\LOGS"
> CreateObject("Wscript.Shell").CurrentDirectory = sPathSpec
> sScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName)
> objShell.Run sScriptPath & "\example.exe"
> End If
> End If
> Next
Change line 18 to: objShell.CurrentDirectory = sPathSpec

I have a question. Why are you changing the current directory of the
script?

Also, are you planning on running this script from a single flash drive or
from many flash drives? If you are using a single flash drive, you could
check for the 'VolumeName' or 'SerialNumber' properties and avoid creating
and then testing for a trigger file.


My System SpecsSystem Spec
Old 03-23-2009   #7 (permalink)
Luka1993


 
 

Re: Thank you

Hi!

Thank you again.

I am running this script from a CD wich will find a USB that me only will
use. I could use the label to recognise it too but i didnt know how.

The CD finds my USB and goes to USB:\SYSTEM folder and executes
CD:\something.exe.

If you have a better idea for making this file then suggest please

The perfect idea would be:

-get the USB letter from label
-get the CD letter (where my script is)
-go to USB:\SYSTEM
-execute CD:\something.exe

that is what i want actually :S


My System SpecsSystem Spec
Old 03-23-2009   #8 (permalink)
Hz


 
 

Re: Find my current drive letter and execute a program from it whilei

Sam Hays wrote:
Quote:

> Just to make sure I understand: you want to run a file at the location
> where the VBScript is running from - not necessarily on the USBdrive?
>
> If that's the case - the path of the running vbscript can be obtained
> with the following script:
>
> lenScriptFullName= len(wscript.scriptfullname)
> b = 0
>
> for l = lenScriptFullName to 1 step -1
> if b <> 1 then
> if mid(Wscript.scriptfullname, l, 1) <> "\" then
> a=a+1
> else
> b=1
> end if
> end if
> next
>
> totrim = len(wscript.scriptfullname) - a
> wscript.echo mid(wscript.scriptfullname, 1, totrim)
a = WScript.ScriptFullName
b = WScript.ScriptName
c = Len(a) - Len(b)
WScript.Echo Left(a, c)

--
..::[ Hz ]::.
My System SpecsSystem Spec
Old 03-24-2009   #9 (permalink)
James Whitlow


 
 

Re: Thank you

"Luka1993" <Luka1993@xxxxxx> wrote in message
news:66EACF41-F7F3-4394-903F-E12F1A3EE60A@xxxxxx
Quote:

> Hi!
>
> Thank you again.
>
> I am running this script from a CD wich will find a USB that me only will
> use. I could use the label to recognise it too but i didnt know how.
Using either the volume name or the serial number is basically the same.
You would replace:

OLD: If objFSO.FileExists(objDrive.DriveLetter & ":\flashdrive.txt") Then
NEW: If objDrive.VolumeName = "SCRIPTFLASH" Then

You could also use the serial number:

If objDrive.SerialNumber = &H50187D93 Then

You can find it from the command line by typing 'vol {drive letter}' from
a command prompt (ex: vol c. You will see output like:

Volume in drive C has no label.
Volume Serial Number is 5018-7D93

To use it in your script, just remove the dash from the center and prefix
(&H) before it to tell VBScript it is a hexadecimal number.
Quote:

> The perfect idea would be:
>
> -get the USB letter from label
See above. Use volume label, serial number or, as you have already done,
use a trigger file.
Quote:

> -get the CD letter (where my script is)
To get the drive letter or path, use the methods of
'Scripting.FileSystemObject'. To get the drive letter of the drive where the
script was launched from:

sScriptDrive = objFSO.GetDriveName(WScript.ScriptFullName)

To get the full path to the script:

sScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName)
Quote:

> -go to USB:\SYSTEM
As you have already done:

objShell.CurrentDirectory = sPathSpec
Quote:

> -execute CD:\something.exe
As you have already done. I would suggest wrapping the path and filename
in quotes, though:

objShell.Run """" & sScriptPath & "\example.exe" & """"

Without the quote wrapping, your script would fail if there is a space in
any of the folders in the path to the script itself. So, assuming that the
serial number of your flash drive is '5018-7D93', your script could look
like this:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set objEnv = objShell.Environment("PROCESS")

objEnv("Year") = Year(now())
objEnv("Month") = Right("0" & Month(now()), 2)
objEnv("Day") = Right("0" & Day(now()), 2)
objEnv("Hour") = Right("0" & Hour(now()), 2)
objEnv("Minute") = Right("0" & Minute(now()), 2)
objEnv("Second") = Right("0" & Second(now()), 2)

For Each objDrive in objFSO.Drives
If objDrive.IsReady Then
If objDrive.SerialNumber = &H50187D93 Then
strPath = objDrive.Driveletter & ":"
sPathSpec = strPath & "\SYSTEM\LOGS"
objShell.CurrentDirectory = sPathSpec
sScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName)
objShell.Run """" & sScriptPath & "\example.exe" & """"
End If
End If
Next



My System SpecsSystem Spec
Old 03-24-2009   #10 (permalink)
Luka1993


 
 

Re: Thank you


The script is working and i used the serial number, it really is a better
idea
This was exactly what i wanted

Thank you soooo much!!!
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to execute external program from powershell PowerShell
find the current stat and reboot VB Script
find current boot configuration Vista General
Re: cannot execute program in dir with space and argument PowerShell
Execute scripts in current scope? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46