Move command in VBScript with a space in destination directory [duplicate] - vbscript

This question already has an answer here:
VBScript pass commandline argument in paths with spaces
(1 answer)
Closed 5 years ago.
Set oShell = WScript.CreateObject("WSCript.shell")
oShell.run "cmd /K cd \ & cd C:\Users\me & cscript /nologo Setup.vbs > newfile & del example.ini & ren newfile example.ini & move example.ini C:\Program Files (x86)\Setup Folder"
Above is my code in which I'm trying to run a script and replace a file in the destination directory. My other script works fine and isn't causing any issues, but in this script I cannot seem to move the file to the destination (C:\Program Files (x86)\Setup Folder) because of the spaces in the directories.
I have tried using /, "'s (this one will not work I realize as it will read the script and believe it to be the end of it), and single quotes ('), before each space of the directory but none of them work.
How would one be able to move a file from one place to the other while still in the script if the directories have spaces in them?

I found the answer to my own question through a bit more research.
The reason it was not working is because I didn't add a set of double quotes before my destination directory. By doing
""C:\Program Files (x86)\Setup Folder"
It worked. I'll leave this here for anybody who encounters this issue in the future.

Related

Cmd file to run executable in a folder of an archived zip file

I have two files which I want to archive. I.e a zip file. Files contained in the archive would be a cmd file and a folder containing the file to run.
I want a cmd script to use to run the file in the folder when the archive file is opened and the cmd file is runned. Would prefer cmd to run silently with no pop up screen.
Can anyone help with the cmd script here?
Assuming that two (2) files are extracted into a directory.
RUNME.CMD
DOIT.EXE
The RUNME.CMD, if it is run, can refer to the directory where it is located.
#ECHO OFF
ECHO My directory is "%~dp0"
IF EXIST "%~dp0DOIT.EXE" ("%~dp0DOIT.EXE")
EXIT /B
Assuming that two (2) files are extracted into a directory.
RUNME.CMD
FOLDER with DOIT.EXE
The RUNME.CMD, if it is run, can refer to the directory where it is located.
#ECHO OFF
ECHO My directory is "%~dp0FOLDER"
IF EXIST "%~dp0FOLDER\DOIT.EXE" ("%~dp0FOLDER\DOIT.EXE")
EXIT /B
This is the Vbs script to use in running this file.
Run.vbs
file.exe
Run.vbs script should be like this.
dim shell
set shell=createobject("wscript.shell")
shell.run "file.exe"
set wshNetwork = CreateObject( "WScript.Network" )`enter code here`
strUserName = wshNetwork.UserName
WScript.Sleep (10000)
X=MSGBOX("File runs successfully" ,0, "Alert For: " & strUserName)
WScript.Interactive = true
Line 4 - 7 ain't compulsory for this task. Its just to locate the Computer Username and display on screen.

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.

.BAT file help (uninstall 2 paths silently)

I have looked around and can't seem to make it work with the research I've done.
I'm going to create a GPO to apply to workstations that will uninstall Malwarebytes 2.0 and 3.0 from a given system. This will allow us to roll out the enterprise version.
What I have in my .bat file is this:
#echo off
cd "C:\Program Files (x86)\Malwarebytes Anti-malware\"
unins000.exe /verysilent /suppressmsgboxes /norestart
cd "C:\Program Files\Malwarebytes\Anti-Malware\"
unins000.exe /verysilent /suppressmsgboxes /norestart
However, I noticed that if one of the paths above doesn't exits (1 will always not exist) than it will pop up a command prompt with an error. I'd like no boxes to pop up at all if possible. I then tried to accomplish this with vbs with the error:
compilation error: Invalid character
This is that script:
Dim objShell
Set objShell = WScript.CreateObject( WScript.Shell )
Sub MalwareBytes()
On Error Resume Next
objShell.Run(%ProgramFiles%Malwarebytes Anti-malwareunins000.exe verysilent
suppressmsgboxes norestart)
objShell.Run(%ProgramFiles(x86)%MalwarebytesAnti-Malwareunins000.exe
verysilent suppressmsgboxes norestart)
End Sub
Set objShell = Nothing
How about, y'know, checking if a path actually exists before trying to go there?
if exist "C:\Program Files (x86)\Malwarebytes Anti-malware" (
cd "C:\Program Files (x86)\Malwarebytes Anti-malware"
unins000.exe /verysilent /suppressmsgboxes /norestart
)
The reason why your VBScript doesn't work is because your syntax is invalid. You need double quotes around the argument to CreateObject() as well as the command strings. With nested double quotes in case of the latter, because you have paths with spaces in them. Not to mention that it would be cleaner to check if a path actually exists in VBScript too.

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

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.

Why is the following multiple DOS commands in one line not working correctly? (Passing Values)

I am trying to create a text file via DOS commands. The commands asks one for the name of the file prior to creating it. I have looked here and here and elsewhere to get me started.
I would like the code to work in one line. So, I should be able to type the entire code in Windows Start > Run box.
This is what I have:
cmd /k #ECHO OFF & SET /P filename=What File name: & copy NUL %filename%.txt & :End
This however ignores the name of the file I gave when asked, and creates %filename%.txt.
I have tried changing the operator before the word copy to |, &&, and & but these don't even ask me for a file name and simply create %filename%.txt
Also, the cmd box stays open after the text file is created.
P.S. I know I can use /q before /k for echo off.
I look forward to your help.
This works here: delayed expansion is used in another cmd process
cmd /c #ECHO OFF & SET /P filename=What File name: & cmd /v /c copy NUL !filename!.txt & exit
The command line has no path defined for the directory and when using the RUN box it will probably be created in the c:\windows\system32 folder, except you will not have write access to that folder.

Resources