Jump to content

touch


orbs
 Share

Recommended Posts

Linux touch is a very useful shell command to set the "modified" timestamp of a file to current time, as well as create a new empty file if it doesn't already exists. this is for Windows, plus the ability to touch a file from the context menu. also, this single script features a demonstration of an installation process, which includes:

    - unregister pending removal (see Note #1 below)
    - register for all files
    - create registry uninstall entry

also, an uninstall process:

    - unregister for all files
    - delete registry uninstall entry    
    - register pending removal

so you can use it from the command-line, or run as administrator to install, or uninstall from Windows Control Panel.

Note #1: the pending removal is using the "PendingFileRenameOperations" registry value to remove the file next time Windows starts; hence, if you perform an install after an uninstall, the install needs to cancel the pending removal set by the former uninstall process.

Note #2: this is set to run and compile for 64-bit. 32-bit compatibility is left as an exercise for the reader.

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_UseX64=Y
#AutoIt3Wrapper_UseUpx=N
#AutoIt3Wrapper_Res_Comment=Touch
#AutoIt3Wrapper_Res_Description=Touch
#AutoIt3Wrapper_Res_Fileversion=0.2.0.0
#AutoIt3Wrapper_Res_ProductVersion=-
#AutoIt3Wrapper_Res_LegalCopyright=Or Ben Shabat (or.ben.shabat@gmail.com)
://////=__=-
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/SO ; ^^ /RM
#NoTrayIcon

#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIFiles.au3>

; declare
Global $sAppName = 'Touch'
Global $sAppVer = '0.2.0.0'
Global $hFile ; if need to create a new file

; exit on too many params
If $CmdLine[0] > 1 Then
    MsgBox($MB_ICONERROR, $sAppName, $sAppName & ' was launched with incorrect number of command line parameters.  ')
    Exit
EndIf

; calculate scenario
Global $iScenario = 0
If $CmdLine[0] > 0 Then $iScenario += 4
If IsAdmin() Then $iScenario += 2
If _IsInstalled() Then $iScenario += 1

; ^^ debug
FileWriteLine(@TempDir & '\' & $sAppName & '.txt', @YEAR & '/' & @MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC & '|' & $iScenario & '|' & $CmdLineRaw)

Switch $iScenario
    Case 0
        MsgBox($MB_ICONINFORMATION, $sAppName, $sAppName & ' installation requires administrative privileges.  ' & @CRLF & @CRLF & 'If you want to use ' & $sAppName & ' without installation, then invoke it with the target file(s) as the command line parameter.  ')
        Exit
    Case 1
        MsgBox($MB_ICONINFORMATION, $sAppName, $sAppName & ' is available in the context menu for any file. You can also invoke ' & $sAppName & ' with the target file(s) as the command line parameter.  ')
        Exit
    Case 4, 5, 6, 7
        If Not (StringInStr($CmdLine[1], '*') Or StringInStr($CmdLine[1], '?')) And FileGetAttrib($CmdLine[1]) = '' Then
            $hFile = FileOpen($CmdLine[1], BitOR($FO_OVERWRITE, $FO_CREATEPATH))
            FileWrite($hFile, '')
            FileClose($hFile)
        EndIf
        If Not FileSetTime($CmdLine[1], '') Then MsgBox($MB_ICONERROR, $sAppName & ' Error', 'Unable to touch ' & $CmdLine[1])
        Exit
    Case 2, 3 ; initial setup => setup at "install", uninstall by control panel => setup at "uninstall"
        Switch _SetupGUI($iScenario - 1) ; send 1 or 2
            Case 1 ; install
                ; cancel pending deletion
                _CancelPendingDeletion()
                ; copy main to @WindowsDir
                If Not FileCopy(@ScriptFullPath, @WindowsDir & '\', 1) Then
                    MsgBox($MB_ICONERROR, $sAppName & ' Setup Error', 'Unable to install the application file.  ')
                    Exit
                EndIf
                ; register for all files
                RegWrite('HKLM\SOFTWARE\Classes\*\Shell\' & $sAppName & '\command', '', 'REG_SZ', @WindowsDir & '\' & $sAppName & '.exe "%1"')
                ; create uninstall entry
                RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName, 'DisplayIcon', 'REG_SZ', @WindowsDir & '\' & @ScriptName)
                RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName, 'DisplayName', 'REG_SZ', $sAppName)
                RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName, 'DisplayVersion', 'REG_SZ', $sAppVer)
                RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName, 'InstallDate', 'REG_SZ', @YEAR & @MON & @MDAY)
                RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName, 'InstallLocation', 'REG_SZ', @WindowsDir)
                RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName, 'EstimatedSize', 'REG_DWORD', 1000)
                RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName, 'UninstallString', 'REG_SZ', '"' & @WindowsDir & '\' & @ScriptName & '"')
                RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName, 'HelpLink', 'REG_SZ', 'https://www.autoitscript.com/forum/topic/185479-touch/')
                RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName, 'Publisher', 'REG_SZ', 'Or Ben Shabat')
                MsgBox($MB_ICONINFORMATION, $sAppName & ' Setup', 'Installation completed successfully.')
            Case 2 ; uninstall
                ; unregister for all files
                RegDelete('HKLM\SOFTWARE\Classes\*\Shell\' & $sAppName)
                ; delete uninstall entry
                RegDelete('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName)
                ; mark file for deletion after reboot
                If @Compiled Then _WinAPI_MoveFileEx(@WindowsDir & '\' & @ScriptName, '', $MOVE_FILE_DELAY_UNTIL_REBOOT)
                ; done
                MsgBox($MB_ICONINFORMATION, $sAppName, 'Uninstallation completed successfully. Some files are scheduled for deletion next time Windows starts.')
        EndSwitch
    Case 5 ; n/a => improper use (running target as admin)
        MsgBox($MB_ICONINFORMATION, $sAppName, 'Improper Use Alert:' & @CRLF & @CRLF & 'running a pre-elevated target via a portable instance is not permitted.  ')
EndSwitch

Func _IsInstalled()
    Return (RegRead('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' & $sAppName, 'UninstallString') <> '') * 1
EndFunc   ;==>_IsInstalled

Func _SetupGUI($iRetInit = 1) ; returns 0 = cancel, 1 = order to install, 3 = order to uninstall
    Local Const $iGUI_W = 200
    Local Const $iGUI_H = 140
    Local Const $iBTN_W = 70

    Local $hGUI = GUICreate($sAppName & ' Setup', $iGUI_W, $iGUI_H, Default, Default, BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX))
    GUISetFont(Default, Default, Default, 'Tahoma')

    GUICtrlCreateGroup('', ($iGUI_W - $iBTN_W) / 2 - 10, 10, $iBTN_W + 20, 80)
    RemoveTheme(GUICtrlGetHandle(-1))

    Local $gRadio_Install = GUICtrlCreateRadio(' Install', ($iGUI_W - $iBTN_W) / 2, 25)
    If $iRetInit = 1 Then GUICtrlSetState(-1, $GUI_CHECKED)
    Local $gRadio_Uninstall = GUICtrlCreateRadio(' Uninstall', ($iGUI_W - $iBTN_W) / 2, 55)
    If $iRetInit = 2 Then GUICtrlSetState(-1, $GUI_CHECKED)
    Local $gBTN_OK = GUICtrlCreateButton('Continue', ($iGUI_W - $iBTN_W) / 2 - 10, 100, $iBTN_W + 20, 25)

    Local $iRet = 0
    GUISetState()

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                $iRet = 0
                ExitLoop
            Case $gBTN_OK
                If GUICtrlRead($gRadio_Install) = $GUI_CHECKED Then $iRet = 1
                If GUICtrlRead($gRadio_Uninstall) = $GUI_CHECKED Then $iRet = 2
                ExitLoop
        EndSwitch
    WEnd

    GUIDelete($hGUI)
    Return $iRet
EndFunc   ;==>_SetupGUI

Func _CancelPendingDeletion()
    Local $aList = StringSplit(RegRead('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager', 'PendingFileRenameOperations'), @LF)
    Local $sNewList = ''
    For $i = 1 To $aList[0]
        If StringRight($aList[$i], StringLen(@ScriptName) + 1) <> '\' & @ScriptName Then
            $sNewList &= ($aList[$i] & @LF)
        Else
            $i += 1
        EndIf
    Next
    $sNewList = StringTrimRight($sNewList, 1)
    RegWrite('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager', 'PendingFileRenameOperations', 'REG_MULTI_SZ', $sNewList)
EndFunc   ;==>_CancelPendingDeletion

Func RemoveTheme($hItem)
    DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", $hItem, "wstr", 0, "wstr", 0)
EndFunc   ;==>RemoveTheme

 

Edited by orbs

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...