Running a batch file in a given directory using VBA - windows

When I go to my batch file's location and open it, the batch file works. My batch file is simply:
cd .\data
dir/b/o:n > names.txt
As you can see, I'm in my current directory and moving down to the sub directory "data" and coping all the names and creating a file called names.txt.
When I say
shell "location of file"
it opens the batch file, but the directory that is defaulted to is C:\my documents, so my commands won't work because it cannot find the sub directory. I want this to be a dynamic batch file, and therefore i need to write something in VBA that will open the batch file under its current directory or something to this effect.
How do I do this?

The following should give you the effect you seek.
My test code is:
Option Explicit
Sub TryShell()
Dim PathCrnt As String
PathCrnt = ActiveWorkbook.Path
Call Shell(PathCrnt & "\TryShell.bat " & PathCrnt)
End Sub
My test batch file is named TryShell.bat and contains:
cd %1
dir *.* >TryShell.txt
I have placed my batch file in the same folder as the workbook containing my macro.
The statement PathCrnt = ActiveWorkbook.Path sets PathCrnt to the name of the directory containing the active workbook. You can set PathCrnt to whatever directory you require.
When I call Shell, I have added PathCrnt as a parameter.
In my batch file, I set the current directory to %1 which is the first parameter.
The dir command works as I wish because the current directory is my directory and not the system default directory.
Hope this is clear.

C:\My Documents is probably the directory where your speadsheet is located. If you add
ChDir "C:\TheFolderWhereYourBatchIs"
before launching your Shell command and that should work...
Alternatively, you could change your batch file to use an absolute directory instead of a relative one.

Related

Get the filename of a running batch file

I am trying to make a batch file that will write the name we names it.
For example if i names the file "test1" and start it it will change the title only to "test1" If i change the name to "bat file" it will display "bat file" in the title.
Is it possible to make a file that will write to a .txt file with the same name as the bat file? I am trying to make a batch file that will log sometings and write it too ".txt" where is the name of the bat file without extension
In batch files %0 stores a reference to the current batch file. The exact format of the value depends on how the file is invoked, but there are a serie of modifiers that can be used to retrieve the needed information (You can see the documentation or execute for /?, as the help for this command also includes the full list of modifiers)
#echo off
title %~n0
echo I am %~n0
> "%~dpn0.txt" echo This goes to the file
Where
%~n0 = name of the current batch file (without extension)
%~dpn0 = drive, path and file name (without extension) of the current batch file
echo %~f0 for writing your batch file name. Use ">" to write anything to a file. For eg use echo "write this" > file.txt

Calling a batch file from another batch file in different directory - resources not found

I'm working with installshield and have a group of batch files that I want to run as part of the install process. Instead of executing each batch file from installshield I want to create one batch file that executes all of the batch files.
The issue I have is that the calling batch file sits two directories up from the others. When the batch file tries to call the others they fail to run because they can not find the resources that they need. It seems that when they are executed from the batch file two directories up they are for some reason using the relative path of the calling batch file. Is my assumption correct?
One of the batch files that I am calling is a batch file to star an h2 database the call looks like this:
call h2\bin\h2.bat
If I go to the /h2/bin directory in a command prompt the h2.bat runs fine but once I run it from the calling batch file this is the error that I get.
Error: Could not find or load main class org.h2.tools.Console
How do I call one batch file from another without using the calling batch files path?
Explanation
It seems that when they are executed from the batch file two
directories up they are for some reason using the relative path of the
calling batch file. Is my assumption correct?
Yes your assumption is correct. Calling a batch file will not change the current working directory. The main batch file will be found because you are providing the correct relative path, but all the other relative paths will be seen from the perspective of your current working directory, not from the directory that contains the main batch file.
%~dp0 is your friend, it yields the drive letter and path to the batch file containing that character sequence. Use it as a basis for relative paths and your batch files will work no matter who calls them from where.
Example:
Fictitious h2.bat that won't work:
#echo off
h2.exe start
Working h2.bat:
#echo off
"%~dp0\h2.exe" start
See What does %~dp0 mean, and how does it work? for more explanations on %~dp0
Try setting the directory:
cd ht\bin\
call h2.bat
cd %HOMEPATH%
REM just reset to where ever you were before.
If that doesn't work, try using the C:// prefix in your path. That might/might not work.
Good Luck!
Suppose current .bat file is running in C drive and you want to run .bat file placed in D: directory then in first .bat write.
D:
cd "D:/folder/folder2/"
call batFile.bat
It might be because you don't have permission. M facing the same problem and i found the solution like this -
Right click on your task than properties.
In properties click on General tab and then click on 'User Group or User' and select appropriate user.
Or create a another bat file to call your bat file and schedule that file. you can create the bat file like this -
open Notepad and give your original bat file path and then call bat file with name like -
D:
cd "E:/ABC/FirstJob/main/"
call main_run.bat
Now save this file with .bat extension.
if your bat file is correct, try cmd command as below and hit enter(tried in windows 10):
"\h2.bat"
e.g: "C:\Users..\bin\h2.bat"
I tried :
pushd h2\bin\
call h2.bat
=> It 's okay.

Using drag and drop from another directory onto a batch file does not work

I have a folder structure that looks like this:
project
bin
my_program.exe
misc_stuff.exe
DROP_OVER_ME.bat
input_file.txt
Basically, I want to be able to drag and drop the input file on top of the DROP_OVER_ME.bat batch file and have it pass on the path of the input file to the exe.
This is what my batch file looks like:
#echo off
start bin/my_program.exe %1
exit
When I drag and drop input_file.txt over the batch file, everything works fine -- my_program.exe successfully receives the path of the input file and runs.
However, when input_file.txt is located outside the project folder, dragging and dropping it makes the batch file throw up a popup message saying
Windows cannot find 'bin/my_program.exe'. Make sure you typed the name correctly, and then try again.
How can I fix my batch file so I can drag and drop files from any arbitrary location inside my filesystem?
Sounds like the batch script is basing the current working directory as the directory you're dragging from, and not the directory containing the script. (You can test this by adding an echo %cd% && pause to your script if you wish.) Try modifying your script as follows to remove any ambiguity about file paths:
#echo off
cd /d "%~dp0"
start "" "bin\my_program.exe" "%~f1"
exit /b

Using a shortcut or batch file to move file to a relative location

I'm trying to create a shortcutor batch file to sit in the windows'send to' menu which sends the file to a location relative to the current file path of the file.
So for example I'm looking to create an archive shortcut to stick in the ocntext menu that would move files as follows
C:\cheese\stilton.txt > C:\cheese\archive\stilton.txt
or
C:\biscuits\hobnobs.txt > C:\biscuits\archive\hobnobs.txt
But I don't understand how to capture the current file path and pass it to the batch\shortcut.
Can anyone help?
Funnily enough, windows relative path does not differentiate between file & directory, when using ...
Simply, move "%1" "%1"\..\archive also works. ;-) Though not a clean way. :-)
Also make sure that you do mkdir "%1"\..\archive before move.
Also, instead of send-to, you may think of adding it to registry; specific to a file type.
For text files, .reg file will look as below:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\txtfile\shell\movefile]
#="Move to archive"
[HKEY_CLASSES_ROOT\txtfile\shell\movefile\command]
#="cmd /c mkdir \"%1\"\\..\\archive & move \"%1\" \"%1\"\\..\\archive "
In a batch file you can get the path of the first parameter with %~dp1, or just the path with %~p1. This means that you would want to pass the full filename with path to the batch file.
To use this in a batch file, for example to move a file, you could do the following:
#echo off
setlocal enabledelayedexpansion
set fn=%~1
move "!!fn!!" "%~p1archive"
The %~1 removes quotes from the filename, and I just do that out of habit.
The %~p1 expands to the path of the file, with a slash appended.
Note that I haven't included here checking that the directory exists, which should be simple enough... add this line before the move to create it:
md "%~p1archive"

help with windows batch scripting basics - execution and calling a separate executable within the script

Newbie to windows scripting. I need help running the .bat file on the command line so I can test it.
I used Text Document as my editor to create the file (opens up also as Notepad).
I performed file "save as" (ALL FILES). If I open up cmd, I can see the file has a .txt extension (myfile.bat.txt). So if I just type in cmd myfile.bat.txt the editor opens. I am not sure how to execute this correctly.
As for the logic in my batch script, I am basically logging into a remote directory (already created the net mount) and now I want to:
run an executeable file
rename some files.
With some research, I written this so far. I have saved it as a .bat file
# echo off
echo This is a batch file to run an executable and rename some files
pause
--run executable file here, just don't know how to do it
x:
cd x:
rename fileA fileB
Any help, good tips/practice would be great. Thanks.
Type in this command in cmd window:
rename myfile.bat.txt myfile.bat
Now you can run the script by simply invoking:
myfile.bat
or
myfile
(provided there's no myfile.exe or myfile.com in the same directory).
If you need to edit the script further, you can either right click it in Explorer and choose Edit or call the editor from the command window:
notepad myfile.bat
To call a program from the script, simply add its name, if it's in the current directory:
someprogram.exe
or the name with the path, if it's somewhere else:
directory\program.exe
or
d:\directory\program.exe
If the name or the path contain spaces, be sure to enclose the entire name & path string in double quotes:
"d:\directory\program name.exe"
you can just type the full name of the program
eg
"c:\program dir\program.exe"
or you can add the program directory to your path environment variable
set PATH=%PATH%;"c:\program dir"
and just type the program name
program
you can also edit your PATH variable in windows http://support.microsoft.com/kb/310519
NOTE: When you save the file in notepad, you want to save it as filename.BAT and select All Files from the second dropdown. If you don't it still gets saved as a .TXT.
A couple of command to consider:
CSCRIPT cscript /? in CMD
START http://ss64.com/nt/start.html
If you're doing say a VBSCRIPT use CSCRIPT to start it. If you're trying to execute another BATCH script or an EXE, use START

Resources