Make .bat silent and automatic - windows

long story short I have a set up that takes pictures every few minutes and saves them. For backup purposes and to keep my flash drive up to date I wrote a short .bat to copy the files every hour
'#echo off
:start
XCOPY "C:\Users\name\pictures\*.bmp" "D:\backup" /q /y
TIMEOUT /t 3600 /nobreak
goto start'
`
However, i would like to make this automatically start(can i just copy it to the startup folder?) and I would like to make it run silently, no open terminal.

Ignoring the fact that this sounds like someone trying to steal pics in a way that is not above board, I think that you would probably have better luck with the Windows Task Scheduler. That way you could schedule the copy and it will happen even after the computer reboots.

It seems to be possible to run it silently from a vbs script
Set WshShell = CreateObject("WScript.shell")
WshShell.Run chr(34) & "loaction of bat as c:\folder\script.bat" & Chr(34), 0
Set WshShell = Nothing
Got this from a quick google, and im not possible to test it as of now but i can return in 2 and a half hours with a better answer if its not working right.

Related

Accidental VBscript fork bomb/infinite loop

This is the first VBscript I've ever written, so I could definitely use some guidance.
Through research and snippet browsing, I've generated the following vbscript to launch a batch file.
dim shell
set shell = CreateObject("WScript.Shell")
shell.Run "permissions.bat"
set shell=nothing
WScript.Quit
The contents of permissions.bat are:
icacls "%PROGRAMFILES(X86)%\programFolder\mySoftwarePackage" /grant "NT AUTHORITY\Authenticated Users":(OI)(CI)M /t
PAUSE
The problem I am having is that when I launch the vbscript, it never stops launching my bat file. It continuously launches instances just like a fork bomb and eventually crashes the windows kernel. Does anybody know what I did wrong here? I didn't put a while loop anywhere.

Desktop Refresh via dll

My personal concern for this problem is because of a dynamic desktop program i am creating which the aim is for users to click a folder on a desktop and the contents of that folder becomes the new desktop. (I will post the code as an answer below as so to not convolute my actual question). However part of the code needs to kill and restart the explorer.exe process in order to reinitialize the desktop to display the new location.
Documentation of this problem is extremely difficult to find as its more technical than most people are willing to go for this particular field. This man is trying to do the exact same thing as me except using autoit, and here users looked more into doing it vbscript side but both came to the same result of killing and restarting explorer.exe in order to update the desktop.
My issue with killing the explorer.exe process in a forceful manner can result in unstable systems and the actual killing of the process takes a longer time to reboot the desktop than whatever action occurs when you simply move the desktop location. I want to know how i can update my desktop, by calling the dlls that update it, but from within a batch and vbscript hybrid.
EDIT:
Investigations to a command such as rundll32.exe user32.dll,LockWorkStation, and later to the user32.dll dependencies has uncovered multiple uses of desktop functions, in which i assume is used to update the desktop in some form. If you would like to view this, download dependency walker and open it to this folder from within the program. (C:\Windows\WinSxS\amd64_microsoft-windows-user32_31bf3856ad364e35_6.3.9600.18123_none_be367a2e4123fd9d\user32.dll)
Here is the manual changing of the desktop via batch and vbs hybrid. Its not perfect, but it provides a nice interface between moving in and out of directories to select the one you want to update to. This uses the taskkill which i want to depreciate with something else.
This is the initial batch script...
#echo off
setlocal enableextensions
::Below computes the desktop location that the program will reference
for /f %%a in ('cscript //nologo C:\HighestPrivelege\DesktopTools\findDesktop.vbs') do set "fold=%%a"
echo %fold%
::this loop will allow users to input new locations for the desktop by
moving through a terminal. Wildcards and autotab completion is usable are
and lack of input results in continuation of script
:loop
echo ################## %fold%
dir %fold% /A:D /B
echo _________________________
set loc=
set /p loc=[New Location?]
cd %fold%\%loc%
set fold=%cd%
IF [%loc%]==[] (goto cont) ELSE (goto loop)
:cont
::Below is the program that runs the regedit rewrite of the desktop variable
for the current user. It passes the decided folder value from earlier as the
new desktop.
cscript //nologo C:\HighestPrivelege\DesktopTools\setdesktop.vbs %fold%
::This restarts explorer.exe in order to reboot the newly created desktop
location. I wish i didnt have to kill explorer.exe forcefully in the process
but so far its the only way to make this program work.
taskkill /F /IM explorer.exe
explorer.exe
endlocal
pause
exit
and the VBS script that follows...
Option Explicit
'variable invocation
Dim objShell, strDesktop, strModify, strDelete, fso, f, File, content, parthe, args, arg1
Set args = WScript.Arguments
set objShell = CreateObject("WScript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")
'this will take in the new desktop location.
set file= fso.OpenTextFile("C:\HighestPrivelege\DesktopTemporary.txt")
content = file.ReadAll
arg1 = args.Item(0)
parthe = ""&arg1&""
'The actual rewrite of the registry file containing the script lies below.
strDesktop = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop"
strModify = objShell.RegWrite(strDesktop,parthe,"REG_EXPAND_SZ")
Activating the batch script will prompt user for where they would like to relocate their desktop to, and pass that variable into the VBscript to be rewrited to the registry. Upon VBScript completion, windows explorer will restart itself in order to reinitialize the new desktop location.
Until i can get a working model of the one where all i have to do is click/interact with the folder to initialize the program, this manual one will have to do.

Simple shutdown script does not function properly after PC wipe

I had a simple VB Script that would let me enter in the amount of minutes before I wanted my PC to turn off by itself, and then it would auto-shutdown. That worked fine. After I wiped my PC, the script no longer functions as intended, instead showing a blank cmd window after I enter the number of minutes before shutdown, and displays the inputbox again (asking for # of minutes before shutdown).
Any ideas on why this won't function correctly, and why it worked before but not now? Do I need a certain package from Microsoft that maybe I didn't reinstall?
Code:
Dim a
Dim oShell
a=inputbox("After how many minutes would you like to shut down your PC? Enter cancel to cancel a previous shutdown")
Set oShell = WScript.CreateObject ("WScript.Shell")
if a = "cancel" then
oShell.run "cmd.exe /c shutdown /a"
elseif a = "" then
MsgBox"Please enter after how many minutes you would like to turn off this PC",0+16,"Enter a number"
elseif a = "0" then
b=msgbox("Are you sure you want to shut down this PC immediately?",4+32,"Shut down immediately?")
if b = "6" then
oShell.run "cmd.exe /c shutdown /s /f"
end if
else
oShell.run "cmd.exe /c shutdown /s /t " & (a * 60)
end if
EDIT: Running the script from its directory works as intended, but running the VBScript from a shortcut (as a I had been doing) doesn't work and yields the above results.
EDIT: Also the script itself won't run properly on my desktop, but runs fine in the folder I store my scripts.
You named the script shutdown.vbs and run it with the working directory set to the directory containing the script. By running oShell.Run "cmd.exe /c shutdown ..." your script is effectively calling itself.
If you call a command shutdown (without path and extension) the system is looking for a file with one of the extensions listed in %PATHEXT% in the directories listed in the %PATH% environment variable. The first match wins.
Since on Windows the current directory comes first in the %PATH% the file %CD%\shutdown.vbs is found before %windir%\system32\shutdown.exe.
Either rename your VBScript or change cmd.exe /c shutdown to cmd.exe /c shutdown.exe and the problem will disappear.

VBS (Visual Basic Script) Run Program hidden/invisible

I am trying to run a program (In this case Internet Explore) hidden/invisible from a VB script.
I found a simple script for making batch files hidden, and tried it. It didn't seem to work as the program just popped up as normal.
Here is my code so far:
CreateObject("Wscript.Shell").Run "iexplore.exe",0,True
This runs the program iexplore.exe, but doesn't run it hidden/invisible.
I am also running this VBS file from a batch file which is hidden.
The batch file simply does:
start Run.vbs
Codes of each script/batch file:
Batch File: Main file launching VBS file
#echo off
:start
start HideExecuteServerVBS.vbs (To Hide the ExecuteServerVBS.bat file when running)
timeout /NOBREAK /T 5
TASKKILL /IM iexplore.exe
timeout /NOBREAK /T 3
TASKKILL /IM iexplore.exe /F
timeout /NOBREAK /T 1800
goto start
HideExecuteServerVBS.vbs
CreateObject("Wscript.Shell").Run "ExecuteServerVBS.bat",0,True
ExecuteServerVBS.vbs
#echo off
C:\Windows\sysWOW64\csript.exe C:\Users\Admin\RunInternetProcess\vbscript.vbs
vbscript.vbs
Set ie = CreateObject("InternetExplorer.Application")
Is there a possible way to run a program invisible through a VB Script (Visual Basic Script)?
So here's the deal, if you are receiving an ActiveX error, you most likely are trying to run this vbscript under a server. Server with a 64bit platform with lack of support for direct execution of 32bit vbscripts? Yeah? If so, here's what you need to do.
Make a batch file:
ExecuteServerVBS.bat
C:\windows\sysWOW64\cscript.exe C:\path\to\your\vbscript.vbs
Put your vbscript code here:
vbscript.vbs
Set ie = CreateObject("InternetExplorer.Application")
'Go crazy
And BOOM. You're done.
UPDATE
update the file ExecuteServerVBS.vbs
#echo off
C:\Windows\sysWOW64\cscript.exe C:\Users\Admin\RunInternetProcess\vbscript.vbs > errorlog.log
update the file vbscript.vbs
On Error Resume Next
Dim ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
'Perform IE functions here......
If err.number <> 0 then wscript.echo err.number & ":" & err.description
You need to load it this way instead:
Set ie = CreateObject("InternetExplorer.Application")
' To make visible, uncomment the following line...
'ie.Visible = True
Have a look at these methods:
http://ss64.com/vb/run.html
http://ss64.com/vb/exec.html
http://ss64.com/vb/shellexecute.html
http://ss64.com/vb/syntax-elevate.html

Run Script if press the close(x) button in batch file

I want to run a script if i press the close(x) button in the batch file. Suppose batch file is running, if i press the close(x) button i want to run some script before closing the batch file incomplete run.
Use start from a different batch file and add the /WAIT argument.
As such:
#echo off
start %cd%/yourbatchfilehere.bat /WAIT
REM additional post-close code here.
Additional info here
This is, as far as I can see, the only appropriate way to get even close to solving your problem using only the system you asked us to use. Since windows' batch implementation provides no way to detect when a batch window is closing and stop it.
You might want to look into something like this. To avoid a user closing the window - just don't show them the window.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "mk123456.bat" & Chr(34), 0
Set WshShell = Nothing
You can use the following vbs script, which will run two files ('Hidden.bat' and 'Visible.bat') so that when 'Visible' is closed, 'Hidden' will run without being shown. This is based on unclemeat's answer and this post.
set WshShell=createobject("wscript.shell")
WshShell.run "Visible.bat", 1, true
WshShell.Run chr(34) & "Hidden.bat" & Chr(34), 0
However I don't know how to pass arguments (especially multi-word ones) to the batch script.

Resources