Problems running batch file as scheduled task

Discussion in 'Windows Server' started by pete0085, Dec 30, 2008.

  1. pete0085

    pete0085 Guest

    I have been trying to run a batch file as a scheduled task. Everytime I
    select to "run now" it doesn't work or preform the task. If I manually
    double click the batch file, then it runs. Is there some command I need to
    add to allow this to run?
     
    pete0085, Dec 30, 2008
    #1
    1. Advertisements

  2. You need to add the usual diagnostic stuff, e.g. like so:
    @echo off
    echo %date% %time% %UserName% >> c:\test.txt
    net use >> c:\test.txt
    s: 1>>c:\test.txt 2>>&1
    cd s:\shared\admin\quality.loop 1>>c:\test.txt 2>>&1
    FOR %%I IN (*.txt) DO 7z a %%~nI.zip -pcomp2009 %%I del *.txt 1>>c:\test.txt
    2>>&1

    The second line serves to prove that the batch file does run, regardless of
    what you might think.

    Now examine c:\test.txt and you will probably see that drive S: does not
    exist or is inaccessible. This is not surprising - mapped drives exist only
    within the context of the process that creates them. You should use UNC
    coding for scheduled tasks.
     
    Pegasus \(MVP\), Dec 30, 2008
    #2
    1. Advertisements

  3. pete0085

    pete0085 Guest

    The problem I was having with the UNC name was the batch file does not allow
    me to enter cd \\server1\shared\admin. Is there a way around this?

    I can enter the drive letter from the server, which is E or cd
    e:\shared\admin, but the scheduled task does not run correctly and ends with
    a code of 1 or never stops running and I need to end it myself.
     
    pete0085, Dec 30, 2008
    #3
  4. It's not the batch file that won't allow you the CD command on a UNC path -
    this is a property of UNC paths that applies everywhere, even at the Command
    Prompt. You can get around it quite easily, e.g. like so:

    @echo off
    echo %date% %time% %UserName% >> c:\test.txt
    set WD=\\Server\Shared\Admin\quality.loop
    FOR /F %%I IN ('dir /b %WD%\*.txt') DO echo 7z a %WD%\%%~nI.zip -pcomp2009
    %WD%\%%I
    echo del %WD%\*.txt

    Remove the word "echo" in Lines 3 and 4 to activate the batch file.
     
    Pegasus \(MVP\), Dec 30, 2008
    #4
  5. pete0085

    pete0085 Guest

    I thank you for your help, but nothing seems to be working. The file will
    run manually . I'm thinking it has to do with the program that is being run,
    7z-zip, that's the 7z command in the batch file. I don't know if that makes
    a difference or gives you other ideas.
     
    pete0085, Dec 30, 2008
    #5
  6. Why don't you restore the debugging code I gave you in my first reply? You
    will probably find that the batch file can't locate 7z.exe, for the very
    simple reason that you did not state where it is located. To make your batch
    file robust you must always fully qualify your third-party commands, e.g.
    like so:
    c:\MyTools\7z.exe
    If you don't then your batch files will fail, for perfectly obvious reasons.
     
    Pegasus \(MVP\), Dec 30, 2008
    #6
  7. pete0085

    pete0085 Guest

    It seems to be working correctly now. After some tweaking I noticed it was
    deleting the .txt files but not converting it to a zip file which made me
    believe there was a problem with the 7z program. I actually had the
    incorrect 7z.exe in the directory. It needed to be the command line version
    of the 7z.exe. It was more an error on my part. Still didn't make any sense
    why it would run by clicking the file, but it works now and thanks for your
    help. I learned something new with the Set WD command.
     
    pete0085, Dec 31, 2008
    #7
  8. Thanks for the feedback. There is probably a slight misunderstanding in your
    reply. The "set WD" command has no particular effect other then creating an
    environmental variable called "WD" so that I don't have to retype it several
    times in my batch file. I might just as easily have made it "set
    x1=\\Server\Shared\Admin\quality.loop".
     
    Pegasus \(MVP\), Jan 1, 2009
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.