We use a batch file as a deployment script for installation of software in a new system. Quite often the batch script will randomly pause until a keypress is present (i.e. the console window is active and a physical key is pressed).
I'm just wondering if anyone has ever come across this? The pause happens at different stages and times while executing the script.
The code format is as below:
cd <folder1>
"Setup.exe"
cd..
cd <folder2>
"Setup.exe"
cd..
cd <folder3>
"Setup.exe"
cd..
...
I guess it's similar to this question. In my case, the batch script is the only substantial process running, otherwise the computer would sit idle.
Related
I'm writing myself a GUI utility for use in a CMD window to navigate between folders,
rather in the style of the old Norton Change Directory utility for DOS.
When run, the app pops up a folder tree to allow the user to select a folder to which
to navigate and then closes and returns to the CMD prompt. At the moment, the way it
works is that it is run as the first command in a "main" batch file. It writes a secondary batch
file, in my app's folder under AppData, containing the commands to change drive and
directory to the folder the user selected, and the main batch file then invokes this
second batch file using CALL.
It works fine, but this way of actually changing the CMD window's current directory
strikes me as inelegant both from the point of view of needing to be run from a batch file
(so that the user's selection can be acted upon after my app has closed) and of
needing the secondary batch file to do the actual navigation.
So, my question is, how can my app send the instructions to the instance of CMD
that owns the window in which the app is run to the folder the user selected? I've tried doing a ShellExecute
of "CMD /K ..." but although that does indeed navigate to the
selected folder, it does so in a new CMD window, not the one my app is run in. The
conceptual gap I have is how to get the current CMD to act on my app's instructions
after my app has terminated.
Fwiw, I thought of trying to write the user's folder selection into an environment variable in the CMD window's environment for the CMD processor to
act upon from there, but this seems to require that the CMD window be opened via "Run as Administrator", which I definitely don't want.
Your program cannot influence the environment variables of the command interpreter because they're separate processes. Your program cannot change the directory of the command interpreter directly, either, because, again, they're separate processes.
You need to use a batch file because the command interpreter executes batch files internally. Since it's all the same process, the batch file has the power to change the current directory, and for that change to remain in effect after the batch file finishes running.
Therefore, you need some way for your interactive program to communicate the directory selection back to the batch file so that it can act on it.
Instead of writing the instructions to another batch file, you could write the result to standard output. Have the batch file capture that output into a variable, and then execute cd on that variable. The batch code would look something like this:
for /f "tokens=*" %%a in ('[select_dir.exe]') do (
set DIRSELECTION=%%a
)
cd /d %DIRSELECTION%
Your Delphi code would look like this:
writeln(selected_dir);
To allow that command to work, you'll need to make sure your program is marked as a console program, as with {$APPTYPE CONSOLE}. If it's not, then the batch file won't receive any output, and probably won't even wait for your program to finish running before proceeding. It's OK for a console program to display a TForm, just like a GUI program.
Basically, I'm wondering if it's possible to sync OneDrive with a batch file. See, I can sync it whenever I go to File Explorer, right click on "OneDrive" on the left, and click "Sync," but I'm wondering if there's command windows has incorporated to have this exact same effect when used with a batch file. I have no idea what it is and have searched to no avail, so I hope that you can help me! Whatever it takes, I'm sure it would be an extremely simple script; the problem is finding out what command to use. Here's an example as a picture:
To shutdown OneDrive from the command line or in a batch file:
%LOCALAPPDATA%\Microsoft\OneDrive\OneDrive.exe /shutdown
To start OneDrive from the command line:
%LOCALAPPDATA%\Microsoft\OneDrive\OneDrive.exe /background
If the /background switch is omitted then Explorer is opened to the OneDrive folder in your user profile but OneDrive does not start.
If restarting OneDrive from a batch file then add the START command or the batch file won't end:
start %LOCALAPPDATA%\Microsoft\OneDrive\OneDrive.exe /background
Get my oneDriveSync.bat and test it.Be minded that it will work only if machine language is English.THe Sync is always the third verb but if it is inactive the third will be Pause so I cant create language independent version at the moment.
This is one simple solution:
Change OneDrive Settings to NOT Start automatically when Windows starts
Create a BAT file and launch OneDrive with Windows Task Scheduler during non-peak hours.
Create a .BAT file to Taskkill OneDrive.exe at the end of the Peak-hours:
Windows 10:
Command line to launch OneDrive (and automatically start syncing the folders/files in settings)
C:\Users\user\AppData\Local\Microsoft\OneDrive\OneDrive.exe
Command line to Taskkill Onedrive.exe
Taskkill /IM OneDrive.exe /F
I'm trying to create a script to copy a secure exe file to the C directory from a flash drive that is assigned the drive letter D. Then to run the exe, delete the exe, then shut down the PC. I have technicians who need to do this in order to make a biometric reader function properly. They keep screwing up the process and I would like to automate the process to save me a headache. The file is secure and cannot be leaked to our customers due to licensing. I already tried a batch script, but the exe doesn't seem to launch correctly.
Here's what I had:
COPY "D:\Biometric\software.exe" "C:\software.exe"
Pause
pushd C:\
Start "C:\software.exe"
Pause
pushd C:\
erase "software.exe" /F /Q
Pause
c:\windows\system32\shutdown -s -f -t 00
I've never tried VBScript, and I figured maybe that might get me the results I need, any help would be appreciated.
Start considers the first set of quotes it finds to be the window's title, so what you have in your code essentially says "set the window's title to 'C:\software.exe' and then execute the start command on nothing."
Insert an extra set of quotes to make the start command work.
start "" "C:\software.exe"
So I tried and tried but couldn't figure out this one for some reason.
how can I run a task from a desired directory instead of System32 directory where cmd.exe is.
so, when I schedule a task and try to run it ..
command prompt suppose to go to "c:\users\aaa\bbb\ccc" and then pass the argument.
Instead, It's starts at c:\Windows\System32 and fails.
Could anybody help me with this please?
I really appreciate it.
Thank you.
EDIT --
so, now I have a run.bat file with following content in it ...
C:\Users\aaa\bbb\ccc\dd (location to my testrunner.bat file)
testrunner.bat Scripts/all.suite website-address ie (command for the task I wanna perform)
net stop schedule (since window is poping up and going away way to fast, I added this to stop it (not working))
type run.bat
#echo off
cd C:\Users\aaa\bbb\ccc\dd
rem this will show all files in dir
rem is the file you're expecting listed?
dir
rem notice how you can make comments with a leading rem(ark)
#echo starting scripts\all.suite
rem you have to change this to have the full path using Windows X:\dir\dir conventions
c:\home\Scripts\all.suite website-address
#echo done running scripts\all.suite website-address
#echo shutting down
net stop schedule
So its still not clear exactly to me your goal. The reason I added the cd c:\... command is that will **C**hange **D**irectory to the path specified.
This is what you need so you can "run a task from a desired directory instead of System32".
Copy everything from the first #echo off to the last net stop and using notepad, paste it into a file, fix command names and paths website-urls, etc, then save that file to c:\temp\testrunner.bat.
Open a cmd.exe window and test that the script works. Just paste c:\temp\testrunner.bat on to cmd-line and hit enter. If that works, then made an entry in the scheduler to run c:\temp\testrunner.bat . I don't know the specifics of running a script for scheduler, so look for clues on the input screen. Is the an option to run 'now'?
If the .bat file doesn't work from the command-line, then you have to fix the file before you try running it in the scheduler. As your command Scripts/all.suite website-address is a little vague, you'll do better to post a new question asking for help to fix the .bat file and use a sample command that people will be able to use on their PCs at home.
IHTH.
I have a batch file to run an application (I used to write some basic code in the 80s in DOS), and I need it to "re-execute" the application every x hours. The thing I cannot find is how to "close" (or stop) the application before running the code again, so it wont keep opening several cmd.exe windows with every loop. I am a total noob at coding but I know the principles of batch files
My actual .bat looks like this
setx some-minor-adjustments
application.exe -couple-of-variables
Then, it opens the usual cmd.exe window, and keeps running the application forever. What I need to do is, somehow, "refresh" (close and re-execute) the application automatically every x hours.
Closing the cmd.exe window is just fine to close the application, nothing more is needed in that sense (like data to save for example)
A friend suggested the cron command but it seems to be for other purposes. I also find over the Internet that I should use the start command to open the application in a new cmd.exe window, or something like that
Any ideas? Thanks in advance!
On Windows, the Task Scheduler is used instead of CRON.
Tasks in task scheduler can be set to shut down after a set time if you can be sure of the maximum duration.
You can also add an exit command after the main command if the main command can be assumed to finish (or spawns to a separate process).
:BEGIN
start /b mspaint
ping -n 6 localhost
tskill mspaint
GOTO BEGIN
This should work on all versions of Windows. Just keep in mind that you have to change the number 6 to the number of seconds of delay you want plus one i.e., the above ping statement will produce a delay of 5 seconds. Similarly, to produce a delay of 1 hour, you will have to write
ping -n 3601 localhost
Also make sure that the batch file is in the same folder as your executable otherwise you will have to specify full path.
Note: You can use mspaint.exe in the start command but for the tskill command, parameter should be only mspaint(no extensions). Using #echo off won't be of much help in this case as the ping requests are echoed back and can't be turned off. You can instead use a batch-to-exe converter and create a silent windowless executable. The -n parameter of ping can be very large but has to be in seconds.