Jump to content

How can i execute "%CD%\..\File.ini" whit VBScript?


Recommended Posts

HI,

Is it possible to get this DOS commend line "%CD%\..\Settings\File.ini" to work whit a VBScript?

It works in a CMD window but i cannot get it to work whit a VBScript. :(

var Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2");var Act = new ActiveXObject("Wscript.Shell");        Act.Run(Act.ExpandEnvironmentStrings(cbo[i].value),1,true);<input type="checkbox" id="cbo" name="checkbox1" value='"..\Programs\File.exe" /INI="%CD%\..\Settings\File.ini"' checked</input>
Edited by Outbreaker
Link to comment
Share on other sites


I do not think it possible to do what you want because the way VBS intrepid %CD% as this %CD% with no reference to location

post-5386-0-22105000-1390025325_thumb.pn

Here is a Example Using Jscript Using The Same Object As VBS

<!-- Hta And Script By Gunsmokingman Aka Jake1Eye Hta And Script By Gunsmokingman Aka Jake1Eye This code is property of Gunsmokingman and Or Jake1Eye and you must have his permission to use. This is only posted as example code and meant only to used as such.--><SCRIPT LANGUAGE='JScript'> var w=375; h=175  window.resizeTo(w,h)  window.moveTo(screen.availWidth/2-(w/2),screen.availHeight/2-(h/2))  var Act = new ActiveXObject("WScript.Shell");  window.onload=function(){document.title='Demo Script HTA'}</SCRIPT><BODY Scroll='No'> <Input Type="CheckBox" Name='TestMe' OnClick="if(TestMe.checked==true){alert(   'Current Directory\t'+Act.CurrentDirectory+'\n\r'+   'App Data Path    \t'+Act.ExpandEnvironmentStrings('%AppData%'))  }" Value="Test">Demo Checkbox</BODY>
Link to comment
Share on other sites

It doesn't work that way because %CD% is not an Environment String. It is a Dynamic Variable, like %DATE% and %TIME%. VBS has ways to get the Current Directory just like it has ways to get Date and Time. You'll just have to do it a different way than the way you had planned. For example:

Dim objShell :Set objShell = CreateObject("Wscript.Shell")Dim objFSO :Set objFSO = CreateObject("Scripting.FileSystemObject") Dim strFolderName :strFolderName = "."Dim strFullPath :strFullPath = objFSO.GetAbsolutePathName(strFolderName) Dim strPath :strPath = objShell.CurrentDirectory Dim strPath2 :strPath2 = Wscript.ScriptFullNameDim objFile :Set objFile = objFSO.GetFile(strPath2)Dim strFolder :strFolder = objFSO.GetParentFolderName(objFile)  MsgBox (strFullPath & vbCRLF & strPath & vbCRLF & strFolder)


In this case, strFullPath, strPath and strFolder will all be equal to the same thing, the path of the folder where the script resides.

Cheers and Regards

Link to comment
Share on other sites

Shouldn't then this here not work when i replace "Act.ExpandEnvironmentStrings" with "Act.CurrentDirectory"?

var Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2");var Act = new ActiveXObject("Wscript.Shell");Act.Run(Act.CurrentDirectory(cbo[i].value),1,true);<input type="checkbox" id="cbo" name="checkbox1" value='"..\Programs\File.exe" /INI="..\Settings\Config.ini"' checked</input>

The problem is that there is a problem that is insisting that i point to the full path for the silent installation Config.ini file. :(

Edited by Outbreaker
Link to comment
Share on other sites

UPS sorry, ignore the %CD% i only want the script to point (print out) to the full path.

LIKE:

/INI="..\Settings\ProgramConfig.ini"'

And if the script is in "F:\OEM\RunOnce\Scripts\" then the script should add this full path:

/INI="F:\OEM\RunOnce\Settings\ProgramConfig.ini"'

Edited by Outbreaker
Link to comment
Share on other sites

Here is another example of getting location, I also demo how to change the CurrentDirectory to another location

 '-> This code is property of Gunsmokingman and Or Jake1Eye and you must have his permission to use.'-> This is only posted as example code and meant only to used as such.Dim Act :Set Act = CreateObject("Wscript.Shell") Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") Dim Old_CD, vB, vT :vB = vbCrLf :vT = vbTab'-> Change The Current Directory To Another Location Old_CD = Act.CurrentDirectory  Act.CurrentDirectory = "E:\"   MsgBox "Current Folder " & vT & _          Replace(Fso.GetFile(WScript.ScriptFullName), _          Fso.GetFile(WScript.ScriptFullName).Name,"") & vB & _         "Parent Folder  " & vT & _          Fso.GetParentFolderName(WScript.ScriptFullName) & vB & _         "Path Name      " & vT & _          Fso.GetAbsolutePathName(WScript.ScriptFullName) & vB & vB & _         "Old Current Directory  " & vT & Old_CD & vB & _         "New Current Directory " & vT & Act.CurrentDirectory & vB & _         "Vbs File Name" & Space(15) & vT & _          Fso.GetFile(WScript.ScriptFullName).Name 
If you need to use Cmd with Vbs here is a VBS script that out put %CD% varible with the correct path.

Dim Act :Set Act = CreateObject("Wscript.Shell") Act.Run( _ "%Comspec% /c @Echo Off && COLOR 9F && CLS && MODE 62,9 && CLS && Echo. &&" & _ "Set CD=CD && Echo. && Echo Current Directory = %CD% && ping -n 5 127.0.0.1>nul"),1,True 
post-5386-0-46196600-1390072609_thumb.pn
Link to comment
Share on other sites

@Outbreaker you will find it easier if you assign the current directory to a variable and then put the variable where you want it. Did you expect it to fill in the full path by itself?

Dim objShell :Set objShell = CreateObject("Wscript.Shell")Dim strPath :strPath = objShell.CurrentDirectoryDim strConfig :strConfig = "\Settings\ProgramConfig.ini"Dim strINI :strINI = strPath & strConfig MsgBox (strINI)

If the script is in "F:\OEM\RunOnce\Scripts\" then strINI will equal "F:\OEM\RunOnce\Settings\ProgramConfig.ini"

And are you trying to use VBScript or JavaScript? The above is for VBScript, but similar logic applies to JavaScript.

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

@Outbreaker

Perhaps you could figure out the correct Cmd that you need using this example

<!-- Hta And Script By Gunsmokingman Aka Jake1Eye Hta And Script By Gunsmokingman Aka Jake1Eye This code is property of Gunsmokingman and Or Jake1Eye and you must have his permission to use. This is only posted as example code and meant only to used as such.--><SCRIPT LANGUAGE='JScript'> var w=375; h=175  window.resizeTo(w,h)  window.moveTo(screen.availWidth/2-(w/2),screen.availHeight/2-(h/2))  var Act = new ActiveXObject("WScript.Shell");  window.onload=function(){document.title='Demo CMD Script HTA'}</SCRIPT><BODY Scroll='No'><Input Type="CheckBox" Name='TestMe'   OnClick="if(TestMe.checked==true){Act.Run('%Comspec% /c @Echo Off && MODE 75,7 &&'+  'CLS && COLOR 9f && TITLE Demo Cmd Jscript HTA && Set CD=CD && Echo. && Echo %CD% && '+  'ping -n 4 127.0.0.1>nul',1,true) }" Value="Test">Demo Checkbox</BODY>
post-5386-0-92818000-1390082236_thumb.pn
Link to comment
Share on other sites

Thanks for the help but i have problems to implement this examples into the Script i'm using.

Here is a trimmed down version of the script i use, the idea i had was to use for example a command like (+basepath+) that would print out the Script current working directory.

          value='"..\Programs\Mozilla Firefox v26.0.exe" /INI=(+basepath+)\..\Settings\Config.ini' checked></input>
<!--This code is property of Gunsmokingman and Or Jake1Eye and you must have his permission to use.This is only posted as example code and meant only to used as such.--><TITLE> « Test Script » </TITLE><HTA:APPLICATION  ID="TESTSCRIPT"  SCROLL="no"  SCROLLFLAT="no"  SINGLEINSTANCE="yes"  SHOWINTASKBAR="yes"  SYAMENU="yes"  MAXIMIZEBUTTON="no"  MINIMIZEBUTTON="yes"  CONTEXTMENU="no"  NAVIGABLE="no"  BORDER="thin"  BORDERSTYLE="normal"  INNERBORDER="no"  CAPTION="yes"  WINDOWSTATE="normal"  APPLICATIONNAME="Test_Script"  ICON="%SystemRoot%\explorer.exe"><script language="JavaScript">//-> Resize And Place In Approx Centerwindow.resizeTo(300,160)window.moveTo(screen.availWidth/2-(300/2),screen.availHeight/2-(160/2))//-> Objects For Runtimevar Act = new ActiveXObject("Wscript.Shell");var fso = new ActiveXObject("Scripting.FileSystemObject");var Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2");//-> Runtime Variblevar c34 = String.fromCharCode(34);var HtaPath = TESTSCRIPT.commandLine.replace(c34,"");var basepath = new Array;//-> Body OnLoad Functionwindow.onload=function() {  basepath = HtaPath.split('\\');  basepath.pop();  basepath = basepath.join('\\');  Act.CurrentDirectory = basepath;}//-> Process Only Checked Checkboxesfunction submit() {  for (var i = 0; i < cbo.length; i++) {    if (cbo[i].value==null==false && cbo[i].checked) {      try {        Act.Run(Act.ExpandEnvironmentStrings(cbo[i].value),1,true);      }      catch(e) {        /*Error Code For Selected But Could Not Run*/;      }    }  }  window.close();}//-> Close HTAfunction closehta() {  var Q=Wmi.ExecQuery("SELECT * FROM Win32_Process")  for (var e=new Enumerator(Q); !e.atEnd(); e.moveNext()) {    var p = e.item();    if (p.CommandLine==null==false && p.name =="mshta.exe") {      var a = p.commandLine.replace('"',"")      var b = p.ExecutablePath + HtaPath      if (a.length==b.length+3==true) {         TKill(p.ProcessId);      }    }      }}//-> Kills ProcessIDfunction TKill(K) {  var Q=Wmi.ExecQuery("SELECT * FROM Win32_Process Where ProcessID='"+K+"'")  for (var e=new Enumerator(Q); !e.atEnd(); e.moveNext()) {    var p = e.item();    p.Terminate();  }}</script><body onbeforeunload="closehta()"><table width="100%" align="center" border="1">  <td valign="top">    <table>      <tr>        <td class="c1">Demo</td>        <td><input type="checkbox" id="cbo" name="checkbox1"          value='"%WinDir%\system32\calc.exe"' checked></input>        </td>      </tr>      <tr>        <td class="c1">INI Config file path Problem</td>        <td><input type="checkbox" id="cbo" name="checkbox2"          value='"..\Programs\File.exe" /INI=(+basepath+)\..\Settings\Config.ini' checked></input>        </td>      </tr>    </table>  </td></table><table width="60%" align="center" style="margin:8px 0px 0px 0px;">  <tr>    <td align="center"><button type="button" id="btc" Style="width:100px;" onclick="submit()">Submit</button></td>    <td align="center"><button type="button" id="btc" Style="width:100px;" onclick="closehta()">Quit</button></td>  </tr></table></body>
Edited by Outbreaker
Link to comment
Share on other sites

Try replacing your "onload" (for this case it's not needed), and "submit" functions with this and see if it gives you any ideas:

//-> Body OnLoad Function -- not needed//-> Process Only Checked Checkboxesfunction submit() {  for (var i = 0; i < cbo.length; i++) {    if (cbo[i].value==null==false && cbo[i].checked) {      try {        Act.Run(cbo[i].value,1,true);      }      catch(e) {        /*Error Code For Selected But Could Not Run*/;        alert(cbo[i].value.replace("(+basepath+)",Act.CurrentDirectory));      }    }  }  window.close();}
Edited by bphlpt
Link to comment
Share on other sites

It works but thier is a little problem it replaces "(+basepath+)" only once in a entry VALUE.

I did try to change 'replace' to 'replaceAll' but that didn't work at all. :(

Example .hta Script:

<!--This code is property of Gunsmokingman and Or Jake1Eye and you must have his permission to use.This is only posted as example code and meant only to used as such.--><TITLE> « Test Script » </TITLE><HTA:APPLICATION  ID="TESTSCRIPT"  SCROLL="no"  SCROLLFLAT="no"  SINGLEINSTANCE="yes"  SHOWINTASKBAR="yes"  SYAMENU="yes"  MAXIMIZEBUTTON="no"  MINIMIZEBUTTON="yes"  CONTEXTMENU="no"  NAVIGABLE="no"  BORDER="thin"  BORDERSTYLE="normal"  INNERBORDER="no"  CAPTION="yes"  WINDOWSTATE="normal"  APPLICATIONNAME="Test_Script"  ICON="%SystemRoot%\explorer.exe"><script language="JavaScript">//-> Resize And Place In Approx Centerwindow.resizeTo(300,160)window.moveTo(screen.availWidth/2-(300/2),screen.availHeight/2-(160/2))//-> Objects For Runtimevar Act = new ActiveXObject("Wscript.Shell");var fso = new ActiveXObject("Scripting.FileSystemObject");var Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2");//-> Runtime Variblevar c34 = String.fromCharCode(34);var HtaPath = TESTSCRIPT.commandLine.replace(c34,"");var basepath = new Array;//-> Body OnLoad Functionwindow.onload=function() {  basepath = HtaPath.split('\\');  basepath.pop();  basepath = basepath.join('\\');}//-> Process Only Checked Checkboxesfunction submit() {  for (var i = 0; i < cbo.length; i++) {    if (cbo[i].value==null==false && cbo[i].checked) {      try {        alert(cbo[i].value.replace("(+basepath+)",Act.CurrentDirectory),1,true);      }      catch(e) {        /*Error Code For Selected But Could Not Run*/;      }    }  }  window.close();}//-> Close HTAfunction closehta() {  var Q=Wmi.ExecQuery("SELECT * FROM Win32_Process")  for (var e=new Enumerator(Q); !e.atEnd(); e.moveNext()) {    var p = e.item();    if (p.CommandLine==null==false && p.name =="mshta.exe") {      var a = p.commandLine.replace('"',"")      var b = p.ExecutablePath + HtaPath      if (a.length==b.length+3==true) {         TKill(p.ProcessId);      }    }      }}//-> Kills ProcessIDfunction TKill(K) {  var Q=Wmi.ExecQuery("SELECT * FROM Win32_Process Where ProcessID='"+K+"'")  for (var e=new Enumerator(Q); !e.atEnd(); e.moveNext()) {    var p = e.item();    p.Terminate();  }}</script><body onbeforeunload="closehta()"><table width="100%" align="center" border="1">  <td valign="top">    <table>      <tr>        <td class="c1">Demo</td>        <td><input type="checkbox" id="cbo" name="checkbox1"          value='"%WinDir%\system32\calc.exe"' checked></input>        </td>      </tr>      <tr>        <td class="c1">INI Config file path Problem</td>        <td><input type="checkbox" id="cbo" name="checkbox2"          value='"(+basepath+)\..\Programs\File.exe" /INI=(+basepath+)\..\Settings\Config.ini' checked></input>        </td>      </tr>    </table>  </td></table><table width="60%" align="center" style="margin:8px 0px 0px 0px;">  <tr>    <td align="center"><button type="button" id="btc" Style="width:100px;" onclick="submit()">Submit</button></td>    <td align="center"><button type="button" id="btc" Style="width:100px;" onclick="closehta()">Quit</button></td>  </tr></table></body>
Edited by Outbreaker
Link to comment
Share on other sites

What!?! No Thanks, just complaints?

It works but thier is a little problem it replaces "(+basepath+)" only once in a entry VALUE.

Of course, that is what "replace" does. In your code there WAS only one occurrence of (+basepath+) in the value.

I did try to change 'replace' to 'replaceAll' but that didn't work at all. :(

AFAIK, replaceAll doesn't exist in JavaScript unless you write the function yourself. Outbreaker, do you do any research for yourself, or do you just blindly copy code fragments then ask others to fix it for you, and if that fails just make stuff up as you go along?

Unless you know something I don't, and can prove it, then GET RID OF THIS, ie DELETE IT:

//-> Body OnLoad Functionwindow.onload=function() {  basepath = HtaPath.split('\\');  basepath.pop();  basepath = basepath.join('\\');}

You can then change all occurrences of -- (+basepath+) -- to -- zzz --- or --- OutbreakerRules --- or --- NoHeDoesn't --- or --- AnyStringYouWant --- since it is just a placeholder. :)

As to replacing all occurrences in a string, there are many ways to do it. Here is one. Replace:

alert(cbo.value.replace("(+basepath+)",Act.CurrentDirectory),1,true);

with:

alert(cbo.value.split("(+basepath+)").join(Act.CurrentDirectory));

After painfully remembering previous times I tried to help you with code, I think I'm done with this for awhile. I will be very curious to see what you are able to come up with as a final solution that works for you. Good luck.

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

Ahh thanks, that is what i was looking for now the HTA Script is now working excellent. :) And the 'replaceAll' commend i found in google but i think that one was for jQuery or JaveScript. :crazy:

I'm sorry for all the noob questions but i'm really not a coder unfortunately i had to use a HTA Script for my little free project there was no way around it. :}

Edited by Outbreaker
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...