@echo off
setlocal
for /f "tokens=1 delims=:" %%i in ('^
findstr /l /b /n /c:"exit /b" "%~dpnx0"') do set "n=%%i"
more +%n% "%~dpnx0">>foo.ps1
powershell /nologo /noprofile /noexit .\foo.ps1
endlocal
exit /b
#PowerShell script begin
function Add-Clock {
$code = {
$reg = '\d{2}:\d{2}:\d{2}'
do {
$now = Get-Date -format 'HH:mm:ss'
$old = [Console]::Title
if ($old -match $pattern) {
$new = $old -replace $pattern, $now
}
else {
$new = "$now $old"
}
[Console]::Title = $new
Start-Sleep -seconds 1
} while ($true)
}
$ps = [PowerShell]::Create()
[void]$ps.AddScript($code)
$ps.BeginInvoke()
}
Add-Clock | Out-Null
Remove-Item .\foo.ps1
Diff to Previous Revision
--- revision 1 2013-03-19 15:57:46
+++ revision 2 2013-10-10 16:24:14
@@ -1,19 +1,24 @@
@echo off
- more +4 "%~dpnx0" >> clock.ps1 && powershell -nol -noe .\clock.ps1
+ setlocal
+ for /f "tokens=1 delims=:" %%i in ('^
+ findstr /l /b /n /c:"exit /b" "%~dpnx0"') do set "n=%%i"
+ more +%n% "%~dpnx0">>foo.ps1
+ powershell /nologo /noprofile /noexit .\foo.ps1
+ endlocal
exit /b
-
+#PowerShell script begin
function Add-Clock {
$code = {
- $regex = '\d{2}:\d{2}:\d{2}'
+ $reg = '\d{2}:\d{2}:\d{2}'
do {
- $clock = Get-Date -format 'HH:mm:ss'
+ $now = Get-Date -format 'HH:mm:ss'
$old = [Console]::Title
- if ($old -match $regex) {
- $new = $old -replace $regex, $clock
+ if ($old -match $pattern) {
+ $new = $old -replace $pattern, $now
}
else {
- $new = "$clock $oltitle"
+ $new = "$now $old"
}
[Console]::Title = $new
@@ -22,10 +27,9 @@
}
$ps = [PowerShell]::Create()
- $null = $ps.AddScript($code)
- [void]$ps.BeginInvoke()
+ [void]$ps.AddScript($code)
+ $ps.BeginInvoke()
}
-Add-Clock
-#this is optional string
-Remove-Item .\clock.ps1
+Add-Clock | Out-Null
+Remove-Item .\foo.ps1