Panarchy
23rd September 2009 04:30 UTC
Scheduled Tasks - How to create some with if & else?
Hello
I'm trying to implement some Scheduled Tasks.
These are for backups, and link to .bat scripts.
There are two kind of Batch scripts in-use;[list=1][*]One that backs-up data to the I:\ drive[*]One that backs up data to the N:\ drive[/list=1]
I'll include both scripts within the one NSIS installer. All servers have an I:\ drive, however only about half of them have the N:\ drive. Here is an idiosyncratic-programming way of showing you what I'd like;
If N:\ exists, extract nDrive-Script.bat to the Backup-Scripts directory
else extract iDrive-Script.bat to the Backup-Scripts directory.
Can you please give me an example script which;
- Extracts the batch files based on existing directories
- Installs them as Scheduled Tasks which run at a certain specified time & date
Thanks in advance,
Panarchy
BTW: Backup-Scripts folder is located in T:\Backup-Scripts
jpderuiter
23rd September 2009 07:50 UTC
To check if the drive exist, you can use IfFileExists, or ${If} ${FileExists} with the LogicLib header.
For scheduled tasks:
http://nsis.sourceforge.net/Scheduled_Tasks
Panarchy
23rd September 2009 22:54 UTC
I have written some code, below which SHOULD do the first part (everything but install the new Scheduled Tasks).
http://pastebin.com/f9bcd502
I can't get the aforementioned code to compile correctly. :(
Here is the output of the compiler;
http://codepad.org/YuaKjt4o
Please help me fix the script.
Thanks in advance,
Panarchy
MSG
24th September 2009 06:51 UTC
Err... The compiler is already telling you what is wrong.
'Error: could not resolve label "PastIBackupsCheck" in function ".onInit"'
You tell it to go to a label that doesn't exist.
Panarchy
25th September 2009 02:54 UTC
Problem solved!
Thanks, I found a better example online, and it is now this;
Function .onInit
IfFileExists "I:\Backups" 0 continue
MessageBox MB_YESNO|MB_ICONQUESTION "Does I:\Backups exist?" IDYES true IDNO continue
true:
!macro ExtractiBackupScripts ToDir
SetOutPath `${ToDir}`
File "Contig.exe"
File "Defrag.cmd"
File "NTBackup.cmd"
!macroend
!insertmacro ExtractiBackupScripts "T:\Backup-Scripts\"
continue:
!macro ExtractnBackupScripts ToDir
SetOutPath `${ToDir}`
File "Contig.exe"
File "NTBackup-2009.cmd"
!macroend
!insertmacro ExtractnBackupScripts "T:\Backup-Scripts\"
FunctionEnd
Which seems to work fine... however I'd like to remove the prompt, and make it continue automatically based on existence of the drive letter/directory.
Also I'll need to add the Scheduled Tasks... which from the looks of things is very confusing. Hopefully I won't have to end up with "ExecWait"ing a Batch!!!
Please tell me how I can fix up my function.
Thanks in advance,
Panarchy
EDIT;
Might of fixed it with:
Section /o "N:\Backups detected" nBackups_detected
nBackupsExists:
SectionGetFlags "${nBackups_detected}" $0
IntOp $0 $0 | ${SF_SELECTED}
SectionSetFlags "${nBackups_detected}" $0
MessageBox MB_OK nBackups
!macro ExtractnBackupscripts ToDir
SetOutPath `${ToDir}`
File "Contig.exe"
File "Defrag.cmd"
File "NTBackup.cmd"
!macroend
!insertmacro ExtractnBackupscripts "T:\Backup-Scripts"
PastnBackupsCheck:
!macro ExtractiBackupScripts ToDir
SetOutPath `${ToDir}`
File "Contig.exe"
File "NTBackup-2009.cmd"
!macroend
!insertmacro ExtractiBackupScripts "T:\Backup-Scripts\c"
SectionEnd
EDIT2: Ended up fixing the issue, now just need to work out how to add Scheduled Tasks...
Panarchy
25th September 2009 06:53 UTC
They ended up not wanting it, so I've got a half-finished script here. Works perfectly, however doesn't install any Scheduled Tasks.
Enjoy ;)
Panarchy
!include Sections.nsh
Name Scheduled Backups
OutFile "Scheduled Backups.exe"
InstallDir "T:\Backup-Scripts"
XPStyle on
Page components
Page instfiles
ShowInstDetails show
Section "Backup Scripts"
!macro ExtractBackupScripts ToDir
SetOutPath `${ToDir}`
File "Contig.exe"
File "Defrag.cmd"
File "NTBackup-2009.cmd"
File "NTBackup.cmd"
!macroend
SectionEnd
; The following section removes old Scheduled Tasks
Section
!macro GetCleanDir INPUTDIR
; ATTENTION: USE ON YOUR OWN RISK!
; Please report bugs here: http://stefan.bertels.org/
!define Index_GetCleanDir 'GetCleanDir_Line${__LINE__}'
Push $R0
Push $R1
StrCpy $R0 "${INPUTDIR}"
StrCmp $R0 "" ${Index_GetCleanDir}-finish
StrCpy $R1 "$R0" "" -1
StrCmp "$R1" "\" ${Index_GetCleanDir}-finish
StrCpy $R0 "$R0\"
${Index_GetCleanDir}-finish:
Pop $R1
Exch $R0
!undef Index_GetCleanDir
!macroend
!macro RemoveFilesAndSubDirs DIRECTORY
!define Index_RemoveFilesAndSubDirs 'RemoveFilesAndSubDirs_${__LINE__}'
Push $R0
Push $R1
Push $R2
!insertmacro GetCleanDir "${DIRECTORY}"
Pop $R2
FindFirst $R0 $R1 "$R2*.*"
${Index_RemoveFilesAndSubDirs}-loop:
StrCmp $R1 "" ${Index_RemoveFilesAndSubDirs}-done
StrCmp $R1 "." ${Index_RemoveFilesAndSubDirs}-next
StrCmp $R1 ".." ${Index_RemoveFilesAndSubDirs}-next
IfFileExists "$R2$R1\*.*" ${Index_RemoveFilesAndSubDirs}-directory
; file
Delete "$R2$R1"
goto ${Index_RemoveFilesAndSubDirs}-next
${Index_RemoveFilesAndSubDirs}-directory:
; directory
RMDir /r "$R2$R1"
${Index_RemoveFilesAndSubDirs}-next:
FindNext $R0 $R1
Goto ${Index_RemoveFilesAndSubDirs}-loop
${Index_RemoveFilesAndSubDirs}-done:
FindClose $R0
Pop $R2
Pop $R1
Pop $R0
!undef Index_RemoveFilesAndSubDirs
!macroend
!insertmacro RemoveFilesAndSubDirs "$WINDIR\tasks\"
SectionEnd
; The above section removes all Scheduled Tasks
Function .onInit
IfFileExists "I:\" 0 continue
MessageBox MB_OKCANCEL|MB_ICONQUESTION "I've cheated the Message box with this, right?" IDOK true IDNO continue
true:
!macro ExtractiBackupScripts ToDir
SetOutPath `${ToDir}`
File "Contig.exe"
File "Defrag.cmd"
File "NTBackup.cmd"
!macroend
!insertmacro ExtractiBackupScripts "T:\Backup-Scripts"
continue:
!macro ExtractnBackupScripts ToDir
SetOutPath `${ToDir}`
File "Contig.exe"
File "NTBackup-2009.cmd"
!macroend
!insertmacro ExtractnBackupScripts "T:\Backup-Scripts"
FunctionEnd