How to open Notepad text file and keep on top of all other windows until i close it in VBScript?

旧街凉风 提交于 2021-02-19 08:34:17

问题


I would like to create a vbscript to open a notepad text file I saved and keep this notepad text file on top of all other windows either until i minimise it or close it.

I want to create a reminder in tasks then so it pops up every 30 minutes or so and reminds me but so when i browse it does not go in the background.

Greatly appreciate any help.

Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "notepad.exe C:\Users\***USER***\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\TODO.txt", 1
Set oShell = Nothing  

回答1:


I made for you an example to create a schedule task to be executed each 30 minutes to open Notepad Maximized :


Option Explicit
Dim Title,fso,Ws,FilePath,TaskName,Repeat_Task,Command
Title = "Create Schedule Task"
Set fso = CreateObject("Scripting.FileSystemObject")
FilePath = "C:\Users\***USER***\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\TODO.txt"
If Not fso.FileExists(FilePath) Then
    MsgBox "File : "& chr(34) & FilePath & chr(34) & vbcrlf & vbcrlf &_
    "Does not exists ! Please check it ," & vbcrlf &_
    "before proceeding with this vbscript !",vbExclamation,Title
    Wscript.Quit(1)
End If
TaskName = "Open_Notepad"
Repeat_Task = 30
Call Create_Schedule_Task(Repeat_Task,TaskName,WScript.ScriptFullName)
Command = "CMD /C Start /MAX Notepad "& FilePath &""
Set Ws = CreateObject("Wscript.Shell")
Ws.run Command,0,True
'--------------------------------------------------------------
Sub Create_Schedule_Task(Repeat_Task,TaskName,ScriptFilePath)
Dim Task,Result
Task = "CMD /C Schtasks /Create /SC DAILY /ST 08:00 /F /RI "&_
Repeat_Task &" /DU 24:00 /TN "& TaskName &" /TR "& ScriptFilePath &""
Set Ws = CreateObject("Wscript.Shell")
Result = Ws.run(Task,0,True)
End Sub
'---------------------------------------------------------------

Bonus : EDIT on 17/08/2020 @08:50 : This is a Hybrid code Batch and Powershell that help us to show All No Microsoft Scheduled Tasks.

Show_No-Microsoft_Tasks.bat


<# : Batch portion
@rem # The previous line does nothing in Batch, but begins a multiline comment block
@rem # in PowerShell.  This allows a single script to be executed by both interpreters.
@echo off
Title Get Schedule Tasks with a Hybrid code Batch and Powershell Script by Hackoo 2020
echo(
rem # This a Powershell command executes the hybrid portion at the bottom of this script
rem @for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0}|out-string)"') do echo %%I
>"%~dpn0.txt"  (
    @for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0}|out-string)"') do echo %%I
)
REM TimeOut /T 3 /NoBreak>nul
If Exist "%~dpn0.txt" Start "" "%~dpn0.txt" & exit /b
rem # End multi-line PowerShell comment block.  Begin PowerShell scripting.
: end Batch / begin PowerShell hybrid code #>
Function getTasks($path) {
    $out = @()
    # Get root tasks
    $schedule.GetFolder($path).GetTasks(0) | % {
        $xml = [xml]$_.xml
        $out += New-Object psobject -Property @{
            "Name" = $_.Name
            "Path" = $_.Path
            "LastRunTime" = $_.LastRunTime
            "NextRunTime" = $_.NextRunTime
            "Actions" = ($xml.Task.Actions.Exec | % { "$($_.Command) $($_.Arguments)" }) -join "`n"     
        }
    }
    # Get tasks from subfolders
    $schedule.GetFolder($path).GetFolders(0) | % {
        $out += getTasks($_.Path)
    }
    #Output
    $out
}
$tasks = @()
$schedule = New-Object -ComObject "Schedule.Service"
$schedule.Connect() 
# Start inventory
$tasks += getTasks("\")
# Close com
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null
Remove-Variable schedule

# To show All No Microsoft Scheduled Tasks
$tasks | ? { $_.Path -notmatch "Micro*" }
Read-Host 'Type any key to continue'
$tasks | ? { $_.Path -notmatch "Micro*" } | Out-GridView
Read-Host 'Type any key to continue'

<#
# Output all tasks
#$tasks | Out-GridView
#Read-Host 'Type any key to continue'
# To show only tasks with those extensions in their TaskPath
#$tasks | ? { $_.Actions -match "(\.vbs|\.vbe|\.cmd|\.bat|\.hta)" } | Out-GridView
#Read-Host 'Type any key to continue'
#>


来源:https://stackoverflow.com/questions/63424337/how-to-open-notepad-text-file-and-keep-on-top-of-all-other-windows-until-i-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!