VBScript is running batch file from the file path of VBScript, not path of batch - windows

I am having a problem with a VBScript which was working just fine yesterday, but for some reason does not work today. I am attempting to run a batch file off the sever, from a VBScript in an adjacent server folder. The VBScript reads like this:
Dim Shell
Set Shell = CreateObject ("WScript.Shell")
Shell.run "cmd /K ..\Analyses\GeniE_SP.bat"
The VBScript is in a file in the folder ..\Local Design Calculations, whereas the batch file is in ..\Analyses. The script is calling up the batch file just fine, but when the batch file runs it is running with the file path of the VBScript. This is what is shown in the command window:
I:\Shared\Projects\105874\Local Design Calculations>REM #echo off
If I simply run the batch file from its actual location, there are no problems as the first line in the command window will read like this (as it should):
I:\Shared\Projects\105874\Analyses>REM #echo off
Any ideas?

Shell.run "cmd /K cd ..\Analyses & GeniE_SP.bat"
If the current active directory for the batch file is a requirement, first change it.

You would specify the full path to what you want. There in no such thing as per drive default directories in WinNT. Graphical programs (ie normal) set their own default directory based on the folder the exe is in (and that's not a per drive default but a program default).
For console programs (ie non graphical) cmd.exe simulates per drive default directories.
SPECIFY FULL PATHS.

Related

Keep cmd.exe console window with specific title open?

in Windows 7 x64 SP1 I need to create a batch file (.bat) which keeps the cmd.exe console window open and has a specific title:
#ECHO OFF
title notepadtest
#ECHO ON
start "" /WAIT notepad
But this batch file keeps opening an infinite number of cmd.exe console windows in an unstoppable loop!
How can I create a batch file (.bat) which creates only ONE cmd.exe console window and keeps it open and has a specific title?
Please ensure that your batchfile is not named notepad.cmd or notepad.bat or anything like any system or external command. Name it something like mynotepad.cmd instead., then try this one please:
#echo off
title notepadtest
start "" /w notepad.exe
cmdline and batch files typically works like this. When a command is issued, it first checks the local path, where the script was launched from for the command, if not found, it will check your environment and system environment. So if you name a batchfile notepad.bat your batch is actually starting itself over and over, instead of finding notepad.exe in the environment variables.
Always name batch files something unique and not system/external command related.
Always use full executable name in a batch, like start "" /w notepad.exe and not start "" /w notepad

Windows context menu run hidden xcopy

I am trying to add a new option to the context menu for folders in Windows. I have managed to add the option and specify its command as follows:
xcopy.exe "%0\*" "c:\Destination\" /EHY
This code is added to regedit.exe
I have a folder in the c: drive named Destination. I am trying to copy the folder that I right clicked to the Destination folder, without a command prompt window.
What is happening: xcopy is running and copying the content of the folder and in the foreground. Please help me with these two issues:
Run the xcopy command without showing a window.
Copy the folder to a new folder in Destination named after the copied folder.
Thank you.
The command that satisfies the two issues listed is at the very end. First, some notes of explanation.
When you add a shell command to the Windows Registry, you have several variables available to you (such as %1, %L, and %V). Now, you would like a new folder in Destination named after the copied folder. Parameter extensions (such as %~n1) can strip everything from the full path and give you the name of the directory leaf.
However, these are not available when using the shell command from the Windows Registry. The most straightforward way to get a plain directory name is to create a temporary batch script, run it, and delete the batch script afterwards.
The following will copy the selected directory as a sub-directory inside Destination:
cmd.exe /c echo #echo off>"C:\Destination\_tempxcopy.bat" & echo xcopy "%%~1\*" "C:\Destination\%~n1" /ECIQHY ^>nul>>"C:\Destination\_tempxcopy.bat" & call "C:\Destination\_tempxcopy.bat" "%1" & del "C:\Destination\_tempxcopy.bat"
This next part requires the use of a third-party utility.
The previous command will open a command window and leave it open as long as copying is in progress. To hide that window, use the tiny utility RunHiddenConsole
The following will copy the selected directory and hide the command window while copying:
"C:\Destination\RunHiddenConsole.exe" cmd.exe /c echo #echo off>"C:\Destination\_tempxcopy.bat" & echo xcopy "%%~1\*" "C:\Destination\%~n1" /ECIQHY ^>nul>>"C:\Destination\_tempxcopy.bat" & "C:\Destination\RunHiddenConsole.exe" /w "C:\Destination\_tempxcopy.bat" "%1" & del "C:\Destination\_tempxcopy.bat"
This could certainly be made more flexible and efficient, but the above command at least demonstrates the technique for accomplishing the task.

Why cannot i use batch XCOPY while running in admin mode?

i have run very simple script:
xcopy some.exe c:\folder\ /h/y and it works normally. but when i try to run .bat file with this code as administrator - cmd line opens for a moment but nothing happens (file not copied). Can anyone explain this issue?
i also tried to use echo xcopy instead of xcopy, but nothing had changed.
i need only admin running of .bat file, cause i want to copy file in \windows\system32 folder
when you start a batchfile as administrator, it's working directory is C:\windows\system32\. So your script doesn't find your file. Either work with absolute paths, or change the working directory.
You can change it to the directory, where your batchfile resides with:
cd /d "%~dp0"
Note: to keep the window open to read any errormessages, append a pause command.

Unable to run vbs file from windows registry

I have a registry key that calls a vbscript file as below.It will be triggered on selecting a Change Cursor option from right click on .cur file
"C:\Program Files\Cursor Manager\CustomCursor.vbs" "%1" 2
Below is the vbs file(CustomCursor.vbs)
Set objArgs = WScript.Arguments ' Create object.
CusorValue=objArgs(1)
Path=objArgs(0)
MsgBox CusorValue
MsgBox Path
But its not working.Its not showing any message
Its working when i am calling the script from command prompt like below.
"C:\Program Files\Cursor Manager\CustomCursor.vbs" "E:\new\CM v5\cursors new\more\Arrow.cur" 2
If the registry value is batch file its working
"C:\Program Files\Cursor Manager\CustomCursor.cmd" "%1" 2
Below is batch file(CustomCursor.cmd)
echo %1
echo %2
pause
But i can use batch file as it will show a command window,as it will show command window while executing script.
Please tell me a way to execute the vbs file from registry or atleast a way to run batch file in background.
Thank you
Change your registry to
"%windir%\system32\wscript.exe" "C:\Program Files\Cursor Manager\CustomCursor.vbs" "%1" 2
The problem is that invoking directly the script file makes windows search for the program to execute it and call the asociated executable with the script as argument, but in this process the rest of the arguments are discarded. Call yourself the script host with all the needed arguments.

Run a script file as administrator

I have this .vbs script that I am trying to run on windows 7.
It has to run with full permissions and it has to do it automatically.
To be clear When the user double clicks on the file it will get the prompt that asks "to allow to run the file as an administrator", and then run with full permissions.
To do this I created a batch file (run.bat) that calls the script file
cscript "V02.vbs"
pause
then I created a shortcut for the batch file which I can choose to run as admin.
The problem I encounter now is that when I run the batch file as admin the folder changes to c\windows\system32. The script, batch file and shortcut are all in the same folder. is there a way to get the folder location?
I may have misunderstood...
The easiest solution would be to "hardcode" that path into the patch file with a -
cd \path\to\my\script
cscript myscript.vbs
pause
Use "modifiers with batch parameters". From the linked documentation:
%~dp1 Expands %1 to a drive letter and path.
Since %0 is your script, %~dp0 is what you want:
cscript "%~dp0V02.vbs"
pause
Note that %~dp0 includes the trailing backslash, so your v02.vbs file name above becomes quite hard to read.

Resources