pushd not working in batch file but is in command prompt? - windows

I'm trying to make a scheduled task that runs a python script in a particular directory located on a network drive. I figured I need a batch file in order to achieve this.
I'm running the following in command prompt successfully:
pushd \\mydrive\somedirectory
Z:\>
But I'm trying to do the same thing in a batch file and get:
The network name cannot be found,
The system cannot find the path specified.
What might I be doing incorrectly?

Related

When creating a RAR file through CMD am getting "Access is denied"

I have a set of files in my system which am creating a RAR file by using a task scheduler and executing a bat file.
This is my bat file
TITLE Covert_To_war
cd D:\RarFolder\Current
"C:\Program Files\WinRAR\Rar.exe" a -ep1 -idq -r -y "Current" "E:\Folder1\backup"
When I execute this bat file alone this function works properly and the Rar file is created. However when I run this from a task scheduler. This throws off an error
Cannot create Current.rar
Access is denied
When I tried to compare the two procedures I found out that the first line in the bat file
cd D:\RarFolder\Current
works when I execute it alone as a bat file. However, when I run it from a task scheduler that line is not executing and the directory hasn't been changed. Can anyone help me out here? Why is the Cd command not working when I execute it from a task scheduler and why does it say Cannot create Current.rar and access is denied.
enter code here
You need to list your Task Scheduler settings. If they are anything other than Only Run When User Logged On then the script won't have access to mapped drives by drive letter as drive letters are mapped by security token. You may use UNC \\servername\sharename\folder\file.ext.
I figured this one out. In the windows task scheduler under actions, I just configured the "start in" field to point out the directory I want the operations to be done.
I had got the access denied error because the first line "CD.." command hadn't been carried out.

How to set a go to a directory and execute a command in a batch file?

During the deployment of the windows application that I make, I have to run a few commands in the CommandPrompt to update the database. I was thinking of making a batch file to automate this process so that the user can update the database just by double-clicking the batch file. Following is the screenshot for the same.
I want my batch file to go to the current directory where the .bat file is located.
Then i want to execute the DBUpdater exe file against the config file.
I am new to batch programming so anything informative will help a lot. Thank you.
Create a batch file named DBUpdater.bat in any directory of your choice with the below script:
#ECHO OFF
setlocal
cd /d %~dp0
DBUpdater.v15.2.exe OfficeManagement.win.exe.config
This assumes that the OfficeManagement.win.exe.config file exists in the same directory as batch file and that DBUpdater.v15.2.exe is recognized as a global command.

task scheduler - batch file - path

I have a batch file stored on my work network. When i go manually on the target folder and open my batch file it works just fine.
Thus I wanted to put this command in my task scheduler, went on it and for its action, browsed my file, got a path Y:\ and such
However when i launch my scheduled task, i have a pop up saying windows doesn't find my bat file and my cmd writes "the path can not be found"
I believe that is because with cmd it starts naturally on the C disk, thus as it is not on the right disk, the path is not found.
A pushd to change disk from C to Y works afterwards, however i have to do that manually on cmd
My question is, how can i handle my task scheduler to launch my file without having to create a copy of my file on the local disk? I would really like it to launch the bat file stored on work network.
Thought at first to write on my bat file the pushd line but well, the file is still stored on the network haha..
Thanks for your help
Run a local batch file to map the network drive as Y
net use y: \\server\blah
Then have it call your network batch file at Y:
call y:\mybatch.bat
As task scheduler runs normally as a different user it doesn't have your drive mappings.
Alternatively call the batch file via it's unc path \\server\blah\mybatch.bat and then on the first line of the batch file pushd the unc path again
pushd \\server\blah\

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.

Windows 7 Command Prompt: How do I execute a batch script from the command line?

I'm using Windows 7, and my problem is running this file from a console (cmd.exe):
W:\software\projects\myproject\build\msvc\build.bat
When I move into the folder containing the file manually and run it from there using the following command sequence, it works:
W:\>cd software
W:\software>cd projects
W:\software\projects>cd myproject
W:\software\projects\myproject>cd build
W:\software\projects\myproject\build>cd msvc
W:\software\projects\myproject\build\msvc>build.bat
However, when I try to run the file from the root directory in any of these ways:
W:\>software\projects\myproject\build\msvc\build.bat
W:\>call software\projects\myproject\build\msvc\build.bat
W:\>#call software\projects\myproject\build\msvc\build.bat
W:\>"software\projects\myproject\build\msvc\build.bat"
W:\>call "software\projects\myproject\build\msvc\build.bat"
W:\>#call "software\projects\myproject\build\msvc\build.bat"
I get the following error message:
The system cannot find the path specified.
I'm pretty sure you didn't have to navigate to the folder containing the file in order to run it when I was using Windows XP (though I could be wrong, of course), but this apparently seems to be the case with Windows 7. Or am I missing something?
You are correct. You do not need to navigate to the batch scripts folder before executing.
The error "The system cannot find the path specified." is most likely caused by something inside your batch-file.
Try to add
cd W:\software\projects\myproject\build\msvc
w:
or in a single command (as suggested by James K, Thanks!)
cd /d W:\software\projects\myproject\build\msvc
Searched a bit more and found this generic solution:
cd /d %~dp0
at the top of your batch file to set the working directory to the directory of the script to check whether this is the cause.
If you execute your file from W:\ this is where the commands are executed (working directory). It is most likely that your script cannot find some file it uses in this location.

Resources