task scheduler - batch file - path - windows

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\

Related

required operation requires elevation

i am trying to use this command in cmd
runas /user:Mymachine\kanhaji diskpart
but it say
RUNAS ERROR : unable to run -diskpart
740 : the requested operation requires elevation.
this is the only user on my system
and it is already an administrator
although going to C:/windows/System32/diskpart.exe -> right click -> run as administrator
works fine
but i just want it to do with cmd
i am creating a batch file which will simply hide my disks on all my systems after just running this batch files
#echo off
runas /user:Mymachine\Kanhaji diskpart
mypassword
select volume 3
remove letter f
i am going to access this batch file through telnet so i have to run it as administrator through command only
Firstly you need to disable UAC I am not sure which Windows version you are using, but here is a good start
Then running diskpart in a script might be simpler if you using it with the script option. so once UAC has been disabled, you can create a .cmd file and a script file.
the script file will contain your actual diskpart commands to perform for instance:
Create a Script file which we will call myDiskScript.txt with the content:
select disk 4
format quick fs=ntfs label="My Disk"
Then create a batch file called MyDiskBatch.cmd and insert the commands. i.e
diskpart /s f:\myDiskScript.txt
Ensure that you point the full path of the myDiskScript.txt in the batch file, else it will search for the script in the execution directory.
You can then run the file via scheduler or by manually execution.
The nice thing about this is that you can create a batch file that does some checks and create the myDiskScript.txt file based on the result of your initial tests, then execute it, making it fairly dynamic then.

File Move Scheduled Tasks not running bat file

I know this question might have been asked in other ways, but all the things I have read and tried had not yet fixed my problem, so I am hoping to get some help here with context to my issue.
The problem:
I need to move files from a local drive to a network drive (the network drive is a SharePoint mapped library) on my server in Windows Azure (don't think the azure part matters, but it provides context).
My thought was to schedule a task that will run a bat file to move the files I need moved and do so frequently (every 5 to 10 minutes). The batch file I have created does what I need it to when I manually run the batch file but not when the task runs it.
Here is the batch file:
echo Write log file > LogStart.txt
C:\Windows\System32\robocopy.exe "\\PCICSWKS001\D$\ToBeMoved" "V:" /s /e /MOV /r:0 /W:0
echo Write log file > LogEnd.txt
You can see that it writes a log file before and after running robocopy.exe.
When the task runs it does write both of these log files so I know that the batch file is at least running.
You can also see that I have tried using the UNC path for the drive in the source, that is because I was reading that the task scheduler might not be able to pick up properly on the drive letters. When I put the machine name in for the destination and run the batch file myself or with the scheduler it errors.
Here is the action taken by the task:
Here is the task general tab
Any assistance would be much appreciated.
I had that same error with Robocopy after a few times of running it:
ERROR 3 (0x00000003) Getting File System Type of Destination
I think it has to do with how robocopy scans the destination when it has a fair amount of files on it. It worked fine when I started the copy job and the Azure destination was empty.
Anyway, I think you should try to use the AzCopy command as that should be less error prone and faster since it was designed for this kind of thing. It's command line switches are similar to robocopy so it should feel pretty familiar.

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

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?

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.

Self executing delete files

How would I create a self executing batch file to delete files in a specific folder.
Scenario: I have a folder on a server where all the scannered documents go to once they have been scanned. They want a the scanned documents to be deleted after 1 day. Can a batch file be created to do that everyday?
You can use the built in task scheduler - this can call a batch file, or just about anything.
(I am assuming Windows, since you mention batch files).
This is quite a well known method, and was documented in MSDN some time ago. This technique works on both Windows 95 and Windows NT. It works because MS-DOS batch files are able to delete themselves. To test this technique, create a small batch file containing the single command:
del %0.bat
The batch file, when run, deletes itself and issues an error "The batch file cannot be found". This error is just a simple message, so it can be safely ignored. By itself this isn't too useful, but when modified to delete our executable it solves our problem, albeit in a rather forceful manner. Our executable will create a batch file (called C:\DelUs.bat) with the following content:
:Repeat
del "C:\MYDIR\MYPROG.EXE"
if exist "MYPROG.EXE" goto Repeat
rmdir "C:\MYDIR"
del "\DelUS.bat"
This batch file repeatedly attempts to delete the specified file, and will run continuously consuming CPU until it succeeds. When the execuable has been deleted, the batch file then deletes itself.
The executable needs to spawn off the batch file using CreateProcess, and then should exit immediately. It would be a good idea to give the batch file's thread of execution a low priority so that it doesn't get much execution time until the original executable has terminated.
Read the entire article at http://www.catch22.net/tuts/self-deleting-executables that contains the full code to this technique.

Resources