Windows CMD script to get directory in which file exists - windows

I have a executable file VSTO.exe & a try.bat file in one folder. I want to get the folder path of the bat file & concat that path with VSTO.exe.
I have this script in my try.bat.
"%~fp0\VSTO.exe" /q
but it creates path: "G:\test\try.bat\VSTO.exe" . I want to get a path "G:\test\VSTO.exe"
can anyone tell me how do i do that?

"%~dp0\VSTO.exe" /q
is the exact answer.
How to get folder path from file path with CMD

Try with this
SET CURDIR=%CD%
"%CURDIR%\VSTO.EXE" /q
The %CD% pseudo-variable expands to the current working directory.
Type SET /? to see other pseudo-variable (last page)

Related

Copy folder to current path open in Command Prompt

I'm trying to create a batch file that copies one folder to the current path open in command prompt.
This is the code I have, but it doesn't work.
#echo off
xcopy /s c:\Users\Alexander\Documents\Other d:\%cd%
pause
Over complicating things...
Just use ".", which means current directory, i.e.:
xcopy /s c:\blah\blah .

Get a list of files present in a folder

I want the names of the files present in a folder . Is there any command in command prompt or any other way that can list all the file name(only file name and its extension) that are present in a particular folder?
Dir does what you want, you may want to add /B to only get filename and extension.
dir /B
Full options of dir can be viewed by
dir /?
Try dir /B that should give you the expected result.

Batch - Resolve relative path

My batch scripts are located in the following directory:
\\test\public\windows\scripts\32\test.bat
I'm trying to get the path of this folder: \test\public\windows\logs
I've tried using %~dp0 in order to get the path of the script, and i've tried:
SET REL_PATH=..\..\
The thing is that it's not showing me this folder.
SET REL_PATH=..\..\
path points to the path according to you, to verify where it is taking from, please make a
echo %CD%
it will tell you the path the system is looking at.
then set the relative path from %CD% to the path of your script.
Please comment if you get stuck after doing this.
For example:
echo %CD%
gives C:\windows\system32
and you want to execute batch file at c:\test\public\windows\scripts\32\test.bat
you will need to do
SET REL_PATH=%CD%\..\..\test\public\windows\scripts\32\
To move to this path do a:
cd /d %REL_PATH%
To solve UNC path do PUSHD and POPD:
#echo off
pushd \\test\public\windows\scripts\32\
REM do your work
call test.bat
popd
Reference: How to run batch file from network share without "UNC path are not supported" message?
Use the PUSHD command when working with network drives. This will assign an unused drive letter to the UNC path so you can navigate it like normal. Then you can use POPD to release it.
For example:
#ECHO OFF
PUSHD "\\test\public\windows\scripts\32"
REM This will be \\test\public\windows
DIR ".\..\..\"
POPD

Batch file: for parameters

I am having trouble understanding what does following windows batch file do, can somebody explain:
for /f %%i in ("%0") do set curpath=%%~dpi
cd /d %curpath%
/*Some other code...*/
cd /d %curpath%
%0 is the full path to the .bat file itself (if run from another directory) and ~dpi is a modifier to extract the drive and directory from a path omitting the file name, so this snippet sets the current drive & directory to the one in which the batch file lives.
I can't see the reason for using a FOR, %~dp0 does the same thing in one go.

How to fix this BATCH file so that when it is execute and find all the path correctly?

I wrote this script to first install the msi and then copy my application to a temporary directory. But none is working. When the windows.bat file is executed it failed to find package\ and also dist directory
1) User downloaded and execute the windows.bat file which has following tree:
C:\Users\Username\Downloads\windows.bat
C:\Users\Username\Downloads\package\<.msi files>
C:\Users\Username\Downloads\dist\<application files>
2) windows.bat contain below:
msiexec /I "package\files.msi" /qb
set temp=%TEMP%
echo %temp%
xcopy dist %temp% /e /h /R
All fails, to run with windows.bat file. What am i doing wrong?
You need to add the following line to the beginning of your batch file:
cd c:\users\username\downloads
alternatively, you can do this:
cd /d %~p0
%~p0 will take argument #0 (the full path to the batch file) and extract the path from it. The /d option will make sure to also change the current drive if the given path contains a drive specification.
Ok, think I know what's wrong. msiexec.exe runs from the Windows System folder (e.g. C:\Windows\Systeme32) so when you pass it the name of the msi file to install, you need to include the full path to it.
So, using #MikeNakis' info about getting the current path within the batch file, try this (slight tweak to use %~dp0 for just the directory, so not including the batch file name too):
msiexec /I "%~dp0\package\files.msi" /qb
Ensure you are in the correct directory to start off with
CD /d c:\users\%USERNAME%\downloads
As the first line in your batch file

Resources