Batch files are text script files that can be run to perform command line or drag and drop operations in DOS.
- DOS command lines can be entered directly into the batch file unquoted with associated switches and parameters.
- Files can be dragged and dropped into batch files to perform batch command operations with them or to them.
- Batch files can be added to the Right Click Open With program menu in Windows. Right click a file type to add or use them.
- 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.
- 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
- BEEP character can be created using the Windows Run command 'Edit'(DOS Editor). Hold down Ctrl and press P then G keys.
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). You may use numbers 0(batch file itself) to 9.
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.
The number 0 refers to the batch file itself and 1 to 9 refer to the order of files dropped or specified in the batch calling command.
@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.
The DOS equivalent of FILES in QBasic that has more file and directory sorting and attribute options. Screen information can be sent to a file using > filename to create a new file or >> filename to append information to an existing file after the DIR command.
* can designate one or more wildcard characters in the file name parameter when more than one file name may exist.
? can designate a single wildcard character in the file name parameter when more than one file may exist.
- DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N] [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
Option Sub Description [drive:][path] Specifies drive, directory [filename] Full or partial file name may include * or ? wildcard characters /A Attributes as listed below: A Files ready for archiving D Directories H Hidden files R Read-only files S System files - attribute prefix means not /B Bare list of file names without header information or drive summary. /C Displays the thousand separator in file sizes(default). - Prefix before C to eliminate the thousands separator. /D Same format as wide(/W) but files are list sorted by column. /L Lists file names in lowercase. /N Long list format filenames listed short before the long name. /O List files in a sorted order: D Sorts by date/time (oldest first) E Sorts by extension alphabetically G Group directories first N Sorts by name alphabetically S Sorts by size with smallest first - Prefix to reverse sort order /P pauses each screen of information. /Q displays owner of the file. /S displays files in specified directory with all subdirectories. /T control what time field is displayed or used for sorting: A Last file access time C File creation time W Last time file was written to /W Wide list display format. /X Long list format filenames listed short before the long name. /4 Displays four-digit years - Prefix before format overrides default DIRCMD setting: /-D /-W /-X
- Note: To use the DIRCMD environment variable to permanently set the wide display format (that is, /w) as the default format, type the following command in your Autoexec.nt file: set dircmd=/w To clear all default settings, type: set dircmd=
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.
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.*" *.*
- Ping can be used to create batch file delays using 1000 milliseconds for one second and a number of pings duration.
- PING [IP address] [-n] [-w] >NUL
IP address use an invalid IP address or 127.0.0.1 (your own IP address) -w milliseconds to wait between pings -n number of pings >NUL send to NUL so it is not displayed
- Example: Delay 1000 milliseconds 10 times equals 10 second delay: PING 127.0.0.1 -w 1000 -n 10 >NUL
BAS files can be compiled anywhere from right click "Open With" menu or dropped into Compile64.BAT file placed in the QB64 folder.
REM WARNING! Copying and pasting BAS files into this BATch file may move EXE file to Owner's 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 Set QB64 folder path: Set "NEWPATH=%QB64%%EXEfile%" REM Display Paths: Echo.QB64 file: %NEWPATH% Echo.Move file: %OLDPATH% REM Change to the QB64 folder path: CD\ REM The QB64 folder drive must be specified when compiling from another Drive! C: CD "%QB64%" REM Compile BAS file: cmd /c start /low QB64.exe -c %1 REM Ping delay batch until EXE file exists. Close manually if compilation fails. :Check IF EXIST "%NEWPATH%" (GOTO Finish) ELSE (PING 127.0.0.1 -w 1000 -n 3 >NUL) GOTO Check :Finish REM Optional 7 second delay if compiler does not finish PING 127.0.0.1 -w 1000 -n 7 >NUL ECHO. MOVE /-Y "%NEWPATH%" "%OLDIR%" REM COPY "%NEWPATH%" "%OLDPATH%" /-Y ECHO. REM Pause GOTO END :END CLS
- Note: Add to Open With Menu! 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 the user folder!