Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Set Var=?  (Read 6327 times)

0 Members and 1 Guest are viewing this topic.

nothlit

    Topic Starter


    Intermediate

    Thanked: 2
    Re: Set Var=?
    « Reply #15 on: May 14, 2008, 07:40:39 AM »
    Thank you thank you thank you.

    Yes that will work perfectly. :) Thank you for all your help.

    nothlit

      Topic Starter


      Intermediate

      Thanked: 2
      Re: Set Var=?
      « Reply #16 on: May 15, 2008, 09:55:11 AM »
      Thought of something. After running the BAT file I want to rename or delete the bat file so it can't be run again. I know I could set a variable using the %CD%, but I noticed that is the bat file is run from the command as follows ...

      Code: [Select]
      c:
      cd program files\bat
      "folder\edit.bat"

      If I set local=%CD% in the bat file that is run, I will get 'C:\Program Files\bat' as my local variable. Is there a way around this to force it to take the actual location of the bat file so I can remove it?

      Dias de verano

      • Guest
      Re: Set Var=?
      « Reply #17 on: May 15, 2008, 10:03:01 AM »
      Is there a way around this to force it to take the actual location of the bat file so I can remove it?

      the filename + extension of a batch file is contained in the variable %0 (percent zero). The full name would be

      %~dpnx0

      so, adding quotes in case the path and/or name has spaces,

      del "%~dpnx0"

      would make a batch delete itself, but you would get an error message because you have deleted the running batch from within itself.



      nothlit

        Topic Starter


        Intermediate

        Thanked: 2
        Re: Set Var=?
        « Reply #18 on: May 15, 2008, 11:53:14 AM »
        Ok, so %0 gives me the whole path, but what is the ~dpnx refer to? or is that like sending commands to the delete command?

        nothlit

          Topic Starter


          Intermediate

          Thanked: 2
          Re: Set Var=?
          « Reply #19 on: May 15, 2008, 12:09:37 PM »
          Ok, I think I found something. I did Call /? in a command prompt and find %~dp1 - expands %1 to a drive letter and path only, but still confused on the nx

          Dias de verano

          • Guest
          Re: Set Var=?
          « Reply #20 on: May 15, 2008, 12:35:32 PM »
          They are standard variable modifiers.

          If you typed for /? at the command prompt, didn't you see this?

             %~I         - expands %I removing any surrounding quotes (")
             %~fI        - expands %I to a fully qualified path name
             %~dI        - expands %I to a drive letter only
             %~pI        - expands %I to a path only
             %~nI        - expands %I to a file name only
             %~xI        - expands %I to a file extension only
             %~sI        - expanded path contains short names only
             %~aI        - expands %I to file attributes of file
             %~tI        - expands %I to date/time of file
             %~zI        - expands %I to size of file

          they can be combined to get compound results:

             %~dpI       - expands %I to a drive letter and path only
             %~nxI       - expands %I to a file name and extension only
             %~fsI       - expands %I to a full path name with short names only


          so if %0 is a batch file's own name and extension, then

          %~d0 is its drive letter with a colon
          %~p0 is the full path to its folder
          %~n0 is its bare name without extension
          %~x0 is its extension with a dot before in front of it (usually .bat or .cmd)

          Put them together adding quotes in case the path has spaces and you get:

          "%~dpnx0"




          nothlit

            Topic Starter


            Intermediate

            Thanked: 2
            Re: Set Var=?
            « Reply #21 on: May 15, 2008, 12:58:09 PM »
            Thank you for the explanation. :)