Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 523 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hidden features of Windows batch files

#1
What are some of the lesser know, but important and useful features of Windows batch files?

Guidelines:

- One feature per answer
- Give both a short **description** of the feature and an **example**, not just a link to documentation
- Limit answers to **native funtionality**, i.e., does not require additional software, like the *Windows Resource Kit*

Clarification: We refer here to scripts that are processed by cmd.exe, which is the default on WinNT variants.

(See also: [Windows batch files: .bat vs .cmd?](

[To see links please register here]

))

Reply

#2
I have always found it difficult to read comments that are marked by a keyword on each line:

REM blah blah blah

Easier to read:

:: blah blah blah
Reply

#3
The path (with drive) where the script is : ~dp0

set BAT_HOME=%~dp0
echo %BAT_HOME%
cd %BAT_HOME%

Reply

#4
Variable substrings:

> set str=0123456789
> echo %str:~0,5%
01234
> echo %str:~-5,5%
56789
> echo %str:~3,-3%
3456
Reply

#5
Integer arithmetic:

> SET /A result=10/3 + 1
4
Reply

#6
I find the ease with which you can redirect the output of commands to files extremely useful:

DIR *.txt > tmp.txt
DIR *.exe >> tmp.txt

Single arrow creates, or overwrites the file, double arrow appends to it. Now I can open tmp.txt in my text editor and do all kinds of good stuff.
Reply

#7
Delayed expansion of variables (with substrings thrown in for good measure):

@echo off
setlocal enableextensions enabledelayedexpansion
set full=/u01/users/pax
:loop1
if not "!full:~-1!" == "/" (
set full2=!full:~-1!!full2!
set full=!full:~,-1!
goto :loop1
)
echo !full!
endlocal
Reply

#8
[The FOR command][1]! While I hate writing batch files, I'm thankful for it.

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces.
Notice the for body statements reference %i to get the 2nd token, %j to get the 3rd token, and %k to get all remaining tokens after the 3rd.

You can also use this to iterate over directories, directory contents, etc...

[1]:

[To see links please register here]

Reply

#9
Sneaky trick to wait N seconds (not part of cmd.exe but isn't extra software since it comes with Windows), see the ping line. You need N+1 pings since the first ping goes out without a delay.

echo %time%
call :waitfor 5
echo %time%
goto :eof
:waitfor
setlocal
set /a "t = %1 + 1"
>nul ping 127.0.0.1 -n %t%
endlocal
goto :eof
Reply

#10
Subroutines (outputs 42):

@echo off
call :answer 42
goto :eof
:do_something
echo %1
goto :eof

and subroutines returning a value (outputs 0, 1, 2, and so on):

@echo off
setlocal enableextensions enabledelayedexpansion
call :seq_init seq1
:loop1
if not %seq1%== 10 (
call :seq_next seq1
echo !seq1!
goto :loop1
)
endlocal
goto :eof

:seq_init
set /a "%1 = -1"
goto :eof
:seq_next
set /a "seq_next_tmp1 = %1"
set /a "%1 = %seq_next_tmp1% + 1"
set seq_next_tmp1=
goto :eof
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through