Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Using a Batch For backing up a directory (Chrome). I'm using the native Windows Schedule Task Manager to ping, run the file (working great). However, if I go to bed (forgetting to shut down Chrome), naturally I don't get a full backup.

Therefore, I added the else if, however if I do shut down Chrome, the file runs, but it's rather "Dirty". How can I add an "if not"? Do I nest it inside the else if?

Here's what I'm running, again it works (regardless of if Chrome is shut down or not), but not "clean". I would like to make it clean, i.e. by showing an error like "Chrome does not Exist".

IF EXIST CHROME COMMAND (
    set drive=C:
    echo ### Killing Chrome Task
    taskkill  /F /IM chrome.exe /T
    PING 1.1.1.1 -n 1 -w 60000 >NUL
)
ELSE (
    :: variables 
    set drive=E:\BrowserBackups\Chrome
    set folder=%DATE:~4,2%_%DATE:~7,2%_%DATE:~10,4%_%RANDOM%
    set backupcmd=xcopy /C/E/H/I/R/K/Y
    echo ### Backing up directory...
    %backupcmd% "%USERPROFILE%\Local Settings\Application Data\Google\Chrome"
                "%drive%\%folder%"
)
pause

Not sure where to put the "IF NOT EXISTS" command.

share|improve this question
1  
Can't you use Windows PowerShell? –  Aluísio A. S. G. Jan 1 '13 at 20:32
1  
What on earth is this? IF EXIST CHROME COMMAND <-- What is that? You have a filename called CHROME with no extension? and you want to run a command if the file CHROME exists, and your command is called COMMAND. What is this?! –  barlop Jan 1 '13 at 20:37
    
I don't think the ELSE you have works the way you think it does. But the ELSE is what you're looking for now. –  Ben Voigt Jan 1 '13 at 20:38
    
@BenVoigt yes you're right it should be ) ELSE ( <--- see if /? So ELSE on the same line as the closing brace of the prior IF –  barlop Jan 1 '13 at 20:39
    
@barlop: the command named COMMAND might be COMMAND.COM. These days it should be invoked as CMD, but the old name is still there. –  Ben Voigt Jan 1 '13 at 20:40
show 1 more comment

1 Answer

Structure your batch file like this:

@echo off
tasklist /fi "imagename eq chrome.exe" 2>NUL | find /c /i "chrome.exe">NUL
if [%errorlevel%]==[0] (
    echo Chrome is running!
    echo Kill it and then backup settings.
) else (
    echo Chrome is not running!
    echo Can backup settings now.
)
share|improve this answer
    
I'd suggest that the "backup settings" part should follow the end of the if-else, since it should execute in either case. –  Ben Voigt Jan 1 '13 at 21:26
    
@BenVoigt: I guess... But going by the OP's comment that he would like a "clean" backup (i.e. when Chrome's not running), I figured the above would be best. Anyway, the important bit's the chrome.exe running-or-not check and proper if-else loop, rest can be modified by him as per his needs. –  Karan Jan 1 '13 at 23:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.