Batch Files

From QB64 Wiki

Revision as of 21:30, 30 September 2011 by Clippy (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Batch files are text script files that can be run to perform command line operations in DOS.


  • Command lines can be entered directly into the batch file(unquoted) with associated switches and parameters.
  • CALL can be used to run a program or other batch file that will return to the original batch file after it has completed.
  • RUN can be used with Qbasic to run a BAS file module: CMD /C start QB.EXE /RUN %~nx1 /L
  • ECHO will display text and commands executed to the screen. ECHO OFF or @ will not. Use @ECHO OFF to display nothing.
  • REM can be used for code comments or to prevent code from being executed. Do not use an apostrophe!
  • GOTO can be used to send command flow to another portion of the batch file. A colon is placed before the line label: :END
  • Quotation marks may be required around path folder or file names that contain spaces.
  • Modifiers can be used when a file will be dropped into the batch file or the batch is run from the Windows Open With menu.
  • BEEP character can be created using the Windows Run command 'Edit'(DOS Editor). Hold down Ctrl and press P then G keys.


Contents

Parameter Modifiers:

Windows XP and newer PCs can also use modifiers with batch parameters. Modifiers use current drive and directory information to expand the batch parameter as a partial or complete file or directory name. To use a modifier, type the percent (%) character followed by a tilde (~) character, and then type the appropriate modifier (that is, %~modifier).

The following table lists the modifiers you can use in expansion:

Modifier Description %~1 Expands %1 to path and filename. QB call shows filename only. %~f1 Expands %1 to the fully qualified path with file name. %~d1 Expands %1 to a drive letter without a \ slash. C: %~p1 Expands %1 to a path with \ at both ends (NO Drive letter). %~n1 Expands %1 to a file first name only.(No extension or dot) %~x1 Expands %1 to the file extension only with the dot. %~s1 Expands %1 to path with short DOS folder and file names only. %~a1 Expands %1 to file attributes. --a------ %~t1 Expands %1 to date and time of file. 01/03/2011 01:23 PM %~z1 Expands %1 to size of file. 1002 %~$PATH:1 Searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found, this modifier expands to the empty string.

The following table lists possible combinations of modifiers and qualifiers that you can use to get compound results.

Modifier Description %~dp1 Expands %1 to a drive letter and path. %~nx1 Expands %1 to a file name and extension. %~snx1 Expands to short filename and extension. %~dp$PATH:1 Searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found.

Example:

@Echo Off IF "%PATH%" == "" GOTO NOPATH :YESPATH @ECHO The PATH environment variable was detected. PATH=C:\DOS;%PATH% GOTO END :NOPATH @ECHO The PATH environment variable was NOT detected. PATH=C:\DOS; GOTO END :END

Note: In the previous examples, you can replace %1 and PATH with other batch parameter values. The %* modifier is a unique modifier that represents all arguments passed in a batch file.
You cannot use this modifier in combination with the %~ modifier. The %~ syntax must be terminated by a valid argument value.You cannot manipulate batch parameters in the same manner that you can manipulate environment variables. You cannot search and replace values or examine substrings. However, you can assign the parameter to an environment variable, and then manipulate the environment variable.

Findstr:

The find command lets you search one file at a time for a string, but the findstr command is more versatile with the following switches.

Syntax:

findstr [/b] [/e] [/l] [/r] [/s] [/i] [/x] [/v] [/n] [/m] [/o] [/p] [/offline] [/g:file] [/f:file] [/c:string] [/d:dirlist] [/a:ColorAttribute] [strings] [[Drive:][Path]FileName[...]]

Options Description /b Matches pattern if at the start of a line /e Matches pattern if at the end of a line /l Searches literally /r Uses text as a regular expression (default) /s Searches current directory and all subdirectories /i Ignores case /x Selects lines that are an exact match /v Selects lines that don’t match /n Displays the line number before the matched line /m Displays only the matching filenames /o Displays the offset of the match before the matched line /p Skips files with non-printable characters. /offline Processes files with offline attribute set /g:<file> Gets the search string from the specified file (e.g., /g:argument.txt) /f:<file> Gets the file list from the specified file (e.g., /f:filelist.txt) /c:"<string>" Uses a specified literal string (e.g., /c:"string") /d: dirlist Searches a comma-delimited list of directories. /a: ColorAttribute Specifies color attributes with two hexadecimal digits. strings Specified text to be searched for in FileName. [Drive:][Path]FileName[...] Specifies a file or files to search.

Note: Use spaces to separate multiple search strings unless the argument is prefixed with /c.
  • The command to search for Windows, NT, or FAQ in filename.txt: findstr "Windows NT FAQ" filename.txt
  • The command to would search for Windows NT FAQ in filename.txt: findstr /c:"Windows NT FAQ" filename.txt
  • To search for "hello" or "there" in file filename.txt, type: findstr "hello there" filename.txt
  • To search for "hello there" in file filename.txt, type: findstr /c:"hello there" filename.txt
  • To find all occurrences of the word "Windows" (with an initial capital W) in the file filename.txt, type: findstr Windows filename.txt
  • To search every file in the current directory and all subdirectories that contained the word Windows, regardless of the letter case, type: findstr /s /i Windows *.*
  • To find all occurrences of lines that contain the word "FOR", preceded by any number of spaces, (as in a computer program loop), and to include the line number where each occurrence is found, type the following: findstr /b /n /c:" *FOR" *.bas
  • If you want to search for several different items in the same set of files, create a text file that contains each search criterion on a new line. You can also list the exact files you want to search in a text file. To use the search criteria in the file Finddata.txt, search the files listed in Filelist.txt, and then store the results in the file Results.out, type: findstr /g:finddata.txt /f:filelist.txt > results.out
  • Assuming you wanted to find every file in the current directory and all sub-directories that contained the word computer, regardless of the letter case. To list every file containing the word computer, type the following: findstr /s /i /m "\<computer\>" *.*
  • Now assume you want to find not only the word "computer," but also any other words that begin with the letters comp, such as "compliment" and "compete. " type the following: findstr /s /i /m "\<comp.*" *.*


QB64 Examples

BAS files can be compiled anywhere from right click "Open With" menu or dropped into Compile64.BAT file placed in the QB64 folder.

@Echo off Title QB64 Compiler color 5F REM BATCH file MUST be in the QB64 folder! Set QB64=%~dp0 Echo.QB64 Path: %QB64% REM Get filename only: Set FILE=%~nx1 Echo.BAS file: %FILE% REM Replace BAS with EXE: SET BASfile=%FILE% SET EXEfile=%BASfile:.BAS=.EXE% Echo.EXE file: %EXEfile% Echo. REM Get BAS file path: Set OLDIR=%CD% Set "OLDPATH=%OLDIR%\%EXEfile%" REM Change to the QB64 folder path: CD "%QB64%" REM Set QB64 folder path: Set "NEWPATH=%QB64%%EXEfile%" REM Display Paths: Echo.QB64 file: %NEWPATH% Echo.Move file: %OLDPATH% REM Compile BAS file: cmd /c start /low QB64.exe -c %1 REM Skip COPY if compiling BAS file in the QB64 folder: IF "%NEWPATH%" == "%OLDPATH%" GOTO END ECHO. ECHO. ECHO PRESS a Key to MOVE file when Compile has completed! ECHO. Pause ECHO. MOVE /-Y "%NEWPATH%" "%OLDIR%" REM COPY "%NEWPATH%" "%OLDPATH%" /-Y ECHO. Pause GOTO END :END CLS

Note: When the BAS file is not in the QB64 folder, the batch file will wait to move the EXE compilation back to the original folder. BAS files located outside of the QB64 folder dropped into this batch file may try to move the compiled file to a default user folder!
Copy and paste into a text editor such as Notepad, Save As BAT and Save As Type: All Files.


Remember that QB64 programs require the DLL files be with the EXE file or in the System32 folder!

Reference:

See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page