Print service provided by iDogiCat: http://www.idogicat.com/
home logo





Home > IT > System Administration > Frequently Used DOS Commands

Frequently Used DOS Commands

iDog

Loop

do some command on each of the files matching a certain wild card in a dir: the command uses the full pathes of the files:

for %%i in (<subdir>\*.*) do <command containing %%i>

do some command on each of the files matching a certain wild card in a dir: the command uses only the filenames of the files:

for %%i in (<subdir>\*.*) do <command containing %%~nxi>

If there are more than one commands to execute, they should be wrapped by "()".

for %%i in (*.txt) do (
    echo "next:"
    echo %%i
)

Note that when we want to build a list, we have to do it in following way:

setlocal enabledelayedexpansion

for %%i in (lib\*.jar) do (
    set CLASSPATH=!CLASSPATH!;%%i
)

if we do it in following way:

set CLASSPATH=%CLASSPATH%;%%i

CLASSPATH is only expanded once, and the result is that only the last file is added. So we need to enable the delayed expansion, and use "!" to replace "%" to refer to a variable.