VBSCRIPT that captures a screenshot and saves to a jpeg

Hi.
I need a vbscript that:
1) Minimizes a bat file named abc.bat (Or let me know what you need to identify this window?)
2) Captures a screenshot of the whole desktop.
3) Saves this screenshot to a jpeg file in C:\SCR\. as ABC.jpeg
4) Closes
5) Restores bat file abc.bat

Can I please ask to not use any third party software as it would just make the script deployment more complicated than necessary. All the computers where this script will be required to work on have Win XP with at least .Net framework V2. Take note that I have shockingly little knowledge about vbscripting. Any help would be greatly appreciated!
Rebel_no_1Asked:
Who is Participating?
 
BillDLConnect With a Mentor Commented:
Haai Rebel. Bly te kenne.

You say that you need to capture a screenshot of the desktop and mention potential uses in context with error logging for admin use.  Is it actually the desktop that you need to capture, or an error dialog that happens to be displayed on top of the desktop?

The reason I am asking is that if you are trying to capture screenshots of error dialogs, there is probably a way that this could be automatically triggered to capture ONLY the error window.  That's another story though.  I will assume it's just the desktop you want to capture.

You have had a suggestion to use a program named "MWSNAP".  I fully agree that a 3rd-party program makes a lot more sense than trying to work with the Windows Clipboard.  It is possible, but I would have to try and locate some VBS files I'm sure I saved a while ago.

My issue is with the use of a program that writes its program settings to the registry, or which needs to be installed.  I would suggest using a completely standalone command-line program that only writes as much to the registry as any other "MRU" file accesses do.

There are loads of methods of capturing screenshots using programs to do the dirty work, but often the hard thing is getting the CMD window out of the way and then activating it again.  Using VBS you can bring windows back into focus by "Window Title" or program name, etc, but I have a purely batch suggestion using just one standalone command-line executable that I have tested in Windows XP.

I would suggest "NirCmd.exe" by Nir Sofer (it's currently on version 2.52):

Home Page:
http://www.nirsoft.net/utils/nircmd.html
Full Command Line Syntax and tips:
http://www.nirsoft.net/utils/nircmd2.html#using
Direct Download Link:
http://www.nirsoft.net/utils/nircmd.zip

Once unzipped, all you need for my suggestion is "NirCmd.exe", and if you wish to use it again for some other purposes, then you might want to retain the help file "NirCmd.chm".

It is a multi-purpose program designed to make regularly used functions and actions immediately accessible from the command line, but you are only interested in one command, namely:

nircmd cmdwait nnnn savescreenshot "path.jpg"

where nnnn is the number of milliseconds to wait before capturing a screenshot of the current view, eg. to wait 3 seconds and then save a screenshot as a named .jpg to the specified drive and directory, use:

nircmd cmdwait 3000 savescreenshot "f:\temp\shot.png"

The problem is that it will just capture a screenshot of the CMD window in front of the Desktop (if run from there), or a Windows Explorer window.

Bt default Windows XP users have a "Show Desktop" icon on their "Quick launch Toolbar" when they choose to show it.  This is actually an *.SCF file that resides in their "Quick Launch" folder which (in Windows XP) is:

"C:\Documents and Settings\<User Profile Name Here>\Application Data\Microsoft\Internet Explorer\Quick Launch\Show Desktop.scf"

You will never see the file extension of an *.SCF file.  It is super-hidden, even with Folder Options > View set to show all files and extensions.  The file is actually just a text-based file in standard "INI" format:

[Shell]
Command=2
IconFile=explorer.exe,3
[Taskbar]
Command=ToggleDesktop

Command=2  refers to Windows Explorer.  There are usually other *.SCF files on a Windows computer that show "Channel Bars" etc.  You don't need to run an *.SCF file using any command other than the file name.  It will run using the registry file association here:

[HKEY_CLASSES_ROOT\SHCmdFile]

and will show the desktop by effectively minimising all open windows to the taskbar.  It runs as a "Shell Command" using the *.SCF file as a "script".

I have found that if I "show the desktop" from a batch file and it minimises to the taskbar, a command following that which calls NirCmd with instruction to take a screenshot still runs, even though the batch file is in a minimised state.

To achieve this I used the batch file to ECHO the correct lines to an *.SCF file in the same folder as the batch file, so as to create it on the fly, and then called it.

Here is an uncommented batch file in the Code Snippet.  I have attached a fully commented version as a *.TXT file.  Just rename that changing the extension to *.CMD.  It MUST be in the same folder as "NirCmd.exe", and has been written to dump the screenshot as a JPG image file in the same folder.  The paths can easily be changed, and there is also then scope for using a command-line emailing program to send you the screenshot as an attachment.
 
 
@echo off
REM NirCmd.exe is assumed to be in same folder as this batch file.

set CurrDir=%~dp0
set CurrDir=%CurrDir:~0,-1%

if exist "%CurrDir%\Desktop.scf" del "%CurrDir%\Desktop.scf" > nul

echo [Shell]>"%CurrDir%\Desktop.scf"
echo Command=2 >>"%CurrDir%\Desktop.scf"
REM echo IconFile=explorer.exe,3 >>"%CurrDir%\Desktop.scf"
echo [Taskbar]>>"%CurrDir%\Desktop.scf"
echo Command=ToggleDesktop>>"%CurrDir%\Desktop.scf"

"%CurrDir%\Desktop.scf"

"%CurrDir%\nircmd.exe" cmdwait 4000 savescreenshot "%CurrDir%\ScreenShot.jpg"

"%CurrDir%\Desktop.scf"

if exist "%CurrDir%\Desktop.scf" del "%CurrDir%\Desktop.scf" > nul

Open in new window


 MakeDesktopScreenshot.txt

What SHOULD happen is this:

1. User opens the folder containing the batch file and double-clicks on it.
2. Everything should minimize to the taskbar
3. After about 4 seconds the batch file should close and disappear from the taskbar
4. Windows Explorer should reappear again with the JPG file in that folder.
5. The "Desktop(.scf)" file should have been deleted by the batch file.

Unfortunately this doesn't really like being run either from the Desktop itself or from a shortcut.

Let me know what you think.

Bill
0
 
Rebel_no_1Author Commented:
I have yet to receive an answer. Therefore I have updated the zones with the hope of attracting an expert.
0
 
ComputerTechieCommented:
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Rebel_no_1Author Commented:
Thanbks CT but I have gone through that post but cannot get it to work. I have also downloaded that screencapture.vb.txt but it gives me an error:
Line: 9
Char:12
Error: Expected identifier
Code: 800A03F2
Source: Microsoft VBScript compilation error.
0
 
markdmacCommented:
You can easily accomplish your goal if you can first install this freeware utility on the PC.
http://www.mirekw.com/winfreeware/files/MWSnap300.zip
Once the default language has been selected, you can send it keyboard commands to take the screenshot.
0
 
Rebel_no_1Author Commented:
I am quite confident that this can be accomplished with a vbscript. Therefore I would like to proceed with only a .vbs. If it seems that this cannot be accomplished (I doubt it) with .vbs, surely I would be urged to look at some 3rd party software. Thanks for the advice anyway! I am confident that somebody on EE can help me. This would surely be a script that allot of people could use. Think of hundreds of operators that is required to log errors on software running on "secured and locked down" PC's for instance... This can just be linked to a menu item or button and instantly you have a screenshot saved to a path or mapping (on a central server) which goes a long way in identifying issues. (Especially in Africa...Which is where I am, mostly.) Therefore this is a very valuable script! I could throw in a little charity and post the photo as evidence on EE if somebody requests that in exchange for the working script? This is Expert exchange, isn't it? :-)
0
 
markdmacCommented:
I think you are confusing the limited abilities of VBScript with the more powerful abilities of .Net Framework that would be accessible via VB.net.

Mwsnap can take command line input, hence it is scriptable.  Since the windows OS does not natively have a command line capable of capturing a screen shot accomplishing this with VBScript is not something native to the language.  You might want to learn more about the VBScript object model by cheating out what it contains on DevGuru.com.
0
 
Rebel_no_1Author Commented:
Thanks for the feedback Mark and mods.
Ok so considering the info I have decided that wmsnap must propably be used. It does complicate the deployment process a little bit but I can live with that. My main script is written as a dos .bat. Lets assume option s is "Capture screen shot". when I press s and enter:
1) Is there a command that can be run to minimize the .bat or .exe (compiled .bat) window first before I actually take the screenshot. If I dont minimize it, it might obscure information behind it. I am guessing that I might have to call a vbscript to do that but obviously my knowledge regarding vbs is lacking.
2) What would the parameters be to call wmsnap, take a screenshot, save the screenshot, close wmsnap and allow the main script to continue.
0
 
Rebel_no_1Author Commented:
Ok Mark I get MWSNAP to capture a screenshot and save it as a tiff with your code.
1) Can I get it to always save as .jpg?
2) I would like mwsnap to close after it finished the capture.
3) I think there is steps where an email is sent, that code can be removed from the script. I just don't know what I can remove?

See the attached script...

Thanks for your help.
'==========================================================================  
'  
' NAME: AutomateScreenCpature.vbs  
'  
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor  
' URL: http://www.thespidersparlor.com  
' DATE  : 02/13/2010  
' COPYRIGHT © 2010, All Rights Reserved  
'  
' COMMENT: This script requires freeware MWSnap, download it from:  
'          http://www.mirekw.com/winfreeware/files/MWSnap300.zip  
'  
'  
'  
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF  
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To  
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A  
'    PARTICULAR PURPOSE.  
'  
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS   
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY  
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS  
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE  
'    OF THIS CODE OR INFORMATION.  
'  
'==========================================================================  
  
 
'Specify the program we are going to monitor 
ProgramWeCheck = "C:\Program Files\iTunes\iTunes.exe" 
'Path to screen capture program 
MWScreenCapture = "C:\SCR\MWSnap.exe" 
'Where do you want to save captures to? 
SavePath = "C:\SCR"
  
'Optional to edit the file naming convention below  
'As scripted this will save a capture with time+date+capture  
'Example: 0315-02132010-capture.jpg  
  
MyDateTime = CStr(Right("0" & Hour(Time),2) & Right("0" & Minute(Time),2) & "-" _   
& Right("0" & Month(Date),2) _   
& Right("0" & Day(Date),2) & Year(Date))  
  
 
'************************************************* 
' Set the company specific email information 
'************************************************* 
' Destination Email Address, seperate multiple addresses by commas,  
' enclose all within quotes 
oTo = "Sharath@plc.com" 
' Who should the sender be? Email Address 
oFrom = "Sharath@plc.com" 
' Set the SMTP server IP  
oMyIP = "inex"  
 
 
  
'***********************************  
'Do not edit below this point  
'***********************************  
Set WshShell = CreateObject("WScript.Shell")  
'Setup directory to save captures in  
Set objFSO = CreateObject("Scripting.FileSystemObject")  
If Not objFSO.FolderExists(SavePath) Then  
        objFSO.CreateFolder(SavePath)  
End If  
  
  
'First run our screen capture program  
Set Capture = WshShell.Exec(MWScreenCapture)  
'Now run our program to be captured  
Set MyApp = WshShell.Exec(ProgramWeCheck)  
WScript.Sleep 1000  
'Make sure the focus is on the program  
WshShell.AppActivate MyApp.ProcessID  
WScript.Sleep 1000 
'Take a screen shot  
WshShell.SendKeys "+^D"  
WScript.Sleep 1000
  
'Activate our screen capture program  
WshShell.AppActivate Capture.ProcessID  
WshShell.SendKeys "%FS"  
WScript.Sleep 1000
WshShell.SendKeys SavePath & "\" & MyDateTime & "-Capture{tab}{down}{down}~~"  
'WshShell.SendKeys "%FX" 
 
'Send an email witht he screen capture 
On Error Resume Next  
Dim oFrom, oMyIP, oTo 
Const ForReading = 1 
 
' Set the visual basic constants as they do not exist within VBScript. 
' Do not set your smtp server information here. 
Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing", _ 
cdoSendUsingPort = 2, _ 
cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver" 
 
 
'Create the CDO connections. 
Dim iMsg, iConf, Flds 
Set iMsg = CreateObject("CDO.Message") 
Set iConf = CreateObject("CDO.Configuration") 
Set Flds = iConf.Fields 
 
'SMTP server configuration. 
With Flds 
.Item(cdoSendUsingMethod) = cdoSendUsingPort 
 
'// Set the SMTP server address here. 
.Item(cdoSMTPServer) = oMyIP 
.Update 
End With 
 
'Set the message properties. 
With iMsg 
Set .Configuration = iConf 
.To = oTo 
.From = oFrom 
.Subject = "A Message For You" 
.HTMLBody = "<html><body><h1>Application Screenshot</h1><a href=" & Chr(34) & "file:///server/share name/" & MyDateTime & "-Capture.jpg" & Chr(34) & "View Screenshot</a></body></html>"
End With 
 
'An attachment can be included. 
'iMsg.AddAttachment = SavePath & "\" & MyDateTime & "-Capture.jpg" 
 
'Send the message. 
iMsg.Send

Open in new window

0
 
Rebel_no_1Author Commented:
WRT the vbscript:

1) Can I get it to always save as .jpg? -- Done -- I set the program environment with a .reg file and removed "down down". This reg file also sets the language so can be seamlessly called from the main .bat script. (main .bat script compiled into abc.exe)
2) I would like mwsnap to close after it finished the capture.
3) I think there is steps where an email is sent, that code can be removed from the script. I just don't know what I can remove?
4) I would like to minimize abc.exe before the screenshot is taken if possible.

I attach the script and the .reg file. I am trying my best to help you help me.
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\MirWoj]

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap]

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\AutoSave]
"AutoSaveFolder"="C:\\SCR\\"
"AutoSaveName"="MWSnap"
"AutoSaveNamePrompt"="0"
"AutoSaveAddSuffix"="1"
"AutoSaveNumFrom"="1"
"AutoSaveNumTo"="999"
"AutoSaveAddDate"="0"
"AutoSaveDateFormat"=" yyyy-mm-dd, hh_nn_ss"
"AutoSaveActive"="0"
"AutoSaveFormat"="bmp"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Colors]
"Background"="12566400"
"Ruler"="65535"
"BkgStyle"="5"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Dialogs]
"frmProxySettings"="402,304"
"frmSoftwareCheck"="312,145"
"frmAddFrame"="316,134"
"frmColorInfo"="274,146"
"frmPrint"="310,195"
"frmAbout"="201,109"
"frmRuler"="215,212,487,49"
"frmHotkeys"="431,220"
"frmSettings"="446,99"
"frmLanguage"="373,176,257,311"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Frames]
"FrameType"="1"
"Simple"="wdt=1, clo=0"
"Shaded"="wdt=7, clo1=65535, clo2=255"
"Button"="wdt=3, clo1=16777215, clo2=8421504"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Hotkeys]
"Fixd"="act=1,ctr=1,alt=0,shf=1,key=F"
"Area"="act=1,ctr=1,alt=0,shf=1,key=A"
"Wind"="act=1,ctr=1,alt=0,shf=1,key=W"
"Desk"="act=1,ctr=1,alt=0,shf=1,key=D"
"Last"="act=1,ctr=1,alt=0,shf=1,key=L"
"Rulr"="act=1,ctr=1,alt=1,shf=0,key=R"
"Zoom"="act=1,ctr=1,alt=1,shf=0,key=Z"
"Clop"="act=1,ctr=1,alt=1,shf=0,key=C"
"Info"="act=1,ctr=1,alt=1,shf=0,key=I"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Internet]
"AutoSaveActive"="0"
"InetProxyAddress"=""
"InetProxyPort"="0"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Layout]
"WindowState"="1"
"FormWidth"="600"
"FormHeight"="450"
"FormTop"="101"
"FormLeft"="63"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Options]
"Autostart"="0"
"MinimizeToTray"="1"
"AlwaysInTray"="1"
"StartMinimized"="0"
"MarkCrsPos"="1"
"ShowPreviewBox"="1"
"Language"="English.ini"
"AddCursorIdx"="-2"
"TrfType"="1"
"SnapCount"="17"
"MaxUndoCount"="8"
"CloseMinimizes"="0"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Printing]
"PrintPos         "="11"
"PrintSizMode     "="1"
"PrintSizScale    "="1.5"
"PrintScaleSmooth "="0"
"PrintScaleMethod "="5"
"PrintOrtVert     "="1"
"PrintHeader      "="1"
"HeaderText       "="%n, %d, (%x x %y)"
"HeaderOrientation"="0"
"PrintFooter      "="0"
"FooterText       "="%p"
"FooterOrientation"="0"
"AutoPrintActive"="0"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Rectangles]
"LastRect"="32x32"
"RectCnt"="5"
"Rect1"="16x16"
"Rect2"="32x32"
"Rect3"="48x48"
"Rect4"="64x64"
"Rect5"="96x72"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Saving]
"LastSavePath"="C:\\SCR\\Noname"
"LastFilterIndex"="2"
"LastJPEGQuality"="75"
"LastColorBits"="24"
"LastTransparent"="0"
"LastTranspPixel"="0"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Settings]
"DelayTime"="800"
"SnapHidePgm"="1"
"SnapRestPgm"="1"
"AutoCopy"="0"
"WarnKeys"="1"
"AskOverwrite"="1"

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\Sounds]
"SoundOn"="1"
"SND_OK"="act=1,builtin=1,path="
"SND_ERROR"="act=1,builtin=1,path="
"SND_SAVE"="act=1,builtin=1,path="
"SND_SNAP"="act=1,builtin=1,path="
"SND_CLIPCOPY"="act=1,builtin=1,path="
"SND_CLIPPASTE"="act=1,builtin=1,path="

[HKEY_CURRENT_USER\Software\MirWoj\MWSnap\UI]
"ViewPanelCapt"="1"
"ViewPanelPict"="1"
"ViewSttBar"="1"
"ViewToolbMain"="1"
"ViewToolbPict"="1"

Open in new window

'==========================================================================  
'  
' NAME: AutomateScreenCpature.vbs  
'  
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor  
' URL: http://www.thespidersparlor.com  
' DATE  : 02/13/2010  
' COPYRIGHT © 2010, All Rights Reserved  
'  
' COMMENT: This script requires freeware MWSnap, download it from:  
'          http://www.mirekw.com/winfreeware/files/MWSnap300.zip  
'  
'  
'  
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF  
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To  
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A  
'    PARTICULAR PURPOSE.  
'  
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS   
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY  
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS  
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE  
'    OF THIS CODE OR INFORMATION.  
'  
'==========================================================================  
  
 
'Specify the program we are going to monitor 
'ProgramWeCheck = "C:\Program Files\iTunes\iTunes.exe" 
ProgramWeCheck = "C:\SCR\MWSnap.exe"
'Path to screen capture program 
MWScreenCapture = "C:\SCR\MWSnap.exe" 
'Where do you want to save captures to? 
SavePath = "C:\SCR"
  
'Optional to edit the file naming convention below  
'As scripted this will save a capture with time+date+capture  
'Example: 0315-02132010-capture.jpg  
  
MyDateTime = CStr(Right("0" & Hour(Time),2) & Right("0" & Minute(Time),2) & "-" _   
& Right("0" & Month(Date),2) _   
& Right("0" & Day(Date),2) & Year(Date))  
  
 
'************************************************* 
' Set the company specific email information 
'************************************************* 
' Destination Email Address, seperate multiple addresses by commas,  
' enclose all within quotes 
oTo = "Sharath@plc.com" 
' Who should the sender be? Email Address 
oFrom = "Sharath@plc.com" 
' Set the SMTP server IP  
oMyIP = "inex"  
 
 
  
'***********************************  
'Do not edit below this point  
'***********************************  
Set WshShell = CreateObject("WScript.Shell")  
'Setup directory to save captures in  
Set objFSO = CreateObject("Scripting.FileSystemObject")  
If Not objFSO.FolderExists(SavePath) Then  
        objFSO.CreateFolder(SavePath)  
End If  
  
  
'First run our screen capture program  
Set Capture = WshShell.Exec(MWScreenCapture)  
'Now run our program to be captured  
Set MyApp = WshShell.Exec(ProgramWeCheck)  
WScript.Sleep 1000  
'Make sure the focus is on the program  
WshShell.AppActivate MyApp.ProcessID  
WScript.Sleep 1000 
'Take a screen shot  
WshShell.SendKeys "+^D"  
WScript.Sleep 1000
  
'Activate our screen capture program  
WshShell.AppActivate Capture.ProcessID  
WshShell.SendKeys "%FS"  
WScript.Sleep 1000
WshShell.SendKeys SavePath & "\" & MyDateTime & "-Capture{tab}~~"
'WshShell.SendKeys "%FX" 
 
'Send an email witht he screen capture 
On Error Resume Next  
Dim oFrom, oMyIP, oTo 
Const ForReading = 1 
 
' Set the visual basic constants as they do not exist within VBScript. 
' Do not set your smtp server information here. 
Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing", _ 
cdoSendUsingPort = 2, _ 
cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver" 
 
 
'Create the CDO connections. 
Dim iMsg, iConf, Flds 
Set iMsg = CreateObject("CDO.Message") 
Set iConf = CreateObject("CDO.Configuration") 
Set Flds = iConf.Fields 
 
'SMTP server configuration. 
With Flds 
.Item(cdoSendUsingMethod) = cdoSendUsingPort 
 
'// Set the SMTP server address here. 
.Item(cdoSMTPServer) = oMyIP 
.Update 
End With 
 
'Set the message properties. 
With iMsg 
Set .Configuration = iConf 
.To = oTo 
.From = oFrom 
.Subject = "A Message For You" 
.HTMLBody = "<html><body><h1>Application Screenshot</h1><a href=" & Chr(34) & "file:///server/share name/" & MyDateTime & "-Capture.jpg" & Chr(34) & "View Screenshot</a></body></html>"
End With 
 
'An attachment can be included. 
'iMsg.AddAttachment = SavePath & "\" & MyDateTime & "-Capture.jpg" 
 
'Send the message. 
iMsg.Send

Open in new window

0
 
BillDLCommented:
By the way, I have no idea and no way to test if this will work in Windows 98, 2000, Vista, or 7, as I have no means to test it right now.  I believe that they all use the same lines of text in the "Show Desktop" icon, but in Windows 7 I don't think this is on the "Quick Launch" toolbar.

I have hopefully mitigated any risks of messing with the pre-existing "Show Desktop.scf" file by creating it with the name "Desktop.scf" and also creating it in and calling it from the same folder as the batch file.

I should have mentioned that you can apply the date and time to the file name of the screenshot image using NirCmd.  It uses variables like this:
$currdate.MM_dd_yyyy$
$currtime.HH_mm_ss$
that can be incorporated like this:

nircmd.exe cmdwait 2000 savescreenshot "f:\temp\Screenshot$currdate.MM_dd_yyyy$-$currtime.HH_mm_ss$.jpg"

I am pretty sure that someone can probably use (and fix if necessary) these VBS methods:

 
'Shell Object
Dim clsidShell As New Guid("13709620-C279-11CE-A49E-444553540000")
Dim shell As Object = Activator.CreateInstance(Type.GetTypeFromCLSID(clsidShell))
shell.ToggleDesktop()

Open in new window


(can't recall where I found that), or:

from: http://msdn.microsoft.com/en-us/library/gg537747(v=vs.85).aspx

 
function fnIShellDispatch4ToggleDesktopVB()
  dim objShell
  set objShell = CreateObject("shell.Shell_Application")
    objShell.ToggleDesktop
  set objShell = nothing
end function

Open in new window


but I can't get either to work.

There are API's that utilise the Windows Clipboard, but I'm quite happy with the batch method and standalone 3rd-party utility program I tested.

Previously I used the free IrfanView image editor, called with the correct commands, eg:
i_view32.exe /capture=0 /convert=c:\test.jpg
Although IrfanView requires installation, it writes its settings to an INI file in its own folder and so could be referred to as "portable".  It doesn't rely on registry settings to run unless you associate it with image file types or change certain user settings.  That's my normal method for automating screenshots, but your need to capture the desktop would cause complications.  I can provide details if you would like to experiment though. (http://www.irfanview.com).
0
 
BillDLCommented:
I just re-read your requirements in a follow on comment and it sounds like you have a "DOS Menu" in your batch file, where after the screenshot is taken the batch file should continue.  I will test mine and see if I can get it to restore as a the window with focus, but I don't think it will work.
0
 
BillDLCommented:
Yes it works for me if I just create a :LABEL up at the top of my batch file with a couple of echoed lines and a PAUSE, and then as my last line have a GOTO that clears the screen and jumps to the label.

eg.

:MENU
cls
echo.
echo Put whatever you want here
echo and here
echo.
PAUSE

REM The rest of the batch file here

REM Last line of batch file
goto :MENU

For me this brings the batch file back into focus again with Windows Explorer and the folder where I ran the batch file from behind it.
0
 
Rebel_no_1Author Commented:
Goeiedag Bill!
I would like to take a screenshot of the whole desktop no matter what is displayed on it. Information like the exe version of the software would then be visible and any displayed error boxes etc. I need this for when somebody says "The software does not work". The screenshot therefore performs a vital role of displaying exactly where, in what module, version, screen the error is found. I just don't want my screencapture application taking up the real estate and obscuring information...

So I did the following. see attached.
Run it and you will get an excellent idea of how it works. It saves the .jpg file to C:\SCR.jpg. You should maybe create that folder?
Last question: I cannot seem to add a %date% and %time% to the filename. (I dont want it to overwrite SCR.jpg the whole time) My computer date is displayed as 2011/04/10. I am testing on a Win7 machine at the moment.

I have only the nircmd.exe on the desktop which would mean no registry settings on remote clients. That is GOOD news. Thanks! Therefore this solution is simpler than mwsnap.exe. Simpler is always better in Africa!!!
::Assume nircmd.exe is in the same path as this abc.bat
::Set window size
mode con: cols=60 lines=10
::Sets the current directory, somehow?
set CurrDir=%~dp0
set CurrDir=%CurrDir:~0,-1%
@Echo off
Title USER ERROR LOGGING ASSISTANCE


:MENU
CLS
ECHO -----------------------------------------------------------
ECHO -                           MENU                          -
ECHO -----------------------------------------------------------
ECHO -                                                         -
::MENU ITEMS. This list will contain 10 additional commands.
Echo - Press s to take a screenshot                            -
Echo - Press x to exit                                         -
ECHO -                                                         -
ECHO -----------------------------------------------------------
SET CHOICE=NULL
SET /P CHOICE=
IF %CHOICE% EQU s GOTO :s
IF %CHOICE% EQU x GOTO :x
IF %CHOICE% EQU NULL GOTO :MENU
GOTO :MENU


:s
CLS
::STEPS THAT MUST BE PERFORMED HERE:
::1) Minimize this dos bat window so that it does not obscure any other software or information. Something like this current command might actually also be fine as it would shrink the dos window. Actually doesn't work too bad!!!
mode con: cols=14 lines=1
::2) Capture screenshot with nircmd.exe. or possibly .vbs?
"%CurrDir%\nircmd.exe" savescreenshot ""C:\SCR\SCR.jpg""
::3) Restore window size
mode con: cols=60 lines=10
::3) Feedback
CLS
ECHO The screenshot have been captured, thank you.
PAUSE
::3)Return to menu
GOTO :MENU


:x
CLS
ECHO THANK YOU FOR USING THIS SOFTWARE
PAUSE
EXIT

Open in new window

0
 
BillDLCommented:
Oh damn.  I thought you were referring to the actual DESKTOP rather than capturing the SCREEN on view.  That does complicate things, and I have always had problems using a VBS file CALLED from a batch file to minimise that batch file and then bring it back into focus when the VBS finishes.

Yours looks to be a pretty good (Lekker) workaround for the problem.

I need to go to bed and won't be able to fully address everything right now, but there are just 2 points to make now that might clear things up.

When you run a batch file, %0 is the variable that will contain the fully qualified and double-quoted path to the batch file itself.

%1 will be the first parameter passed to it, %2 the second, etc.

Variables can be modified in various ways as they are expanded.  The modifier starts with a ~ and used simply, %~0 will remove the surrounding double-quotes from %0 as it is expanded.  The same can be achived with %~f0

%~n0 expands to ONLY the Name (n) of %0
%~nx0 expands to ONlY the Name (n) AND Extension (x) of %0
%~dp0 expands to ONLY the Drive (d) AND Path (p) of %0 and leaves a trailing backslash.

Type  FOR /?  and page to the end of the help file to see the available variables.
Also type  SET /?  and page to about half way down to see another form of variable modifiers.

To set the Current Directory path it makes sense to set a usefully named variable "CurrDir" to the path of the batch file being run, but remove the file name and extension of the batch file.

set CurrDir=%~dp0

To remove the trailing backslash I use another type of modifier which instructs that the variable's value be expanded starting from the leftmost character (Zero), being the C in C:\ and backspacing in one character from the end (-1) to exclude it, ie. the trailing \

set CurrDir=%CurrDir:~0,-1%

I can now just use %CurrDir% anywhere in the batch file to expand to the full path to the folder containing the batch file I am running.

To explain why you are not being able to apply dates to your screenshot names, it is because I neglected an essential leading character (~) required in front of the $ symbol for the Date and Time variables used by NirCmd.  Sorry.

Your variables are:
~$currdate.yyyy-MM-dd$
~$currtime.HH-mm-ss$
and you can juggle the yyyy, mm, dd, hh, mm, and ss around, or even leave off the ss.

Your command can be changed to this format:

"%CurrDir%\nircmd.exe" savescreenshot "C:\SCR\SCR_~$currdate.yyyy-MM-dd$_~$currtime.HH-mm$.jpg"

to yield the file name: "SCR_2011-04-10_11-43.jpg"

I'll take a closer look at your batch file later.
0
 
Rebel_no_1Author Commented:
Ok, GOT IT TO WORK!

The script will now also rename the file after it has been saved, to YYYYMMDD-HHhMMmSS.jpg.
Therefore the script works as expected.

I compiled the script into an exe with "bat to exe Converter V1.5". I included the nircmd.exe into the compiled file. Therefore this is now a single exe which can quickly and easily be called to take a screenshot and save the image to a mapping or wherever. I will attach the code as someone else could surely use such a robust solution. Any further input would be appreciated but I feel that all of the guys that helped me here have already done enough! Thank you BillDL Especially! Baie Dankie!!!

Thank you, Thank you, Thank you!!!
::Assume nircmd.exe is in the same path as this abc.bat
::Set window size
mode con: cols=60 lines=10
::Sets the current directory, somehow?
set CurrDir=%~dp0
set CurrDir=%CurrDir:~0,-1%
@Echo off
Title USER ERROR LOGGING ASSISTANCE


:MENU
CLS
ECHO -----------------------------------------------------------
ECHO -                           MENU                          -
ECHO -----------------------------------------------------------
ECHO -                                                         -
::MENU ITEMS. This list will contain 10 additional commands.
Echo - Press s to take a screenshot                            -
Echo - Press x to exit                                         -
ECHO -                                                         -
ECHO -----------------------------------------------------------
SET CHOICE=NULL
SET /P CHOICE=
IF %CHOICE% EQU s GOTO :s
IF %CHOICE% EQU x GOTO :x
IF %CHOICE% EQU NULL GOTO :MENU
GOTO :MENU


:s
CLS
::STEPS THAT MUST BE PERFORMED HERE:
::1) Minimize this dos bat window so that it does not obscure any other software or information. Something like this current command might actually also be fine as it would shrink the dos window. Actually doesn't work too bad!!!
mode con: cols=14 lines=1
::2) Capture screenshot with nircmd.exe. or possibly .vbs?
"nircmd.exe" savescreenshot "C:\SCR.jpg"
::3) Restore window size
mode con: cols=60 lines=10
::4) Rename SCR.jpg
set TmpFile="%temp%.\tmp.vbs"
echo> %TmpFile% n=Now
echo>>%TmpFile% With WScript
echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
echo>>%TmpFile% .Echo "set yr=" + Right(Year(n),2)
echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
echo>>%TmpFile% .Echo "set day=" + Right(100+Day(n),2)
echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
echo>>%TmpFile% .Echo "set min=" + Right(100+Minute(n),2)
echo>>%TmpFile% .Echo "set sec=" + Right(100+Second(n),2)
echo>>%TmpFile% .Echo "set dow=" + WeekDayName(Weekday(n),1)
echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
echo>>%TmpFile% .Echo "set iso=" + CStr(1 + Int(n-2) mod 7)
echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
echo>>%TmpFile% End With
cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
call "%temp%.\tmp.bat"
del "%temp%.\tmp.bat"
del %TmpFile%
set TmpFile=
set stamp=%year%-%month%-%day%_%hour%.%min%.%sec%
SET NAME=%year%%month%%day%-%hour%h%min%m%sec%
RENAME C:\SCR.jpg %NAME%.jpg 
::5) Feedback
::CLS
ECHO The screenshot have been captured, thank you.
PAUSE
::6)Return to menu
GOTO :MENU


:x
CLS
ECHO THANK YOU FOR USING THIS SOFTWARE
PAUSE
EXIT

Open in new window

0
 
Rebel_no_1Author Commented:
This .bat (can be compiled to exe) code can be used to replicate out to a whole Enterprise. It could then serve as a simple procedure to get a user to instanly send you a screenshot to a mapping in the event of an error or something similar. It was developed on win7 but I am pretty sure that it will also work on WinXP (for which it was intended). Simplest solution to accomplish this task on the internet! I would have attached the exe but the code is safer i guess. Remember to download nircmd.exe
0
 
BillDLCommented:
Baie Dankie Rebel.

Sorry for the delay in returning to this question.  Been pretty busy.  Your method looks like it's about as good as possible using a "DOS" batch script.  Good workaround for "minimising" the window there.  It avoids potential problems using VBS methods of trying to set focus on named windows, which cannot always be relied upon to work as intended.

If you use the NirCmd method of capturing the screenshot, you should be able to dispense with the VBS Date and Time method and just use the built-in NirCmd variables which SHOULD work.

Totsiens
Willem
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.