Return to site

Echo Batchfiles

broken image


Here is how to use colored text in batch files!www.mediafire.com/download/4o1h3lnyauf4c­dh/colorsettings.bat. @echo off rem start at the top of the tree to visit and loop though each directory for /r%%a in (.) do ( rem enter the directory pushd%%a echo In directory: cd rem leave the directory popd ) Notes. ECHO defaults to OFF at the command line. The command-line ECHO is most useful when you are learning how to use advanced features. Similarly, executing ECHO ON in a batch file enables, and ECHO OFF disables echoing of batch file commands. ECHO defaults to ON in batch files. The current ECHO state is inherited by called batch files. Batch files for DOS, OS/2, Windows 95/98, NT 4, 2000 and XP Technically, DOS provides just eight basic batch file commands — CALL, ECHO, FOR, GOTO, IF, PAUSE, REM, and SHIFT — plus a tiny assortment of miscellaneous doodads: replaceable parameters, environment variables, labels.

Batchfiles

Echo Variables In Batch Files

Windows and MS-DOS batch files use variables in the same way. They both support system variables and user variables. User variables may be defined user variables or parameter user variables. Defined user variables are directly created by the user. Parameter user variables are passed to the batch file as command line parameters, for example:

mybatch.bat param1 param2 param3

System variables exist in the operating system at all times. They include variables for the time, the date, the Windows folder, and other settings. To view a list of system variables (and possibly defined user variables), run the set command at the command prompt. To view the contents of a system variable, simply use the echo command to display the contents. For example:

echo %SystemRoot%

This command will show the Windows directory (called the System Root in Windows terminology). System variables can be used in batch files in place of manually typed information. For example, if you want to copy a file to the Windows directory, it is better to use the SystemRoot variable instead of manually typing C:Windows (which may not be the Windows directory on some computers to which you copy the batch file).

Echo Batchfiles

Note that the variable in batch files or at the CMD Windows Command Prompt is surrounded by percent signs. While we say the variable is SystemRoot, it is referenced as %SystemRoot%. Interestingly, when creating a variable in batch files or at the Windows Command Line, you do not surround it with percent signs. If you do, the ACTUAL variable will include the percent signs and things get messy. For example, to create a variable named horse that contains the value silver, use this command:

set horse=silver

Additionally, note that the variable string contents are not surrounded with quotation marks. The quotation marks are not needed and, if used, become part of the variable contents. This is even true when spaces are included. For example, the following command works just fine when using variables in batch files or at the command prompt:

set horse=hi ho silver

So, the set command is used to create user defined variables within and outside of batch files.

You can also pass parameters to batch files. They parameters are then accessed as %1 through %9. You can do some fancy work (with the shift command) to use more command line parameters, but for most batch files, this process works just fine.

Syntax

Batchfiles
  • for /l %%p in (startNumber, increment, endNumber) do command
  • for /f %%p in (filename) do command
  • for /f %%p in ('textStrings') do command
  • for /f %%p in ('command') do command
  • for /r drive:path %%p in (set) do command
  • for /d %%p in (directory) do command

Remarks

The for command accepts options when the /f flag is used. Here's a list of options that can be used:

  • delims=x Delimiter character(s) to separate tokens

  • skip=n Number of lines to skip at the beginning of file and text strings

  • eol=; Character at the start of each line to indicate a comment

  • tokens=n Numbered items to read from each line or string to process

  • usebackq Use another quoting style:

    Use double quotes for long file names in 'files'

    Use single quotes for 'textStrings'

    Use back quotes for `command`

Batch Files Echo Newline

Looping through each line in a files set

The following will echo each line in the file C:scriptstestFile.txt. Blank lines will not be processed.

File

More advanced example shows, how derived in FOR loop from a restricted files set data can be used to redirect batch execution, while saving the searched content to a file:

Note, how long command strings are split into several code lines, and command groups are separated by parentheses.

Recursively Visit Directories in a Directory Tree

for /r command can be used to recursively visit all the directories in a directory tree and perform a command.

Notes:

  • for /r - Loop through files (Recurse subfolders).
  • pushd - Change the current directory/folder and store the previous folder/path for use by the POPD command.
  • popd - Change directory back to the path/folder most recently stored by the PUSHD command.

Renaming all files in the current directory

The following uses a variable with a for loop to rename a group of files.

By defining the variable name %%j and associating it with all current files (*.*), we can use the variable in a for loop to represent each file in the current directory.

Every iteration (or pass) through the loop thereby processes a different file from the defined group (which might equally have been any group, e.g. *.jpg or *.txt).

In the first example, we substitute text: the text string 'old' is replaced by the text string 'new' (if, but only if, the text 'old' is present in the file's name).

In the second example, we add text: the text 'Prefix ' is added to the start of the file name. This is not a substitution. This change will be applied to all files in the group. Clydesdale foal cropshidden hills stable.

In the third example, again we add text: the text ' Suffix' is added to the end of the file name. Again, this is not a substitution. This change will be applied to all files in the group.

The final line actually handles the renaming.

Newline

Echo Variables In Batch Files

Windows and MS-DOS batch files use variables in the same way. They both support system variables and user variables. User variables may be defined user variables or parameter user variables. Defined user variables are directly created by the user. Parameter user variables are passed to the batch file as command line parameters, for example:

mybatch.bat param1 param2 param3

System variables exist in the operating system at all times. They include variables for the time, the date, the Windows folder, and other settings. To view a list of system variables (and possibly defined user variables), run the set command at the command prompt. To view the contents of a system variable, simply use the echo command to display the contents. For example:

echo %SystemRoot%

This command will show the Windows directory (called the System Root in Windows terminology). System variables can be used in batch files in place of manually typed information. For example, if you want to copy a file to the Windows directory, it is better to use the SystemRoot variable instead of manually typing C:Windows (which may not be the Windows directory on some computers to which you copy the batch file).

Note that the variable in batch files or at the CMD Windows Command Prompt is surrounded by percent signs. While we say the variable is SystemRoot, it is referenced as %SystemRoot%. Interestingly, when creating a variable in batch files or at the Windows Command Line, you do not surround it with percent signs. If you do, the ACTUAL variable will include the percent signs and things get messy. For example, to create a variable named horse that contains the value silver, use this command:

set horse=silver

Additionally, note that the variable string contents are not surrounded with quotation marks. The quotation marks are not needed and, if used, become part of the variable contents. This is even true when spaces are included. For example, the following command works just fine when using variables in batch files or at the command prompt:

set horse=hi ho silver

So, the set command is used to create user defined variables within and outside of batch files.

You can also pass parameters to batch files. They parameters are then accessed as %1 through %9. You can do some fancy work (with the shift command) to use more command line parameters, but for most batch files, this process works just fine.

Syntax

  • for /l %%p in (startNumber, increment, endNumber) do command
  • for /f %%p in (filename) do command
  • for /f %%p in ('textStrings') do command
  • for /f %%p in ('command') do command
  • for /r drive:path %%p in (set) do command
  • for /d %%p in (directory) do command

Remarks

The for command accepts options when the /f flag is used. Here's a list of options that can be used:

  • delims=x Delimiter character(s) to separate tokens

  • skip=n Number of lines to skip at the beginning of file and text strings

  • eol=; Character at the start of each line to indicate a comment

  • tokens=n Numbered items to read from each line or string to process

  • usebackq Use another quoting style:

    Use double quotes for long file names in 'files'

    Use single quotes for 'textStrings'

    Use back quotes for `command`

Batch Files Echo Newline

Looping through each line in a files set

The following will echo each line in the file C:scriptstestFile.txt. Blank lines will not be processed.

More advanced example shows, how derived in FOR loop from a restricted files set data can be used to redirect batch execution, while saving the searched content to a file:

Note, how long command strings are split into several code lines, and command groups are separated by parentheses.

Recursively Visit Directories in a Directory Tree

for /r command can be used to recursively visit all the directories in a directory tree and perform a command.

Notes:

  • for /r - Loop through files (Recurse subfolders).
  • pushd - Change the current directory/folder and store the previous folder/path for use by the POPD command.
  • popd - Change directory back to the path/folder most recently stored by the PUSHD command.

Renaming all files in the current directory

The following uses a variable with a for loop to rename a group of files.

By defining the variable name %%j and associating it with all current files (*.*), we can use the variable in a for loop to represent each file in the current directory.

Every iteration (or pass) through the loop thereby processes a different file from the defined group (which might equally have been any group, e.g. *.jpg or *.txt).

In the first example, we substitute text: the text string 'old' is replaced by the text string 'new' (if, but only if, the text 'old' is present in the file's name).

In the second example, we add text: the text 'Prefix ' is added to the start of the file name. This is not a substitution. This change will be applied to all files in the group. Clydesdale foal cropshidden hills stable.

In the third example, again we add text: the text ' Suffix' is added to the end of the file name. Again, this is not a substitution. This change will be applied to all files in the group.

The final line actually handles the renaming.

Iteration

This line will iterate from 1 to 39, increasing by 2 each time.

The first parameter, 1, is the starting number.

The second parameter, 2, is the increment.

Batch Echo All Files In Folder

The third parameter, 40, is the maximum.

Batch Echo To Multiple Files






broken image