[Batch] The Official Batch File Repository - Learn & ask questions about batch files

Discussion in 'Scripting' started by timesurfer, Oct 11, 2010.

  1. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    Remember I said "Scripting Forum" so here is where I referenced you :p :p
     
  2. tcntad

    tcntad MDL Guru

    Oct 26, 2009
    4,480
    1,500
    150
    Aha ok well allright then :p
     
  3. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #63 timesurfer, Oct 24, 2010
    Last edited: Oct 24, 2010
    (OP)
    Indeed you have been referenced :p
     
  4. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #64 Calistoga, Oct 24, 2010
    Last edited by a moderator: Apr 20, 2017
    Take a batch file located at C:\Windows\script.bat
    You know that this is the location of the batch file, but sometimes you need the batch file to know its own location (C:\Windows\).

    The %cd% variable returns the current working directory of the script, this is not necessarily the same as the location of it.

    When you call app.exe from a batch script it will look for this file several places; the Windows PATH variable (%path%) which includes a lot of special directories, such as System32 and the Windows folder, but it will also look in the scripts working directory.

    This means that if the batch file is located at C:\Windows\, its working directory can be something totally different, such as C:\Users\Calistoga. It's essentially where the script will look when you ask it to work with files without providing a full path. For this reason, %cd% is unreliable if you use the "cd" command in your script, as the the directory you have cd'ed into is the one that will be returned.

    Now if you look at %~dp0 it's a totally different story. This variable will always return the directory where the script is located, so as such, this is a very reliable option (and the preferred).

    So you could probably say that this is about getting the location of the script and where it is currently working (it's desktop so to speak).

    Consider this example, where the script is C:\Windows\script.bat. The script executes the following command:
    Code:
    cd "C:\Users\Calistoga"
    
    Now the %cd% variable will return C:\Users\Calistoga (since this is where we cd'ed to), while the %~dp0 variable will return C:\Windows (because this is where the script is located).

    </walloftext> :smoking:
     
  5. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #65 timesurfer, Oct 24, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Great thanks so what's the cmds title just

    %~dp0

    I just need definition to put on first post

    Tell me how to word it please ;)
     
  6. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #66 Calistoga, Oct 24, 2010
    Last edited: Oct 24, 2010
    Maybe "Get script location and current working directory", eventually just "Obtain script location",

    You're right, it wasn't that easy to put a word on it :p

    Edit: The AutoIt help file defines the macro @ScriptDir (which is the same as %~dp0) as "Directory containing the running script".
     
  7. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    So for instance why do we need the location of the script we're running. Example please in .bat file

    Thanks
     
  8. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #68 Calistoga, Oct 24, 2010
    Last edited by a moderator: Apr 20, 2017
    Now you're asking a difficult question :p Let's say you have an app that takes one parameter (a directory path), this app is going to do something with or in that folder. You need it to work with the folder where your script is located, then this variable is useful.

    As for a specific example...
    You have two files in a folder. script.bat and app.exe
    You cd to another directory and try to launch "app.exe" this will fail as the working directory is not where the app.exe is located. Easy fix?
    Code:
    %~dp0app.exe
    This code will always work.
     
  9. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #69 timesurfer, Oct 24, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    I just started to get it now excellent example. so I've changed first post So tell me if that works for you dude as the way you'd have it be? :p
     
  10. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    Yep that's excellent :cool:
     
  11. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #71 timesurfer, Oct 24, 2010
    Last edited: Oct 24, 2010
    (OP)
    Maybe you get scriptor MDL title

    MDL Scriptor

    I think so...

    And any idea's you have for the repo let me know!
     
  12. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,330
    1,377
    90
    I should mention that Powershell is becoming more and more powerful and can even create GUI applications.

    open Windows Search
    type Powershell
    and enter this is in the command line.

    [reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
    $form= New-Object Windows.Forms.Form

    $button = New-Object Windows.Forms.Button

    $button.text = "Click Here!"

    $form.controls.add($button)

    $form.ShowDialog()
     
  13. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    [​IMG]

    Cna you explain a little more about powershell and what's it's used for mostly? Any comparisons to define it?
     
  14. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,330
    1,377
    90
    I will provide some more information tomorrow as time is short tonight and tomorrow is a holiday so I will have more time then.
     
  15. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #75 Calistoga, Oct 28, 2010
    Last edited by a moderator: Apr 20, 2017
    Looping Statements: FOR

    The FOR-loop
    The art of repeating a piece of code.

    There are times when you need to process a list of items. Such a list can be for example a list of files in a folder, or a text file containing a list of URL's.

    Consider the following example:
    Code:
    @echo off
    
    for %%i in (*.*) do echo %%i
    
    pause
    The part we are interested in is the line starting with for. When executed in a batch file, this code will print a list of all files located in the current working directory.

    %%i is the iteration variable. Each time the loop evaluates, this variable is updated to the current value.

    First iteration: %%i = a_file.txt
    Second iteration: %%i = b_file.txt


    The (*.*) is the files that will be iterated. Replace the second asterisk with "txt" (*.txt) and the loop will only process txt-files.

    What you see after do (do echo %%i) is the function that is executed at each iteration for the current value of the iteration variable. You could replace "echo" with "del" and it would delete all files in that directory.

    To better understand how the loop works, consider this C# example:
    Code:
    for (int i = 1; i <= 5; i++)
            {
                Console.WriteLine(i);
            }
    
    This code will continue to evaluate until the iteration variable (i) is bigger than 5. The current value of the iteration variable will be printed at each loop. In other words: Evaluate the expression until it is no longer true. At each loop, execute the code inside the brackets and start over again.

    The following batch example will print all numbers from 1 to 5:
    Code:
    @echo off
    
    for /l %%i in (1,1,5) do echo %%i
    
    pause
    Notice the /L parameter. This enables the for-loop to iterate through all numbers from 1-5. Why? Consider the following:
    Code:
    FOR /L %variable IN (start,step,end) DO command [command-parameters]
    The first number we used, 1, is the initial value of the iteration variable (start). The second value is the number that should be added to the iteration variable (step) until it finally reaches the last value (end), which is where we want to end up.
     
  16. odinthegreatone

    odinthegreatone MDL Junior Member

    Dec 4, 2008
    80
    2
    0
    #76 odinthegreatone, Oct 31, 2010
    Last edited by a moderator: Apr 20, 2017
    Thanks. Tried the code, but the folder created doesn't display the date properly, just shows 0-2010 instead of 31-10-2010. I was wondering if their is a way to fix that. (This was used in Windows 7 64 Bit)
     
  17. MasterDisaster

    MasterDisaster MDL Expert

    Aug 29, 2009
    1,256
    674
    60
    #77 MasterDisaster, Oct 31, 2010
    Last edited by a moderator: Apr 20, 2017
    If the %date% variable returns the date as 31-10-2010 then you can use
    Code:
    set dest=%date%
    mkdir %dest%
    xcopy /is %source% %dest%
    
    For me the the %date% returns Sat 10/30/2010, so I had to convert it to 10-30-2010.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  18. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    Is it possible to remove minimize and maximize buttons in console?

    Thank you
     
  19. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    #79 timesurfer, Nov 17, 2010
    Last edited by a moderator: Apr 20, 2017
    (OP)
    The "color" command

    The "color" command

    "The screen colors can be configured from the command line itself with the command "color" followed by a two-digit hexadecimal number. The first digit determines the background and the second determines the text color. The table below shows the relationship between the hex numbers and colors.

    Table I. Hexadecimal color codes

    0 = Black8 = Gray
    1 = Blue9 = Light Blue
    2 = GreenA = Light Green
    3 = AquaB = Light Aqua
    4 = RedC = Light Red
    5 = PurpleD = Light Purple
    6 = YellowE = Light Yellow
    7 = White F = Bright White

    For example the command color "0C" will give a black background with red text. Settings made this way apply only to the current session. Entering "color" with no argument will return the system to the starting colors."

    [​IMG]

    Example:

    Code:
    @echo off
    @color 0C
    title Hello World...
    :MAINMENU
    mode con: cols=45 lines=4
    CLS 
    echo.Hello World...
    echo. 
    echo.
    slmgr /dlv  
    pause
    
    To open your batch file right click and choose edit...
     
  20. G00GLE_ME

    G00GLE_ME MDL Novice

    May 28, 2010
    7
    1
    0
    cool col yea idk how to do this and thanks for sharing the information i wonder what colorful things i can create