-1
schtasks /create /tn "DAILYPROCCOMPLETE" /tr "\\server\Digital Imaging\Batch Programs\DAILYPROCCOMPLETE.bat" /sc once /s COMPUTER /u DOMAIN\USER /p PASSWORD /st 20:00 /f

My code works fine from Windows 7 command prompt but does not work from .bat file

However, .bat works on Windows 8 Is there a way to make the .bat work on Windows 7? It seems like it just loops on Windows 7 on the same line of code and doesn't stop with any errors.

1 Answers1

1

Figured it out. When I ran the .bat file on Windows 8 it worked because it defaulted to Windows Directory since UNC paths are not supported. Windows 7 does not default to Windows Directory and just loops forever. All I needed to do to make it work was just add another code to start with C drive so I added c: on the first line of code like below and it worked like a charm :) But I'm sure you all knew that.

c:
schtasks /create /tn "DAILYPROCCOMPLETE" /tr "\\server\Digital Imaging\Batch Programs\DAILYPROCCOMPLETE.bat" /sc once /s COMPUTER /u DOMAIN\USER /p PASSWORD /st 20:00 /f
  • That was definitely not the real problem. It is in general to always use in a batch file fully qualified file names to make the execution of the batch file independent on the values of the environment variables `PATH` and `PATHEXT` (more fail-safe) and making the execution of the batch file faster as `cmd.exe` does not longer need to search for the files. So there can be used `%SystemRoot%\System32\schtasks.exe /create /tn "DAILYPROCCOMPLETE" /tr "\\server\Digital Imaging\Batch Programs\DAILYPROCCOMPLETE.bat" /sc once /s COMPUTER /u DOMAIN\USER /p PASSWORD /st 20:00 /f` – Mofi Jun 19 '22 at 10:07
  • But the executables in `%SystemRoot%\System32` like `schtasks.exe` should be always found by `cmd.exe` because of the first folder path of __system__ environment variable `Path` is `%SystemRoot%\System32` by Windows default, any Windows since Windows NT 4.0. On your Windows 7 machine the __system__ environment variable is most likely corrupted and for that reason the command line did not work. `cmd.exe` of Windows 7 sets also `%SystemRoot%` as current directory on double clicking on a batch file in a shared folder accessed using its UNC path. – Mofi Jun 19 '22 at 10:15
  • See also [CMD does not support UNC paths as current directories](https://stackoverflow.com/a/45072396/3074564) for more details. I recommend to check the __system__ environment variable `Path` and if present also the __user__ environment variable `Path` on the Windows 7 machine and fix them on being corrupted somehow. – Mofi Jun 19 '22 at 10:17
  • Thanks Mofi for correcting me on the real problem. Can this be a work around for now or do you see any issue with this work around? – Mike from Guam Jun 20 '22 at 23:34
  • We are trying to migrate all our batch programs to Windows 10 since Windows 7 is no longer supported. – Mike from Guam Jun 20 '22 at 23:38
  • You wrote: "Windows 7 does not default to Windows Directory and just loops forever." I think now the reason is that you named the batch file `schtasks.bat` or `schtasks.cmd` which is the reason for batch file running itself endless on containing just the line `schtasks /create /tn "DAILYPROCCOMPLETE" /tr "\\server\Digital Imaging\Batch Programs\DAILYPROCCOMPLETE.bat" /sc once /s COMPUTER /u DOMAIN\USER /p PASSWORD /st 20:00 /f` and the current directory contains the batch file with file name `schtasks`. It is not advisable to give a batch file the name of an internal command of `cmd.exe`. – Mofi Jun 21 '22 at 07:22
  • It is also not advisable to give a batch file the name of an external [Windows command](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands), i.e. the name of an executable in `%SystemRoot%\System32`. You see the reason. The batch file is executed instead of the Windows command `schtasks`. The real solution for all versions of Windows is therefore the renaming of the batch file to something other than `schtasks` and use in the batch file `%SystemRoot%\System32\schtasks.exe` instead of just `schtasks` to avoid such troubles in future on any Windows. – Mofi Jun 21 '22 at 07:25
  • The command line with `c:` is not needed anymore after using the fully qualified file name of `schtasks` in the batch file. It is of no help if the batch file is really named `schtasks.bat` or `schtasks.cmd` containing just `schtasks` instead of `%SystemRoot%\System32\schtasks.exe` and being stored for testing on your desktop and executed by double clicking on the batch file on your desktop. In this case the desktop directory is set as current directory by `explorer.exe` starting `cmd.exe` to process batch file and `c:` does not change the current directory and batch file runs endless itself. – Mofi Jun 21 '22 at 07:29
  • Please take also a look on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) It explains in full details how `cmd.exe` searches for executables and scripts specified just with their file names without file extension and without path. – Mofi Jun 21 '22 at 07:31
  • Thanks again Mofi. Learning a lot from you. Will make a note not to name any batch file with an external windows command. – Mike from Guam Jul 04 '22 at 02:47