Using a .bat file to run another .bat file - 2nd .bat file (fails)

Page 1 of 2 12 LastLast

  1. Posts : 14
    Various - W7 32bit, 64bit
       #1

    Using a .bat file to run another .bat file - 2nd .bat file (fails)


    Hi,

    I am using two .bat files together. The first .bat file runs on a timed scheduled task (something like 3pm daily) and is simply a warning/informational message and then if the user hit's "any key" it will close the window/complete the execution and as a result will call a second .bat file. (if they close the first DOS .bat prompt window the second will not be called) The second .bat file runs a series of backup exercises. Namely mail as well as a single folder on the machine. Here is my issue:

    If I manually execute the first .bat file, the 1st dos window opens, I hit any key - the window closes and the second backup .bat file executes beautifully. (I would have mail/files closed) When the first .bat file is called from the scheduled task, i(f the scheduled task opens the first file) and then before hitting 'any key' in the active DOS window, I click out of the prompt to close mail and any open files I have that are included in the backup, THEN activate the DOS window, press return - the second file opens and "runs" but doesn't backup - it acts as it would if I had the outlook mail open (skips the backup).

    It seems that if I take focus off the first DOS prompt that pops up to close mail/files then activate it to execute the second .bat file it 'skips' the execution of the second...it'll run it - the second DOS screen pops up and scrolls through the commands but no backup is actually done.

    I'm perplexed here! I very much appreciate anyone's thoughts/input on this one. Thank you very much.

    John
      My Computer


  2. Posts : 5,092
    Windows 7 32 bit
       #2

    If you post both the .bat files then perhaps we can see what is happening.
      My Computer


  3. Posts : 14
    Various - W7 32bit, 64bit
    Thread Starter
       #3

    Absolutely - it works great if I open the 'prompt' manually OR if it fires and I don't have outlook or anything open and can just hit a key so it triggers the second file execution and backup. but if I have to 'unfocus' the first DOS prompt to close outlook or files before 'regaining focus' of it to hit a key to continue - the execution happens but nothing actually backs up...so confused.

    Prompt file here:

    @echo PLEASE CLOSE OUTLOOK EMAIL AND ANY OPEN FILES YOU STORE IN THE "VITUSA" FOLDER ON YOUR MACHINE FOR A BRIEF DATA BACKUP.
    @echo *******
    @echo *******
    @echo PRESS ENTER ONLY AFTER OUTLOOK/FILES HAVE BEEN CLOSED.
    @echo *******
    @echo *******
    @echo IF YOU DO NOT WISH TO BACKUP YOUR FILES AT THIS TIME THEN CLOSE THIS WINDOW BY CLICKING ON THE 'X' IN THE UPPER RIGHT CORNER OF THIS POPUP.
    @echo *******
    @echo *******
    PAUSE

    Call C:\Backup.bat





    and backup file (called bat file) here:


    ::Create the Log file of bat file execution
    ::
    ::
    Echo >U:\BackupLog.txt


    :: Outlook file compression and backup
    ::
    ::
    for %%A in (C:\Users\%USERNAME%\AppData\Local\Microsoft\Outlook) do "C:\Program Files (x86)\7-Zip\7z.exe" a -t7z -mx5 "U:\outlooknew" -xr!*.bat "C:\Users\%USERNAME%\AppData\Local\Microsoft\Outlook">>U:\BackupLog.txt
    REN "U:\%USERNAME%outlook.7z" "outlookold.7z"
    REN "U:\outlooknew.7z" "%USERNAME%outlook.7z"
    DEL "U:\outlookold.7z"



    ::User file compression and backup
    ::
    ::
    for %%A in (C:\Users\John\Documents\Vitusa) do "C:\Program Files (x86)\7-Zip\7z.exe" a -t7z -mx5 "U:\Vitusanew" -xr!*.bat "C:\Users\John\Documents\Vitusa">>U:\BackupLog.txt
    REN "U:\%USERNAME%Vitusa.7z" "Vitusaold.7z"
    REN "U:\Vitusanew.7z" "%USERNAME%Vitusa.7z"
    DEL "U:\Vitusaold.7z"



    ::Backup the OneNote notebook to the U:
    ::
    ::
    xcopy C:\Users\%USERNAME%\Documents\"OneNote Notebooks"\Vitusa U:\"JK OneNote Backup"/s/e/i/y>>U:\BackupLog.txt
      My Computer


  4. Posts : 5,092
    Windows 7 32 bit
       #4

    From what you say it sounds like it sees the outlook files as in use by another process. I don't know why it doesn't give an error though.

    edit: instead of .bat you could get a bit more sophisticated using a scripting language. AutoIt is easy to learn. AutoHotkey_L is more powerful. And there's always the MS ones such as powershell and VBScript.

    The AutoIt and AutoHotkey_L packages are very small. Typically a compiled program is around 300 KB or less. Both have forums where you can ask for help if you get stuck.
      My Computer


  5. Posts : 880
    Windows 7 Professional 64bit
       #5

    Couple longshots that are easy to try:

    1. Rename the 2nd file Backup.cmd (and Call it as such of course)

    2. Wrap the 2nd command into a vbs file thusly so its window doesn't show at all:

    -- Create a .VBS file e.g. Backup.VBS with these lines:

    Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run chr(34) & "C:\Backup.cmd" & Chr(34), 0
    Set WshShell = Nothing

    -- Now from the 1st command Call C:\Backup.VBS

    This solved a similarly odd problem for me, though honestly I am using AutoHotKey to call the .VBS commands. Again, the VBS magic is a generic method I found which simply runs (any) .bat file w/o causing a Command window to open.
      My Computer


  6. Posts : 14
    Various - W7 32bit, 64bit
    Thread Starter
       #6

    thank you for the suggestions - I'll give them a try. curious though - I am using the 1st command prompt a the user interaction to decide if they want/are ready to backup at that time or not...if the vbs option hides it completely that would not be the effect I wanted.....
      My Computer


  7. Posts : 14
    Various - W7 32bit, 64bit
    Thread Starter
       #7

    MilesAhead - I think you just gave me an avenue to pursue - I've been saying it as the possible issue/action but not realizing then that might be the problem to address! lol. if the O/S is viewing Outlook as still getting used perhaps I need to add a kill-cmd to the outlook just prior to calling the second .bat file....perhaps there is some writing that goes on immediately after closing outlook and I need some time before the second .bat fires if I can't kill it manually. thx. I'll have to bring up the process manager and see if outlook is still there when I close the application from the user side! thx!
      My Computer


  8. Posts : 5,092
    Windows 7 32 bit
       #8

    Or you could try inserting a delay in the 2nd batch file with this line:
    PING 127.0.0.1 -n 5 >nul

    Depending what you read the number after the -n is approximately the delay in seconds, or seconds plus 1. IOW, using 5 as shown should get at least a 4 second delay. That may give the system time to "let go" of the files after the app is closed.
    Last edited by MilesAhead; 23 Aug 2013 at 13:55. Reason: clarity
      My Computer


  9. Posts : 880
    Windows 7 Professional 64bit
       #9

    jfk92 said:
    I am using the 1st command prompt a the user interactio...if the vbs option hides it completely that would not be the effect I wanted.....
    If you read my suggestion it said "the 2nd command".
      My Computer


  10. Posts : 14
    Various - W7 32bit, 64bit
    Thread Starter
       #10

    maxseven said:
    jfk92 said:
    I am using the 1st command prompt a the user interactio...if the vbs option hides it completely that would not be the effect I wanted.....
    If you read my suggestion it said "the 2nd command".
    thx but unfortunately wrapping the second command into a vbs file doesn't solve the issue either. Same behavior exhibited - unfortunately the only way is to manually launch the prompt and fire the second command set - any 'loss of focus' of the first prompt seems to hose the success of the second.....thx anyway.
      My Computer


 
Page 1 of 2 12 LastLast

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 21:22.
Find Us