Windows Batch File - Clearing another batch file - windows

quick explanation:
I have a batch file converting a series of swfs to mp4s which are placed into the batch file via PHP fwrite. This is ran every hour on windows Task Scheduler. For example:
C:
cd "path\Moyea\SWF to Video SDK"
swf2videocmd "conversion_command_here"
cd "path\MySQL\bin"
mysql --host=hostip --user=user_name --password=hostpw--database=database_name --table=table --execute="UPDATE table SET processed=1 WHERE processID=44;"
This works fine, updates the SQL and runs the process. There can be as many conversions in one batch file as necessary, they're added dynamically on demand. Windows Task Scheduler runs the batch fine aswell.
I then have a secondary batch file which Task Scheduler runs at the same time - clear.bat. This clears the first one so the conversion job can start afresh with the new files next time:
D:
cd "path\youtube\"
nul > swf2mp4.bat
It clears the first batch file fine, but returns a prompt saying Windows cannot access the specified device, path or file.
In windows Task Scheduler, it can't close this prompt so the task gets stuck running, meaning the next instance of the task (hourly) can't be run. I would make it automatically close each hour, but the conversion queue may be large. All I need to do is supress the error.

Replace
nul > swf2mp4.bat
with
type nul > swf2mp4.bat
and it should work fine.

Related

Windows Task Scheduler runs manually, fails in scheduler

I have been trying to make the scheduler work for the past two days.
I am trying to sync a folder from a remote server using WinSCP, however I do not want to keep WinSCP running continuously and am thus running it using a batch file. The batch file is below.
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="WinSCP_log.log" /ini=nul ^
/command ^
"open sftp://xxxxxxxxxxxxxxxxx -hostkey=""xxxxxxxxxxxxxxxxxxxxx""" ^
"cd /data/remotexxxxxx" ^
"lcd C:\localxxxxx" ^
"synchronize local" ^
It runs fine when I just run the batch file. When using the Windows Task Scheduler to run every 20 minutes it does not run. However, even in the Scheduler if I double click and run manually it works fine. I even tried converting the batch file to a .exe, but face the same issue there.
The Windows Task Scheduler settings are -
Program/Script: /Directory/sync_winscp.bat Arguments: Start in:
/Directory
And similarly for the .exe file
Program/Script: /Directory/sync_winscp.exe Arguments: Start in:
I also tried -
Program/Script: .../Win32/cmd.exe Arguments:
/Directory/sync_winscp.bat Start in:
It is running with highest privileges and does not have a timeout.
Update:
I also have another .bat file to call python and run a script daily which runs perfectly fine.
Edit the task and switch to the Triggers-tab.
Select the designated trigger in the list and click Edit-button.
Make sure that all of the below is true:
"Begin the task" option is set to "On a schedule" value.
Start date is today, and time is a few minutes later than current.
"Enabled" check-box is checked.
Click OK all the way out to activate the task, then verify at the next run time.
All of the above settings are important, but the key here is, the task will NOT run until it gets triggered first. In other words, it will run on schedule only AFTER it gets triggered. This is why start time must be set slightly into the future.

VBS Runs from Explorer but not Task Scheduler

I have a legacy VBS script that runs on a schedule via task scheduler. The script checks a folder (located in a mapped network drive) and processes the files it finds in the folder.
Recently it just stopped working. I didn't write this script, I've only inherited it. It no longer seems to find files in the folder it's looking in, even when they're there. To test we created a log file and wrote file names to it. When manually double-clicking the file and running it from explorer it writes to the log file and sees everything in the directory. If you run it from the Task Scheduler it sees no files and writes nothing to the log file. It also runs for days and never seems to exit when the scheduler runs it, even if there are no files and when I run manually it closes immediately when there are no files.
We have several other legacy scripts that are very similar in function to this one and they all work exactly as expected without issue.
This script is run as administrator (the others are as well). I set the "Start In" to it's current directory (the start in directive is not set for the other scripts and they function normally) and this did not help.
This script uses full file paths to everything (the others do as well).
It works as expected only when double clicked.

VBS script does not work properly from Task Scheduler in Windows 10

I have a windows server designed to perform mail merges for postal letters.
A outside server generates xls data sheets and SFTPS them to my windows server, along with a JSON file with further instructions:
The Letter to merge with (docx file)
An E-mail address to send a PDF of the merged letters.
The script works like this:
1. Constantly(every 60 seconds) monitor directory for JSON files. ->
2. If there is a JSON file, parse it. ->
3. Find XLS file related to that JSON file ->
4. Run VBA/ EXCEL macro to mail merge XLS and the letter into a PDF ->
5. Email PDF to appropriate contact ->
6. Move XLS file to Processed Folder
7. Move JSON file to PRocessed Folder
If I run it manually from the command line it works just fine. However, If I run it from Task Scheduler, either with "RUN" or automatically when the machine restarts or when a given user logs in the script fails. Steps 1, 2, and 3 work. The VBA code/ Macro fails to work.
I'm hoping this can be resolved through configuration of Task Scheduler. Here is my current setup:
Script runs from a bat file, the message script keeps a CMD window open that explains what the script is doing:
cscript C:\automerge\message.vbs
cscript C:\automerge\automerge.vbs >> C:\automerge\logs.log
Task Scheduler Settings:
Run only when user is logged on
Run with Highest Privileges
Configure for Windows 10 (tried using 7 and 8 too)
Trigger:
Trigger at logon - Enabled
Action:
Start a program
Details:
"C:\AutoMerge\JSON Watch.bat"
Conditions:
Start the task only if computer is on AC power
Stop if computer switches to battery power
Settings:
Allow Task to run on demand
If the running task does not end when requested, force it to stop
Can anyone give me advice on how to change the scheduled task or batch file to get this to work?
Try executing like this instead of calling the batch file directly.
32-bit
Action: "%SYSTEMROOT%\system32\cmd.exe /c C:\AutoMerge\JSON Watch.bat"
or 64-bit
Action: "%SYSTEMROOT%\SysWOW64\cmd.exe /c C:\AutoMerge\JSON Watch.bat"
If it is 64-bit... update your cscript to execute %SYSTEMROOT%\SysWOW64\cscript.exe
Turns out Noodles was right it was something in the code.
I created a file system object and used it to get a File.Name. Then I passed this File.Name(without the path) to a function that parses the JSON in the file. This worked just fine when I manually ran it.
JSONExists(File.Name)
In order to run properly from Task Scheduler I had to make sure it passed the entire file name including the path.
FPath = "C:\Automerge\"
JSONExists(FPath & File.Name)
I'm not exactly sure why I need the whole path with Task Scheduler and not when I run manually, but at least now I have a good rule of thumb.

.BAT file not running in task scheduler

I have a simple batch file set up on a 64-bit Windows Server 2008 R2. I'm trying to set it up to run under the Windows task scheduler, specifying the admin account as the user. I have no problem running the batch file manually, when logged on to the server with the same admin account. When in the Task Scheduler though, the task just Runs and hangs.
Here's the code from the batch file, moving files from a mapped drive to which the admin account has access:
DEL "O:DEL "O:\Finance file\AP\Check Run Merge\*.*" /Q
copy "O:\Finance file\AP\Invoices\ADT SECURITY_549527511_2015-05-12_655.32.pdf" "O:\Finance file\AP\Check Run Merge\142000.pdf"
copy "O:\Finance file\AP\Invoices\CENTRAL DRUG SYSTEM_244658_2015-05-08_3.50.pdf" "O:\Finance file\AP\Check Run Merge\142001.pdf"
copy "O:\Finance file\AP\Invoices\DATAFLOW BUS.SYSTEMS_156290_2015-05-20_7.50.pdf" "O:\Finance file\AP\Check Run Merge\142003.pdf"
I have just run a batch file from task scheduler using the following settings for the Action on Windows 7. The main part I got wrong the first time was not running it using cmd.exe. Also adding the /c so that the cmd closes after the batch file has run (that is probably why it takes forever).
I made my batch file write to a log file as follows so I could see that it was running:
time /t >> C:\temp\mytestlog.txt
just run it as a cmd.exe with the bat file as an arg. you could also short cut it and run that

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