Robocopy certain filename and latest file - robocopy

Im kinda need help with scripting robocopy.
I want copy file with "-Full.zip" contain in the filename and the latest file.
[I want copy filename contain -Full.zip]
[1]: https://i.stack.imgur.com/ngJ3V.jpg
Here is my code but not working
#echo off
set Source=D:\Source
set Target=F:\Target
set Logfile=F:\Test\logcopy.log
SET debug=-debug -log blat.log -timestamp
set Scr="%temp%\TempVBS.vbs"
( echo sPath = "%Source%"
echo Set oFSO = CreateObject("Scripting.Filesystemobject"^)
echo sYesterday = FormatDateTime(DateAdd("d", Now(^), 0^),0^)
echo Process sPath
echo Sub Process(sP^)
echo For Each oFile In oFSO.GetFolder(sp^).Files
echo If FormatDateTime(oFile.DateLastModified,2^) = sYesterday _
echo Then oFile.Attributes = 32 _
echo Else oFile.Attributes = 0
echo Next
echo For Each oFolder In oFSO.GetFolder(sP^).SubFolders
echo Process oFolder.Path
echo Next
echo End Sub ) > %Scr%
cscript //nologo %Scr%
del %Scr%
robocopy "*-Full.zip" /a /s "%Source%" "%Target%" /XF /XC /XO /LOG:%Logfile%

Related

How can I use Explorer in Powershell to select a file to be processed within my Powershell script [duplicate]

Typically, asking the user to supply a file name to a batch script is a messy affair, requiring no misspellings, quotes around paths with spaces, and so forth. Unfortunately, users aren't well-known for accuracy. In situations where input file location is not known until runtime, using a GUI for file selection input reduces the likelihood of user error.
Is there a way to invoke a File... Open style gui file chooser or folder chooser from a Windows batch script?
If the script user has PowerShell or .NET installed, it is possible. See the answer below.
I'm also interested to see what other solutions anyone else can offer.
File Browser
Update 2016.3.20:
Since PowerShell is a native component of pretty much all modern Windows installations nowadays, I'm declaring the C# fallback as no longer necessary. If you still need it for Vista or XP compatibility, I moved it to a new answer. Starting with this edit, I'm rewriting the script as a Batch + PowerShell hybrid and incorporating the ability to perform multi-select. It's profoundly easier to read and to tweak as needed.
<# : chooser.bat
:: launches a File... Open sort of file chooser and outputs choice(s) to the console
:: https://stackoverflow.com/a/15885133/1683264
#echo off
setlocal
for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0} | out-string)"') do (
echo You chose %%~I
)
goto :EOF
: end Batch portion / begin PowerShell hybrid chimera #>
Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
$f.ShowHelp = $true
$f.Multiselect = $true
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }
This results in a file chooser dialog.
The result of a selection outputs You chose C:\Users\me\Desktop\tmp.txt to the console. If you want to force single file selection, just change the $f.Multiselect property to $false.
(PowerShell command mercilessly leeched from the Just Tinkering Blog.) See the OpenFileDialog Class documentation for other properties you can set, such as Title and InitialDirectory.
Folder Browser
Update 2015.08.10:
Since there is already a COM method for invoking a folder chooser, it's pretty easy to build a PowerShell one-liner that can open the folder chooser and output the path.
:: fchooser.bat
:: launches a folder chooser and outputs choice to the console
:: https://stackoverflow.com/a/15885133/1683264
#echo off
setlocal
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose a folder.',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
setlocal enabledelayedexpansion
echo You chose !folder!
endlocal
In the BrowseForFolder() method, the fourth argument specifies the root of the hierarchy. See ShellSpecialFolderConstants for a list of valid values.
This results in a folder chooser dialog.
The result of a selection outputs You chose C:\Users\me\Desktop to the console.
See the FolderBrowserDialog class documentation for other properties you can set, such as RootFolder. My original .NET System.Windows.Forms PowerShell and C# solutions can be found in revision 4 of this answer if needed, but this COM method is much easier to read and maintain.
This should work from XP upwards and does'nt require an hibrid file, it just runs mshta with a long command line:
#echo off
set dialog="about:<input type=file id=FILE><script>FILE.click();new ActiveXObject
set dialog=%dialog%('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);
set dialog=%dialog%close();resizeTo(0,0);</script>"
for /f "tokens=* delims=" %%p in ('mshta.exe %dialog%') do set "file=%%p"
echo selected file is : "%file%"
pause
Windows Script Host
File Selection
Windows XP had a mysterious UserAccounts.CommonDialog WSH object which allowed VBScript and JScript to launch the file selection prompt. Apparently, that was deemed a security risk and removed in Vista.
Folder Selection
However, the WSH Shell.Application object BrowseForFolder method will still allow the creation of a folder selection dialog. Here's a hybrid batch + JScript example. Save it with a .bat extension.
#if (#a==#b) #end /*
:: fchooser2.bat
:: batch portion
#echo off
setlocal
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0"') do (
echo You chose %%I
)
goto :EOF
:: JScript portion */
var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, "Please choose a folder.", 0, 0x00);
WSH.Echo(folder ? folder.self.path : '');
In the BrowseForFolder() method, the fourth argument specifies the root of the hierarchy. See ShellSpecialFolderConstants for a list of valid values.
A file / folder selection may be done with pure Batch, as shown below. Of course, the feel and look is not as pleasant as a GUI, but it works very well and in my opinion it is easier to use than the GUI version. The selection method is based on CHOICE command, so it would require to download it in the Windows versions that don't include it and slightly modify its parameters. Of course, the code may be easily modified in order to use SET /P instead of CHOICE, but this change would eliminate the very simple and fast selection method that only requires one keypress to navigate and select.
#echo off
setlocal
rem Select a file or folder browsing a directory tree
rem Antonio Perez Ayala
rem Usage examples of SelectFileOrFolder subroutine:
call :SelectFileOrFolder file=
echo/
echo Selected file from *.* = "%file%"
pause
call :SelectFileOrFolder file=*.bat
echo/
echo Selected Batch file = "%file%"
pause
call :SelectFileOrFolder folder=/F
echo/
echo Selected folder = "%folder%"
pause
goto :EOF
:SelectFileOrFolder resultVar [ "list of wildcards" | /F ]
setlocal EnableDelayedExpansion
rem Process parameters
set "files=*.*"
if "%~2" neq "" (
if /I "%~2" equ "/F" (set "files=") else set "files=%~2"
)
rem Set the number of lines per page, max 34
set "pageSize=30"
set "char=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
rem Load current directory contents
set "name[1]=<DIR> .."
:ProcessThisDir
set "numNames=1"
for /D %%a in (*) do (
set /A numNames+=1
set "name[!numNames!]=<DIR> %%a"
)
for %%a in (%files%) do (
set /A numNames+=1
set "name[!numNames!]= %%a"
)
set /A numPages=(numNames-1)/pageSize+1
rem Show directory contents, one page at a time
set start=1
:ShowPage
set /A page=(start-1)/pageSize+1, end=start+pageSize-1
if %end% gtr %numNames% set end=%numNames%
cls
echo Page %page%/%numPages% of %CD%
echo/
if %start% equ 1 (set base=0) else set "base=1"
set /A lastOpt=pageSize+base, j=base
for /L %%i in (%start%,1,%end%) do (
for %%j in (!j!) do echo !char:~%%j,1! - !name[%%i]!
set /A j+=1
)
echo/
rem Assemble the get option message
if %start% equ 1 (set "mssg=: ") else (set "mssg= (0=Previous page")
if %end% lss %numNames% (
if "%mssg%" equ ": " (set "mssg= (") else set "mssg=%mssg%, "
set "mssg=!mssg!Z=Next page"
)
if "%mssg%" neq ": " set "mssg=%mssg%): "
:GetOption
choice /C "%char%" /N /M "Select desired item%mssg%"
if %errorlevel% equ 1 (
rem "0": Previous page or Parent directory
if %start% gtr 1 (
set /A start-=pageSize
goto ShowPage
) else (
cd ..
goto ProcessThisDir
)
)
if %errorlevel% equ 36 (
rem "Z": Next page, if any
if %end% lss %numNames% (
set /A start+=pageSize
goto ShowPage
) else (
goto GetOption
)
)
if %errorlevel% gtr %lastOpt% goto GetOption
set /A option=start+%errorlevel%-1-base
if %option% gtr %numNames% goto GetOption
if defined files (
if "!name[%option%]:~0,5!" neq "<DIR>" goto endSelect
) else (
choice /C OS /M "Open or Select '!name[%option%]:~7!' folder"
if errorlevel 2 goto endSelect
)
cd "!name[%option%]:~7!"
goto ProcessThisDir
:endSelect
rem Return selected file/folder
for %%a in ("!name[%option%]:~7!") do set "result=%%~Fa"
endlocal & set "%~1=%result%
exit /B
Other solution with direct run PowerShell command in Batch
rem preparation command
set pwshcmd=powershell -noprofile -command "&{[System.Reflection.Assembly]::LoadWithPartialName('System.windows.forms') | Out-Null;$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog; $OpenFileDialog.ShowDialog()|out-null; $OpenFileDialog.FileName}"
rem exec commands powershell and get result in FileName variable
for /f "delims=" %%I in ('%pwshcmd%') do set "FileName=%%I"
echo %FileName%
Batch + PowerShell + C# polyglot solution
This is the same solution as the Batch + PowerShell hybrid, but with the C# fallback stuff re-added for XP and Vista compatibility. Multiple file selection has been added at xNightmare67x's request.
<# : chooser_XP_Vista.bat
:: // launches a File... Open sort of file chooser and outputs choice(s) to the console
:: // https://stackoverflow.com/a/36156326/1683264
#echo off
setlocal enabledelayedexpansion
rem // Does powershell.exe exist within %PATH%?
for %%I in ("powershell.exe") do if "%%~$PATH:I" neq "" (
set chooser=powershell -noprofile "iex (${%~f0} | out-string)"
) else (
rem // If not, compose and link C# application to open file browser dialog
set "chooser=%temp%\chooser.exe"
>"%temp%\c.cs" (
echo using System;
echo using System.Windows.Forms;
echo class dummy {
echo public static void Main^(^) {
echo OpenFileDialog f = new OpenFileDialog^(^);
echo f.InitialDirectory = Environment.CurrentDirectory;
echo f.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
echo f.ShowHelp = true;
echo f.Multiselect = true;
echo f.ShowDialog^(^);
echo foreach ^(String filename in f.FileNames^) {
echo Console.WriteLine^(filename^);
echo }
echo }
echo }
)
for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
if not exist "!chooser!" "%%I" /nologo /out:"!chooser!" "%temp%\c.cs" 2>NUL
)
del "%temp%\c.cs"
if not exist "!chooser!" (
echo Error: Please install .NET 2.0 or newer, or install PowerShell.
goto :EOF
)
)
rem // Do something with the chosen file(s)
for /f "delims=" %%I in ('%chooser%') do (
echo You chose %%~I
)
rem // comment this out to keep chooser.exe in %temp% for faster subsequent runs
del "%temp%\chooser.exe" >NUL 2>NUL
goto :EOF
:: // end Batch portion / begin PowerShell hybrid chimera #>
Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
$f.ShowHelp = $true
$f.Multiselect = $true
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }
For a folder chooser for XP or Vista, use either the WSH solution or npocmaka's HTA solution.
Two more ways.
1.Using a hybrid .bat/hta (must be saved as a bat) script .It can use vbscript or javascript but the example is with javascrtipt.Does not create temp files.Selecting folder is not so easy and will require an external javascript libraries , but selecting file is easy
<!-- : starting html comment
:: FileSelector.bat
#echo off
for /f "tokens=* delims=" %%p in ('mshta.exe "%~f0"') do (
set "file=%%~fp"
)
echo/
if not "%file%" == "" (
echo selected file is : %file%
)
echo/
exit /b
-->
<Title>== FILE SELECTOR==</Title>
<body>
<script language='javascript'>
function pipeFile() {
var file=document.getElementById('file').value;
var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
close(fso.Write(file));
}
</script>
<input type='file' name='file' size='30'>
</input><hr><button onclick='pipeFile()'>Submit</button>
</body>
1.1 - without submit form proposed by rojo (see comments):
<!-- : starting html comment
:: FileSelector.bat
#echo off
for /f "tokens=* delims=" %%p in ('mshta.exe "%~f0"') do (
set "file=%%~fp"
)
echo/
if not "%file%" == "" (
echo selected file is : "%file%"
)
echo/
exit /b
-->
<Title>== FILE SELECTOR==</Title>
<body>
<script language='javascript'>
function pipeFile() {
var file=document.getElementById('file').value;
var fso= new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
close(fso.Write(file));
}
</script>
<input id='file' type='file' name='file' size='30' onchange='pipeFile()' >
</input>
<hr>
<button onclick='pipeFile()'>Submit</button>
<script>document.getElementById('file').click();</script>
</body>
2.As you already using powershell/net you can create selfcompiled jscript.net hybrid.It will not require temp cs file for compilation and will directly use the built-in jscrript.net compiler.There's no need of powershell too and the code is far more readable:
#if (#X)==(#Y) #end /* JScript comment
#echo off
:: FolderSelectorJS.bat
setlocal
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do (
set "jsc=%%v"
)
if not exist "%~n0.exe" (
"%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
)
for /f "tokens=* delims=" %%p in ('"%~n0.exe"') do (
set "folder=%%p"
)
if not "%folder%" == "" (
echo selected folder is %folder%
)
endlocal & exit /b %errorlevel%
*/
import System;
import System.Windows.Forms;
var f=new FolderBrowserDialog();
f.SelectedPath=System.Environment.CurrentDirectory;
f.Description="Please choose a folder.";
f.ShowNewFolderButton=true;
if( f.ShowDialog() == DialogResult.OK ){
Console.Write(f.SelectedPath);
}
I will leave an 'echo' even to verify that multiple choice works in this code
echo off
set cmd=Add-Type -AssemblyName System.Windows.Forms;$f=new-object Windows.Forms.OpenFileDialog;$f.InitialDirectory= [environment]::GetFolderPath('Desktop');$f.Filter='Text Files(*.txt)^|*.txt^|All Files(*.*)^|*.*';$f.Multiselect=$true;[void]$f.ShowDialog();if($f.Multiselect) {$f.FileNames}else{$f.FileName}
set pwshcmd=powershell -noprofile -command "&{%cmd%}"
for /f "tokens=* delims=" %%I in ('%pwshcmd%') do call :sum "%%I" ret
echo =========
echo --%ret%--
pause
exit /B
:sum [mud] [ret]
echo "%~1"
set FileName=%FileName% "%~1"
set ret=%FileName%
exit /B
I has been wrote my own portable solution:
https://github.com/andry81/contools/tree/HEAD/Utilities/src/_gui/wxFileDialog/
You can download executable from here:
https://github.com/andry81/contools/tree/HEAD/Utilities/bin/contools/wxFileDialog.exe
The utility has dependency on wxWidgets 3.1.x, so you can actually build it for other operating systems.

use for loop to add downloading sections to a powershell script

I want to construct a powershell script that downloads a specific set of files. This set will change periodically, so a for loop is the cleanest way to update the process.
The URLs for each file update on a daily basis with both the date and a unique serial number, but I already have a process in place for extracting that info and saving it to a file. I also have a method for constructing a PS script to download a specific file from such a URL:
set /p file_info=<"<path to file with info>"
set file_dir=<known page directories>
set DL=C:\Users\%USERNAME%\Downloads\test.ps1
echo $url = "http://<domain>.com/%file_dir%/%file_info%" > "%DL%"
echo $output = "C:\Users\%USERNAME%\Downloads\%file_info%" >> "%DL%"
echo (New-Object System.Net.WebClient).DownloadFile($url, $output) >> "%DL%"
I can also loop additions to a PS script:
setlocal enabledelayedexpansion
set /p obj=<"<path to file with list of objects>"
set /p file_info=<"<path to file with info>"
set DL=C:\Users\%USERNAME%\Downloads\test.ps1
:: restarting PS script
echo #new > "%DL%"
for %%f in (%obj%) do (
set a=%%f
echo !a!!file_info! >> "!DL!"
)
However, when I replace echo !a!!file_info! >> "!DL!" with the echo lines from the first script (adapted in the same fashion), the PS script is no longer updated with the looped content (note that I now use the PS username to avoid any issues with delayed expansion):
setlocal enabledelayedexpansion
set /p obj=<"<path to file with list of objects>"
set /p file_info=<"<path to file with info>"
set file_dir=<known page directories>
set DL=C:\Users\%USERNAME%\Downloads\test.ps1
:: restarting PS script
echo #new > "%DL%"
for %%f in (%obj%) do (
set a=%%f
echo $url = "http://<domain>.com/!file_dir!/!a!/!file_info!" >> "!DL!"
echo $output = "C:\Users\$env:UserName\Downloads\!file_info!" >> "!DL!"
echo (New-Object System.Net.WebClient).DownloadFile($url, $output) >> "!DL!"
)
::alternative that doesn't work either:
for %%f in (%obj%) do (
set a=%%f
call echo $url = "http://<domain>.com/!file_dir!/!a!/!file_info!" >> "!DL!"
call echo $output = "C:\Users\$env:UserName\Downloads\!file_info!" >> "!DL!"
call echo (New-Object System.Net.WebClient).DownloadFile($url, $output) >> "!DL!"
)
Why are there issues using a for loop to add URL download sections to a PS script? Is there a way to avoid these issues?
The main problem with your code is that your echoed closing parentheses are prematurely closing the for loop parentheses. In my example below, I've escaped those with carets, ^.
Example:
#Echo Off
SetLocal EnableExtensions
Set /P "obj=" 0< "<path to file with list of objects>"
Set /P "file_info=" 0< "<path to file with info>"
Set "file_dir=<known page directories>"
Set "DL=C:\Users\%USERNAME%\Downloads\test.ps1"
( For %%G In (%obj%) Do (
Echo $url = "http://<domain>.com/%file_dir%/%%G/%file_info%"
Echo $output = "C:\Users\$Env:UserName\Downloads\%file_info%"
Echo (New-Object System.Net.WebClient^).DownloadFile($url, $output^)
)
) 1> "%DL%"

Manage a Batch-based server in PowerShell

I downloaded the binaries of a web server app. Extracting it leaves me with the following directory tree:
./
bin/
start.bat
stop.bat
lib/
*.jar
*.*
As a PowerShell fan, I would rather call Start-MyWebServer.ps1 than start.bat.
This task would be simple if it wasn't for start.bat: it writes the logs directly in the console, and asks to Press a key to continue... before exiting (pause?). The same goes for stop.bat.
I've tried various solutions based on Start-Process -FilePath "...\start.bat" -NoNewWindow, with an without -Wait in order to start the server in the background...
I also tried using Start-Job:
Start-Job -Name "MyWebServer" -ScriptBlock {
Start-Process -FilePath "...\start.bat" -Wait -NoNewWindow
} | Out-Null
This last solution almost works, i.e. when Start-MyWebServer.ps1 returns, the server is still initializing, even though the job is marked as completed. Is there a better solution to my problem that polling the server until it's ready? Or is it the expected behavior of a Start-* commandlet?
Here is the content of start.bat (actually named startup.bat, credit: GeoServer):
#echo off
rem -----------------------------------------------------------------------------
rem Startup Script for GeoServer
rem -----------------------------------------------------------------------------
cls
echo Welcome to GeoServer!
echo.
set error=0
rem JAVA_HOME not defined
if "%JAVA_HOME%" == "" goto trySystemJava
rem JAVA_HOME defined incorrectly
if not exist "%JAVA_HOME%\bin\java.exe" goto badJava
rem Setup the java command and move on
set RUN_JAVA=%JAVA_HOME%\bin\java
echo JAVA_HOME: %JAVA_HOME%
echo.
:checkGeoServerHome
rem GEOSERVER_HOME not defined
if "%GEOSERVER_HOME%" == "" goto noHome
rem GEOSERVER_HOME defined incorrectly
if not exist "%GEOSERVER_HOME%\bin\startup.bat" goto badHome
goto checkDataDir
:trySystemJava
for /f %%i in ('where java') do set RUN_JAVA=%%i
rem --- we might be on amd64 having only x86 jre installed ---
if "%RUN_JAVA%"=="" if DEFINED ProgramFiles(x86) if NOT "%PROCESSOR_ARCHITECTURE%"=="x86" (
rem --- restart the batch in x86 mode---
echo Warning: No java interpreter found in path.
echo Retry using Wow64 filesystem [32bit environment] redirection.
%SystemRoot%\SysWOW64\cmd.exe /c %0 %*
exit /b %ERRORLEVEL%
)
if "RUN_JAVA%"=="" goto noJava
goto checkGeoServerHome
:noJava
echo The JAVA_HOME environment variable is not defined, and no Java executable could be found.
goto JavaFail
:badJava
echo The JAVA_HOME environment variable is not defined correctly.
goto JavaFail
:JavaFail
echo Please install Java or, if present but not in the path, set this environment variable via the following command:
echo set JAVA_HOME=[path to Java]
echo Example:
echo set JAVA_HOME=C:\Program Files\Java\jdk8
echo.
set error=1
goto end
:noHome
if exist ..\start.jar goto noHomeOK
echo The GEOSERVER_HOME environment variable is not defined.
goto HomeFail
:badHome
if exist ..\start.jar goto badHomeOK
echo The GEOSERVER_HOME environment variable is not defined correctly.
goto HomeFail
:HomeFail
echo This environment variable is needed to run this program.
echo.
echo Set this environment variable via the following command:
echo set GEOSERVER_HOME=[path to GeoServer]
echo Example:
echo set GEOSERVER_HOME=C:\Program Files\GeoServer
echo.
set error=1
goto end
:noHomeOK
echo The GEOSERVER_HOME environment variable is not defined.
goto setHome
:badHomeOK
echo The GEOSERVER_HOME environment variable is not defined correctly.
goto setHome
:setHome
echo Temporarily setting GEOSERVER_HOME to the following directory:
cd ..
set GEOSERVER_HOME=%CD%
echo %GEOSERVER_HOME%
echo.
goto checkDataDir
:checkDataDir
rem GEOSERVER_DATA_DIR not defined
if "%GEOSERVER_DATA_DIR%" == "" goto noDataDir
goto setMarlinRenderer
:noDataDir
rem if GEOSERVER_DATA_DIR is not defined then use GEOSERVER_HOME/data_dir/
if exist "%GEOSERVER_HOME%\data_dir" goto setDataDir
echo No valid GeoServer data directory could be located.
echo Please set the GEOSERVER_DATA_DIR environment variable.
echo.
echo Set this environment variable via the following command:
echo set GEOSERVER_DATA_DIR=[path to data_dir]
echo Example:
echo set GEOSERVER_DATA_DIR=C:\Program Files\GeoServer\data_dir
echo.
set error=1
goto end
:setDataDir
set GEOSERVER_DATA_DIR=%GEOSERVER_HOME%\data_dir
echo The GEOSERVER_DATA_DIR environment variable is not defined correctly.
echo Temporarily setting GEOSERVER_DATA_DIR to the following directory:
echo %GEOSERVER_DATA_DIR%
echo.
goto setMarlinRenderer
:setMarlinRenderer
cd %GEOSERVER_HOME%
for /f "delims=" %%i in ('dir /b/s "%GEOSERVER_HOME%\webapps\geoserver\WEB-INF\lib\marlin*.jar"') do set MARLIN_JAR=%%i
if "%MARLIN_JAR%" == "" (
echo Marlin renderer jar not found
goto run
)
set MARLIN_ENABLER=-Xbootclasspath/a:"%MARLIN_JAR%" -Dsun.java2d.renderer=org.marlin.pisces.MarlinRenderingEngine
set JAVA_OPTS=%JAVA_OPTS% %MARLIN_ENABLER%
goto run
:run
cd %GEOSERVER_HOME%
echo Please wait while loading GeoServer...
echo.
"%RUN_JAVA%" %JAVA_OPTS% -DGEOSERVER_DATA_DIR="%GEOSERVER_DATA_DIR%" -Djava.awt.headless=true -DSTOP.PORT=8079 -DSTOP.KEY=geoserver -jar start.jar
cd bin
goto end
:end
if %error% == 1 echo Startup of GeoServer was unsuccessful.
echo.
pause
Did you consider the capturing of the output of the start.bat?
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = '...\start.bat'
$psi.RedirectStandardOutput = $true
$psi.UseShellExecute = $false
$psi.WindowStyle = 'Minimized'
$bytes = New-Object System.Collections.ArrayList
$s = ''
$enc = [System.Text.Encoding]::ASCII
$done = 'Press any key to continue'
$p = [System.Diagnostics.Process]::Start($psi)
for() {
$i = $bytes.add($p.StandardOutput.BaseStream.ReadByte())
if( $bytes[$i] -lt 0 ) { break }
$s = $enc.GetString($bytes)
if( $s -match "`n${done}$" ) { break }
}
if( !$p.HasExited ) {
$p.Kill()
Write-Host 'done...'
}
$p.Dispose()

How can I get the results of a CHKDSK that run on boot?

Problematic
CHKDSK ran when my machine rebooted, and displayed some stuff.
Problem is I have no idea what it displayed, since it then proceeded
to reboot the machine when it was done. How do I get it to stop,
pause or otherwise let me see what it did ?
chkdsk cannot run because the volume is in use by another process mean
?, CHKDSK needs exclusive access to the disk it’s checking if it’s
been instructed to attempt fixes or repairs. If that disk is your
Windows drive (C:), CHKDSK can’t have exclusive access, because
Windows is using that drive simply to run your system.
When we restart, the CHKDSK is performed before Windows is loaded.
CHKDSK runs as it normally does, and when it completes, it reboots the system – which, of course, causes any progress or results that might have been displayed on-screen to disappear.
In order to create a helpful tool for maintenance of my hard drives to check, fix and repair them for errors.
I did this batch
#echo off
Title Check Disk drives for errors and fix them by Hackoo 2016
mode con cols=67 lines=5 & Color 0A
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
set "TmpLog=%Tmp%\TmpLog.txt"
set "Log=%~dp0%computername%_%~n0.txt"
If Exist "%TmpLog%" Del "%TmpLog%"
If exist "%Log%" Del "%Log%"
REM --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO **************************************
ECHO Running Admin shell... Please wait...
ECHO **************************************
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
::::::::::::::::::::::::::::
:: START ::
::::::::::::::::::::::::::::
( Echo Scan started # & Date /T & Time /T & echo ************************ ) > "%TmpLog%"
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=3" ^|find /i ":"') do (
set "fix=%%i"
Call :Affich !fix!
(
echo !fix! Drive
echo ************************
echo(
(echo O
echo Y) | CHKDSK !fix! /f
echo(
echo ************************
)>> "%TmpLog%"
)
EndLocal
Goto Question
Exit /b
:Question
( echo Scan finished # & Date /T & Time /T & echo ************************ )>> "%TmpLog%"
CMD /U /C Type "%TmpLog%" > "%Log%"
If Exist "%TmpLog%" Del "%TmpLog%"
(
echo Answ = MsgBox("Did you want to reboot the computer to complete the scanning ?",VbYesNo+VbQuestion,"Reboot the computer to check hard disk drives for errors by Hackoo"^)
echo If Answ = VbYes then
echo wscript.Quit(0^)
echo Else
echo wscript.Quit(1^)
echo End If
)>"%tmp%\%~n0.vbs"
Cscript /nologo "%tmp%\%~n0.vbs"
IF "%errorlevel%" EQU "1" (start "" "%Log%" & Exit ) else (goto Shutdown)
:Shutdown
echo(
cls
echo(
echo Save your work - Reboot of your computer in 20 seconds
echo(
echo Enregistrer vos documents - Redemarrage du PC dans 20 seconds
Shutdown.exe /r /t 20 /c "Enregistrer vos documents - Redemarrage du PC dans 20 secondes"
start "" %Log%
pause>nul
exit /b
:Affich
Cls
echo(
echo ***********************************
Echo Please wait a while Scanning "%~1"
echo ***********************************
Timeout /T 2 /nobreak>nul
exit /b
So, my question is : How can i get the results of a CHKDSK that run on boot by batch or powershell ?
The information is recorded in the eventlog. You should be able to obtain it like this (using PowerShell):
Get-EventLog -LogName Application -Source Wininit |
Where-Object { $_.Message -like '*checking file system*' } |
Sort-Object TimeGenerated -Descending |
Select-Object -First 1 -Expand Message
I found the answer here:
get-winevent -FilterHashTable #{logname="Application"; id="1001"}| ?{$_.providername –match "wininit"} | fl timecreated, message | out-file Desktop\CHKDSKResults.txt
In batch file we can do like that:
#echo off
set "Log=%tmp%\CHKDSKResults.txt"
If Exist "%Log%" del "%Log%"
Powershell -Command "& "Get-winevent -FilterHashTable #{logname='Application'; id='1001'}^|?{$_.providername -match 'wininit'} ^| fl timecreated, message ^| out-file '%Log%'"
Start "" "%Log%"
EDIT : On 27/07/2016 The final code :
#echo off
Title Check Disk drives for errors and fix them by Hackoo 2016
mode con cols=67 lines=5 & Color 0A
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
set "TmpLog=%Tmp%\TmpLog.txt"
set "Log=%~dp0%computername%_%~n0.txt"
set "MyVBSFile=%~dp0%~n0_On_Boot.vbs"
set "Value=CHKDSK_ON_BOOT"
Set "Key=HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce"
If Exist "%TmpLog%" Del "%TmpLog%"
If exist "%Log%" Del "%Log%"
REM --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO **************************************
ECHO Running Admin shell... Please wait...
ECHO **************************************
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
::::::::::::::::::::::::::::
:: START ::
::::::::::::::::::::::::::::
( Echo Scan started # & Date /T & Time /T & echo ************************ ) > "%TmpLog%"
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2" %%i in ('wmic logicaldisk where "drivetype=3" ^|find /i ":"') do (
set "fix=%%i"
Call :Affich !fix!
(
echo !fix! Drive
echo ************************
echo(
(echo O
echo Y) | CHKDSK !fix! /f
echo(
echo ************************
)>> "%TmpLog%"
)
EndLocal
Goto Question
Exit /b
::******************************************************************
:Question
( echo Scan finished # & Date /T & Time /T & echo ************************ )>> "%TmpLog%"
CMD /U /C Type "%TmpLog%" > "%Log%"
If Exist "%TmpLog%" Del "%TmpLog%"
(
echo Answ = MsgBox("Did you want to reboot the computer to complete the scanning ?",VbYesNo+VbQuestion,"Reboot the computer to check hard disk drives for errors by Hackoo"^)
echo If Answ = VbYes then
echo wscript.Quit(0^)
echo Else
echo wscript.Quit(1^)
echo End If
)>"%tmp%\%~n0.vbs"
Cscript /nologo "%tmp%\%~n0.vbs"
IF "%errorlevel%" EQU "1" ( goto AddKey ) else ( goto Shutdown )
::******************************************************************
:Shutdown
echo(
cls
echo(
echo Save your work - Reboot of your computer in 120 seconds
echo(
echo Enregistrer vos documents - Redemarrage du PC dans 120 seconds
Call:AddKey && Shutdown.exe /r /t 120 /c "Enregistrer vos documents - Redemarrage du PC dans 120 secondes"
pause>nul
exit /b
::******************************************************************
:Affich
Cls
echo(
echo ***********************************
Echo Please wait a while Scanning "%~1"
echo ***********************************
Timeout /T 2 /nobreak>nul
exit /b
::******************************************************************
:AddKey
reg query "%key%" /v "%Value%" >nul 2>&1
If "%errorlevel%" EQU "0" ( Goto :EOF
) Else (
reg add "%Key%" /v "%Value%" /t REG_SZ /d "%MyVBSFile%">nul
(
echo Option Explicit
echo 'Run as Admin
echo If Not WScript.Arguments.Named.Exists("elevate"^) Then
echo CreateObject("Shell.Application"^).ShellExecute DblQuote(WScript.FullName^) _
echo , DblQuote(WScript.ScriptFullName^) ^& " /elevate", "", "runas", 1
echo WScript.Quit
echo End If
echo Dim ws,PSCommand,LogFile,ret
echo LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, "."^)^) ^& "txt"
echo set ws = createobject("wscript.shell"^)
echo PSCommand = "cmd /c Powershell -Command ""& ""Get-winevent -FilterHashTable #{logname='Application'; id='1001'}^|?{$_.providername -match 'wininit'} ^| fl timecreated, message ^| out-file "^& SimpleQuote(LogFile^) ^&""
echo ret = ws.run(PScommand,0,True^)
echo ws.run DblQuote(LogFile^)
echo '**************************************
echo Function DblQuote(Str^)
echo DblQuote = chr(34^) ^& Str ^& chr(34^)
echo End function
echo '**************************************
echo Function SimpleQuote(Str^)
echo SimpleQuote = ChrW(39^) ^& Str ^& ChrW(39^)
echo End Function
echo '**************************************
)>"%MyVBSFile%"
start "" "%Log%"
)
Exit /b
::*******************************************************************

Script to start service if not running then email results

I need to create a script which checks if a service is running and starts it if failed. Regardless of the outcome I would like an email which tells me the results. I would like this to be in a .bat file although .vbs is acceptable.
I have found various posts on how to do this. I am currently using:
set service=###
for /F "tokens=3 delims=: " %%H in ('sc query %service% ^ | findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (net start %service%)
This runs successfully and will start the service if stopped however I am getting
%%H was unexpected at this time
This causing it to register as failed on Task scheduler
The second part of the script I am using is:
for /F "tokens=3 delims=: " %%H in ('sc query %service% ^| findstr " STATE"') do (
if /I "%%H" EQ "RUNNING" (
SendMail /smtpserver localhost /to me#mydomain.com /from watchdog#mydomain.com /subject Service Autostart Notification /body Autostart on service %service% succeded.
) else (
SendMail /smtpserver localhost /to me#mydomain.com /from watchdog#mydomain.com /subject Service Autostart Notification /body Autostart on service %service% failed.
)
)
)
)
This is not sending the email as "Sendmail is not recognised as an internal or external command, operable command or batch file"
Please could someone give me assistance with this. I have tried to find solutions online but have been unsuccessful thus far.
Kind Regards
This batch file allows you to send an email with no third party software, if your IT has not locked down windows scripting host.
Change the port and SSL encryption if your email server needs it - read the first part of the batch file.
You can call the batch file like this:
CALL email.bat "myname#gmail.com" "RecipientEmailAddress#server.com" "Subject line" "Email Body in one line" "smtp.gmail.com" "myname#gmail.com" "password" "d:\folder\filename to attach.txt"
Email.bat
:: email.bat :::::::::::::::::::::::::::::::::::::::::::::::::::::
#echo off
setlocal
:: use these settings to send from a gmail account
:: set port=465 and set SSL=True
:: use these settings for standard email SMTP port and no encryption
:: set port=25 and set SSL=False
:: Change these following items to use the same variables all the time
:: or use the command line to pass all the variables
set Port=25
set SSL=False
set From="myemail#myemailserver.com"
set To="recipient#server.com"
set Subject="Subject line"
set Body="Email Body in one line"
set SMTPServer="mailservername.myemailserver.com"
set User="username"
set Pass="password"
set fileattach="d:\myfolder\file.txt"
:: This section sets the command line arguments
:: use this format: CALL email.bat "myname#gmail.com" "RecipientEmailAddress#server.com" "Subject line" "Email Body in one line" "smtp.gmail.com" "myname#gmail.com" "password" "d:\folder\filename to attach.txt"
if "%~7" NEQ "" (
set From="%~1"
set To="%~2"
set Subject="%~3"
set Body="%~4"
set SMTPServer="%~5"
set User="%~6"
set Pass="%~7"
set fileattach="%~8"
)
set "vbsfile=%temp%\email-bat.vbs"
del "%vbsfile%" 2>nul
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
echo >>"%vbsfile%" Set objArgs = WScript.Arguments
echo >>"%vbsfile%" Set objEmail = CreateObject("CDO.Message")
echo >>"%vbsfile%" objEmail.From = %From%
echo >>"%vbsfile%" objEmail.To = %To%
echo >>"%vbsfile%" objEmail.Subject = %Subject%
echo >>"%vbsfile%" objEmail.Textbody = %body%
if exist %fileattach% echo >>"%vbsfile%" objEmail.AddAttachment %fileattach%
echo >>"%vbsfile%" with objEmail.Configuration.Fields
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendusing") = 2 ' not local, smtp
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpserver") = %SMTPServer%
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpserverport") = %port%
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendusername") = %user%
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendpassword") = %pass%
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpusessl") = %SSL%
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpconnectiontimeout") = 30
echo >>"%vbsfile%" .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send
cscript.exe /nologo "%vbsfile%"
echo email sent (if variables were correct)
del "%vbsfile%" 2>nul
goto :EOF