need help in running a bat file but only after restart (log off/on not included) - windows

i have a certain program that i want to run as windows boots up
but! i need it to be only after windows restart not log off and log on are out of the question - the system i have is running tests and and the app that i want to run is canceling those tests that`s why i need it to run only after windows restarts , so there is no chance that a tests would fail because of it
the .bat file points to the location of
tried shell:common startup but that open the .bat file even after i log off or log on
how do i do this ? i need this to be in a script/.bat file as well

You should add it here
C:\Users\<user name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
But cause you mentioned after only restart then you will need to run an background script to handle that restart
Or we may..
The common command to restart windows via .bat or cmd file is shutdown -r
So we can create
reboot.bat ,script.bat ,mover.bat > for example in 'C:\test' directory
reboot.bat
move script.bat "C:\Users\<user name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\script.bat"
shutdown -r
script.bat
: : stuff here..
C:\test\mover.bat
mover.bat
move "C:\Users\<user name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\script.bat" script.bat
now that we have our scripts let's add them to our Environment variables
From the desktop, right click the Computer icon.
Choose Properties from the context menu.
Click the Advanced system settings link.
Click Environment Variables.
In the section System Variables, find the PATH environment variable
and select it.
Click Edit then add ; following by C:\test Click Ok, Ok etc and
restart your cmd if opened
To answer your question just type reboot.bat or reboot in CMD command-line
Which when you want to run your script after restart you have to type this to cmd or just..make a restart.bat file with this command for just one click
I used stack overflow for android to answer this without test cause i'm not windows but i'm just familiar with plus didn't have my PC at this moment but i hope that answer is clear and understandable

Related

Run as Administrator Bat File/ Cmd File Windows 10

I have the following cmd command:
C:\Users\spidey\AppData\Local\Programs\Python\Python39\python C:\Users\spidey\Documents\sleepScript\textdocument.py
Inside a .bat file. When I double click and run it, it runs well but when I try to right click and run as administrator it just opens the window and closes adruptly without completing the execution.
The end result of the command is a text file which is created in first case but not when I try to run it as administrator.
The reason for this to run as adminstrator is because I will be running it on cloud and so there it runs as administrator.
Here are the content of textdocument.py:
import datetime
file = open('read.txt', 'w')
file.write('Executed # ' + str(datetime.datetime.now()))
file.close()
Here is another update:
Tried to create a shortcut and setup it's advanced property as run as administrator. But that doesn't work as well.
After doing this, I tried to run the .bat file again as administrator but no effect.
As suggested in the comments, I shifted all the files to C: drive so that it's accesible to everyone. But unfortunately that didn't work as well window just opens and aprubtly closes without giving the end result. On the other hand normal running works here as well.
Here is the command:
C:\Python\Python39\python C:\uiPath\textdocument.py
After a long debugging, I realized that the command was actually running correctly. It's all that it was saving the file into the other default directory after adding in my python script:
os.chdir(path)
Where path is the path where I wanted it to store. Whereas in case of double clicking and running it, by default that path was set to the path where the file was located in.
The problem got fixed. Thank you everyone for the help!

run curl from windows task scheduler automatically

I have a simple one line bat file that runs a curl localhost:port. My curl.exe is in the same folder as the bat I don't have it installed globally. It runs fine if I double click it, it also runs fine if I right click in task scheduler and select run task. It also says it completes the "scheduled tasks" successfully but nothing happens (I'm sure of this as I'm checking data that should update if the script is run, and it doesn't happen under the scheduled/automatic scenarios). After reading lots of users issues I tried configuring in two ways (all on 5 minute updates):
Common Way
Action: Start a Program
Program/script: C:\p\updater.bat
Start in: C:\p\
Other Way
Action: Start a Program
Program/script: cmd
Add arguments: /c start "" "C:\p\updater.bat"
I have set permissions to the bat and the containing folder to allow all for all users/etc. Neither work when automatically triggered, neither error. I've tried in Server 2008 and Windows 10 (my OS) mode. Both work if I right click run task, neither automatically, any ideas?
Default working directory for scheduled script is %Windows%\system32. Try to add in first line to you updater.bat:
cd c:\p\
For diagnostic, you can add output redirect to you commands in bat file:
echo Script Started >> c:\p\log.txt
curl SomeCommand SomeCommand >>c:\p:\curloutput.txt
echo Script Ended >> c:\p\log.txt
and check files log.txt and curloutput.txt after execution of your script

Running batch file at Windows 7 UNLOCK

I have a very simple .bat file. It only has one line:
powercfg -h off
What this does is turn off the computer's ability to hibernate. It works fine when I double click the file. I can confirm this by going into the command prompt and typing "powercfg /a", which shows me that hibernate is indeed off. The problem is that this batch file doesn't seem to be running at logon even though it's in my Startup folder.
I've tested it by turning hibernate on using the command prompt ("powercfg -h on") and then actually putting my computer into hibernation. Then I turn it back on and log in to Windows. But when I open a command prompt and type "powercfg /a" it shows me that hibernate is still on. It seems the batch file doesn't run at logon even though it is in my Startup folder.
I've also tried making it run from a task scheduled to run at logon, but that didn't work either.
Some ideas:
Make sure you set the Start in and Program/script options of the batch file correctly.
If (1) doesn't work then try moving the .bat file to a directory with basic permissions.
Try to schedule the execution of the batch file like this cmd /c "c:\path\batch.bat"
Also take a look at this: Batch runs manually but not in scheduled task.
I got it to work using Task Scheduler. The problem was that I was using the trigger "At log on," when I should have chosen "On workstation unlock."
It's obvious to me now, but I didn't think of it at the time: hybernating didn't actually log me off, it only locked me out.

auto run a bat script in windows 7 at login

I have recently acquired a windows 7 laptop from my late grandmother.
I have been using it for work and other things. I decided to create a VM using VirtualBox And now I want to create a user on the (Windows 7 Host) machine so that when I log into that user it autoruns a .bat script to start the VM. To make it clear I only want to run it only if the user "VM" logs in and not my normal user and it would be super awesome if it would autostart in full screen. I have a shortcut on my desktop that executes the command:
"C:\Program Files\Oracle\VirtualBox\VirtualBox.exe" --comment "VM" --startvm "12dada4d- 9cfd-4aa7-8353-20b4e455b3fa"
but how do I make an autorun.bat when I log into the User "VM"?
To run the batch file when the VM user logs in:
Drag the shortcut--the one that's currently on your desktop--(or the batch file itself) to Start - All Programs - Startup. Now when you login as that user, it will launch the batch file.
Another way to do the same thing is to save the shortcut or the batch file in %AppData%\Microsoft\Windows\Start Menu\Programs\Startup\.
As far as getting it to run full screen, it depends a bit what you mean. You can have it launch maximized by editing your batch file like this:
start "" /max "C:\Program Files\Oracle\VirtualBox\VirtualBox.exe" --comment "VM" --startvm "12dada4d-9cfd-4aa7-8353-20b4e455b3fa"
But if VirtualBox has a truly full-screen mode (where it hides even the taskbar), you'll have to look for a command-line parameter on VirtualBox.exe. I'm not familiar with that product.
I hit this question looking for how to run batch scripts during user logon on a standalone windows server (workgroup not in domain). I found the answer in using group policy.
gpedit.msc
user configuration->administrative templates->system->logon->run these programs at user logon
add batch scripts.
you can add them using cmd /k mybatchfile.cmd if you want the command window to stay (on desktop) after batch script have finished.
gpupdate - to update the group policy.
Just enable parsing of the autoexec.bat in the registry, using these instructions.
:: works only on windows vista and earlier
Run REGEDT32.EXE.
Modify the following value within HKEY_CURRENT_USER:
Software\Microsoft\Windows NT\CurrentVersion\Winlogon\ParseAutoexec
1 = autoexec.bat is parsed
0 = autoexec.bat is not parsed

Running Bat file on Win 7 machine

I have an executeable (Command line which requires arguments/parameters) i need to run on a Win 7 machine. I can run the executeable if i right click and choose run as administrator. Of course, it returns an error code that the required command line parameters were not found. In addition, the executable will create an error log if it has a problem, errors out, throws an exception, etc. I then created a batch file and added MyProgram.exe param to it, and right clicked that and chose run as administrator. I get nothing.....no return code, no error log. I created a shortcut on the desktop to the batch file and changed its advanced properties to run as administrator and still nothing. Anyone have any ideas? I need to run this executable with params from a batch file on a Win 7 machine. Thanx
Edited:
I guess maybe it could be a problem with my Batch file, since i am a novice at those as well. I simply had one line:
MyProgram.exe MyImportFile.txt 1 1
Try creating a shortcut on Desktop to cmd.exe
There you have your commandline. Right click your new shortcut, run as administrator.
Then run your bat file or your exe directly with your parameters.

Resources