running batch files...

Discussion in 'Windows Vista General Discussion' started by maya, Jul 30, 2007.

  1. maya

    maya Guest

    in previous systems I would put shortcuts to batch files on my desktop
    and they would run fine if I just open the shortcuts.. in Vista batch
    files don't run if you open shortcuts to them.. you have to navigate
    your way to where they are in DOS shell and run them from there... is
    there a way around this??

    thank you..
    maya, Jul 30, 2007
    1. Advertisements

  2. maya

    Jon Guest

    Works fine here.

    It may depend more on the particular code that you have in your batch files.
    If you can post examples of code that fails then people may be able to
    advise further.
    Jon, Jul 30, 2007
    1. Advertisements

  3. maya

    maya Guest

    ok, thank you very much.. will further check this @ home tonight..
    maya, Jul 30, 2007
  4. maya

    maya Guest

    ok, this is what I had to do... had to do a new batch file that calls
    other batch file, put shortcut to it on desktop.. THAT worked.. don't
    know why can't directly run batch file I orig wanted to run from
    shortcut.. (code in said batch file:

    @echo off
    if "%OS%" == "Windows_NT" setlocal
    rem Start script for the CATALINA Server
    rem $Id: startup.bat 302918 2004-05-27 18:25:11Z yoavs $

    rem Guess CATALINA_HOME if not defined
    set CURRENT_DIR=%cd%
    if not "%CATALINA_HOME%" == "" goto gotHome
    if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
    cd ..


    thank you........
    maya, Jul 31, 2007
  5. maya

    Jon Guest

    Possibly what's happening is that you're running these batch files with the
    'Run as administrator' option set.
    This sets the starting directory to the system32 directory, rather than to
    the location of the batch file (as it is if you run it non-elevated). Your
    batch file seems to rely on the starting directory being set correctly.

    So you could either put some code at the start of your batch file to change
    to the directory in which it is located (if that is what you want)

    [For a batch file named 'somename.bat', something like this might do it at
    the start of the batch file.....

    Set WD=%0
    Set WD=%WD:somename.bat=%
    cd /d %WD%


    or set up a global environment variable 'CATALINA_HOME' to point to the
    directory in which the 'bin' directory is situated, as your snippet of code
    suggests, if that 'bin' directory is always in the same place.

    You can do that via
    Right-click Computer > Properties > Advanced System Settings > Environment
    Click 'New' under 'System Variables'......
    Jon, Jul 31, 2007
  6. I'm dealing with a similar problem. I have one bat file that calls another
    bat file in the same directory, but I'm not sure which directory they'll both
    be installed in. I just know they'll be in the same directory. When I run the
    first bat file as an Administrator, the current directory changes to
    Windows\System32, and it can't locate the second bat file to run it. Your
    code example above returns the complete path to the bat file from the %0
    parameter, including the bat file name itself. E.g.
    How can my bat file truncate this path to remove the bat file name, so as to
    get only the directory path, e.g. C:\users\moralejf\documents? The
    Set WD=%WD:somename.bat=%
    line in your example is very mysterious to me, but if it is intended to
    truncate the bat file name, it doesn't succeed in doing so (even if I change
    somename.bat to the actual bat file name). Thanks in advance, Joseph
    Joseph Morales, Aug 23, 2007
  7. maya

    Jon Guest

    The first line retrieves the full path to the running batch file into WD ,
    and the second line

    Set WD=%WD:somename.bat=%

    removes the string 'somename.bat' from it - but it's the name of the batch
    file in which it appears rather than the one it is calling. So it would be
    'a.bat' in the batch file 'a.bat', 'b.bat' in the batch file b.bat etc

    This is lifted from the help documentation for 'set'. ie from 'set /?' at a

    "Environment variable substitution has been enhanced as follows:


    would expand the PATH environment variable, substituting each occurrence
    of "str1" in the expanded result with "str2". "str2" can be the empty
    string to effectively delete all occurrences of "str1" from the expanded
    output....... "

    Hope this helps
    Jon, Aug 23, 2007
  8. Thanks for the explanation! It turns out that I had a typo that was keeping
    this from working -- I typed a 0 instead of an O in my filename. (I hate it
    when that happens!)
    Now your example works, and I also understand why it works.

    Now, just to push my luck... Is there any way to not have to hard code the
    bat file name? That way if I retitle the bat file later, it won't break the
    Joseph Morales, Aug 23, 2007
  9. maya

    Jon Guest

    Good question. It would be a laborious to have to hard code that into every
    batch file, and would also cause problems if a batch file were renamed.

    Looks like there's a similar discussion in this thread, with some code
    samples in it. Looks like there is a more generic solution in there.......

    "run as administrator" changes default directory"+administrator&rnum=1#70df42c859cb0ba0
    Jon, Aug 23, 2007
  10. maya

    Jon Guest

    NB There's an elegant solution from 'cquirke' at the end of that thread ie
    these 2 lines at the start of the batch file...

    CD %~dp0
    Jon, Aug 23, 2007
  11. maya

    Jon Guest

    Oops, my bad, just the one line required....

    CD %~dp0
    Jon, Aug 23, 2007
  12. Awesome! This is superb, thanks!
    Joseph Morales, Aug 23, 2007
  13. maya

    Jon Guest

    You're welcome. NB Both of 2 lines would be required if running a batch
    file from another drive, however.
    Jon, Aug 23, 2007
    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.