Difference between revisions of "SHELL"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
m (Text replacement - "==QBasic/QuickBASIC==" to "{{PageQBasic}}")
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
The '''SHELL''' statement allows a program to use [[STRING]] OS command lines in Windows, Mac OSX and Linux.
+
The [[SHELL]] statement allows a program to run external programs or command line statements in Windows, macOS and Linux.
  
  
''QB'' {{PageSyntax}} SHELL [''DOSCommand$'']
+
{{PageSyntax}}
 +
: [[SHELL]] [{{Parameter|DOSCommand$}}]
 +
: [[SHELL]] ['''_DONTWAIT'''] ['''_HIDE'''] [{{Parameter|DOSCommand$}}]
  
''QB64'' {{PageSyntax}} SHELL [{{KW|_DONTWAIT}}] [{{KW|_HIDE}}] [''DOSCommand$'']
 
  
 +
{{PageDescription}}
 +
* If the ''DOSCommand$'' [[STRING]] parameter isn't used, the "command console" is opened and execution is halted until the user closes it manually.
 +
* If [[_DONTWAIT]] is used, the '''QB64''' program doesn't wait for the SHELLed program/command to end.
 +
* When the [[_HIDE]] action is used, the [[CONSOLE|console]] window is hidden and screen info can be "redirected" (using redirection characters like >) to a file (recommended).
 +
* Commands are external commands, according to the user's operating system, passed as [[STRING|strings]] enclosed in quotes or string variables.
 +
* Commands can be a mixture of [[STRING|strings]] and string variables added together using the + [[concatenation]] operator.
 +
* Command text can be in upper or lower case. Use single spacing between items and options.
 +
* '''QB64''' automatically uses CMD /C when using [[SHELL]], but it is allowed in a command string. {{text|Note: CMD alone may lock up program.|red}}
 +
** '''Note: Some commands may not work without adding CMD /C to the start of the command line.'''
 +
* '''QB64''' program screens will not get distorted, minimized or freeze the program like QBasic fullscreen modes would.
 +
* '''QB64''' can use long path folder names and file names and [[SHELL]] command lines can be longer than 124 characters.
 +
* In Windows, use additional [[CHR$]](34) quotation marks around folder or file names that contain spaces.
 +
* For other operating systems, both the quotation mark character and the apostrophe can be used to enclose a file name that contains spaces.
 +
*'''NOTE: Use [[CHDIR]] instead of CD as SHELL commands cannot affect the current program path.'''
  
  
* If the ''[[DOS]]Command$'' [[STRING]] parameter isn't used the "command console" is opened.
+
{{PageQBasic}}
* If [[_DONTWAIT]] is used the '''QB64''' program doesn't wait for the SHELLed program/command to end.
+
* '''QBasic BAS files could be run like compiled programs without returning to the IDE when [[SYSTEM]] was used to [[END|end]] them.'''
* When the [[_HIDE]] action is used, the [[CONSOLE|console]] window is hidden and screen info can be "redirected"(>) to a file (recommended).
+
* A user would invoke it with {{InlineCode}}SHELL "QB.EXE /L /RUN program.BAS"{{InlineCodeEnd}}
* Commands are external [[DOS]] commands as [[STRING|strings]]  enclosed in quotes or string variables.
 
* Commands can be a mixture of [[STRING|strings]] and string variables added together using the + [[concatenation]] operator.
 
* Command text can be in upper or lower case. Use single spacing between items and options.
 
* '''QB64''' automatically uses CMD /C when using [[SHELL]], but is allowed in command. {{text|Note: CMD alone may lock up program!|red}}
 
:: '''Note: Some commands may not work without adding CMD /C to the start of the command line.'''
 
* '''QB64''' program screens will not get distorted, minimized or freeze the program like Qbasic fullscreen modes will.
 
* '''QB64''' can use long path folder names and file names and [[SHELL]] command lines can be longer than 124 characters!
 
* In Windows use additional [[CHR$]](34) quotation marks around folder or file names that contain spaces.
 
*'''NOTE: Use [[CHDIR]] instead of CD as SHELL commands cannot affect the current program path!'''
 
* '''Qbasic BAS files can be run like compiled programs without returning to the [[IDE]] when [[SYSTEM]] is used to [[END|end]] them!'''
 
<center>'''{{text|SHELL "QB.EXE /L /RUN program.BAS"|green}}'''</center>
 
  
  
 +
{{PageExamples}}
 
''Example 1:'' When working with file or folder names with spaces, add quotation marks around the path and/or file name with [[CHR$]](34).
 
''Example 1:'' When working with file or folder names with spaces, add quotation marks around the path and/or file name with [[CHR$]](34).
 
{{CodeStart}} '' ''
 
{{CodeStart}} '' ''
Line 29: Line 34:
 
{{Cl|SHELL}} "start Notepad temp.dir" ' display temp file contents in Notepad window '' ''
 
{{Cl|SHELL}} "start Notepad temp.dir" ' display temp file contents in Notepad window '' ''
 
{{CodeEnd}}
 
{{CodeEnd}}
:{{small|Contents of ''temp.dir'' text file:}}
+
:Contents of ''temp.dir'' text file:
{{TextStart}}Free Cell.ico
+
{{OutputStart}}
{{TextEnd}}
+
Free Cell.ico
 +
{{OutputEnd}}
  
  
''Example 2:'' Opening a Windows program(Notepad) to read or print a Basic created text file.
+
''Example 2:'' Opening a Windows program (Notepad) to read or print a Basic created text file.
 
{{CodeStart}}
 
{{CodeStart}}
 
{{Cl|INPUT}} "Enter a file name to read in Notepad: ", filename$
 
{{Cl|INPUT}} "Enter a file name to read in Notepad: ", filename$
Line 42: Line 48:
 
{{CodeEnd}}
 
{{CodeEnd}}
  
:''Explanation:'' Notepad is an easy program to open in Windows. No path is needed! Windows NT computers, including XP, use CMD /C where older versions of DOS don't require any command reference. The top command opens Notepad in a normal window for a user to view the file. They can use Notepad to print it. The second command places Notepad file in the taskbar and prints it automatically. The filename variable is added by the program using proper spacing.  
+
:''Explanation:'' Notepad is an easy program to open in Windows as no path is needed. Windows NT computers, including XP, use CMD /C where older versions of DOS don't require any command reference. The top command opens Notepad in a normal window for a user to view the file. They can use Notepad to print it. The second command places Notepad file in the taskbar and prints it automatically. The filename variable is added by the program using proper spacing.  
  
 
::*'''Start''' is used to allow a Basic program to run without waiting for Notepad to be closed.
 
::*'''Start''' is used to allow a Basic program to run without waiting for Notepad to be closed.
 
::* '''/min''' places the window into the taskbar. '''/max''' is fullscreen and no option is a normal window.
 
::* '''/min''' places the window into the taskbar. '''/max''' is fullscreen and no option is a normal window.
::* Notepad's '''/p''' option prints the file contents. Even with USB printers!
+
::* Notepad's '''/p''' option prints the file contents, even with USB printers.
  
:'''Note: A fullscreen [[SCREEN (statement)|SCREEN]] mode must be changed after a Windows program is opened in Qbasic ONLY!'''
 
:: Besides minimizing a QBasic program, fullscreen modes will lose the current screen information and freeze. Switch to another screen mode and back to the one you were in immediately after the SHELL! Screen 0 windows will work OK.
 
  
  
Line 55: Line 59:
 
{{CodeStart}}
 
{{CodeStart}}
 
  currentpath$ = Path$ ' function call saves a path for later program use
 
  currentpath$ = Path$ ' function call saves a path for later program use
  PRINT currentpath$
+
  {{Cl|PRINT}} currentpath$
  
 
  {{Cl|FUNCTION}} Path$   
 
  {{Cl|FUNCTION}} Path$   
   {{Cl|SHELL}} {{Cl|_HIDE}} "CD > D0S-DATA.INF"   'code to hide window in '''QB64'''
+
   {{Cl|SHELL}} {{Cl|_HIDE}} "CD > D0S-DATA.INF" 'code to hide window in '''QB64'''
   {{Cl|OPEN}} "D0S-DATA.INF" FOR {{Cl|APPEND}} AS #1  'this may create the file
+
   {{Cl|OPEN}} "D0S-DATA.INF" {{Cl|FOR (file statement)|FOR}} {{Cl|APPEND}} {{Cl|AS}} #1  'this may create the file
 
         L% = {{Cl|LOF}}(1)          'verify that file and data exist
 
         L% = {{Cl|LOF}}(1)          'verify that file and data exist
 
   {{Cl|CLOSE}} #1   
 
   {{Cl|CLOSE}} #1   
 
   {{Cl|IF}} L% {{Cl|THEN}}                      'read file if it has data
 
   {{Cl|IF}} L% {{Cl|THEN}}                      'read file if it has data
     {{Cl|OPEN}} "D0S-DATA.INF" FOR {{Cl|INPUT (file mode)|INPUT}} AS #1
+
     {{Cl|OPEN}} "DOS-DATA.INF" {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1
 
     {{Cl|LINE INPUT (file statement)|LINE INPUT}} #1, line$          'read only line in file
 
     {{Cl|LINE INPUT (file statement)|LINE INPUT}} #1, line$          'read only line in file
 
     Path$ = line$ + "\"            'add slash to path so only a filename needs added later
 
     Path$ = line$ + "\"            'add slash to path so only a filename needs added later
 
     {{Cl|CLOSE}} #1
 
     {{Cl|CLOSE}} #1
 
   {{Cl|ELSE}} : Path = ""                'returns zero length string if path not found
 
   {{Cl|ELSE}} : Path = ""                'returns zero length string if path not found
   END IF
+
   {{Cl|END IF}}
 
   {{Cl|KILL}} "D0S-DATA.INF"              'deleting the file is optional
 
   {{Cl|KILL}} "D0S-DATA.INF"              'deleting the file is optional
 
  {{Cl|END FUNCTION}} '' ''
 
  {{Cl|END FUNCTION}} '' ''
Line 74: Line 78:
 
{{small|Code by Ted Weissgerber}}
 
{{small|Code by Ted Weissgerber}}
 
:''Explanation:'' The '''SHELL "CD"''' statement requests the current working path. This info is normally printed to the screen, but the '''>''' pipe character sends the information to the DOS-DATA.INF file instead('''QB64''' can use [[_HIDE]] to not display the DOS window). The function uses the [[OPEN]] FOR [[APPEND]] mode to check for the file and the data([[INPUT (file mode)|INPUT]] would create an error if file does not exist). The current path is listed on one line of the file. The file is opened and [[LINE INPUT (file statement)|LINE INPUT]] returns one line of the file text. The function adds a "\" so that the Path$ returned can be used in another file statement by just adding a file name. Save the Path$ to another variable for later use when the program has moved to another directory.
 
:''Explanation:'' The '''SHELL "CD"''' statement requests the current working path. This info is normally printed to the screen, but the '''>''' pipe character sends the information to the DOS-DATA.INF file instead('''QB64''' can use [[_HIDE]] to not display the DOS window). The function uses the [[OPEN]] FOR [[APPEND]] mode to check for the file and the data([[INPUT (file mode)|INPUT]] would create an error if file does not exist). The current path is listed on one line of the file. The file is opened and [[LINE INPUT (file statement)|LINE INPUT]] returns one line of the file text. The function adds a "\" so that the Path$ returned can be used in another file statement by just adding a file name. Save the Path$ to another variable for later use when the program has moved to another directory.
 +
: In '''QB64''' you can simply use the [[_CWD$]] statement for the same purpose of the example above.
  
  
''Example 4:'' Determining if a drive or path exists. Cannot use with a file name specification!
+
''Example 4:'' Determining if a drive or path exists. Cannot use with a file name specification.
 
{{CodeStart}} '' ''
 
{{CodeStart}} '' ''
 
{{Cl|LINE INPUT}} "Enter a drive or path (no file name): ", DirPath$
 
{{Cl|LINE INPUT}} "Enter a drive or path (no file name): ", DirPath$
{{Cl|IF}} PathExist%(DirPath$) {{Cl|THEN}} PRINT "Drive Path exists!" {{Cl|ELSE}} PRINT "Drive Path does not exist!"
+
{{Cl|IF}} PathExist%(DirPath$) {{Cl|THEN}} {{Cl|PRINT}} "Drive Path exists!" {{Cl|ELSE}} PRINT "Drive Path does not exist!"
 
{{Cl|END}}
 
{{Cl|END}}
  
Line 88: Line 93:
 
{{Cl|SHELL}} {{Cl|_HIDE}} CMD$ + "If Exist " + Path$ + "\nul echo yes > D0S-DATA.INF"
 
{{Cl|SHELL}} {{Cl|_HIDE}} CMD$ + "If Exist " + Path$ + "\nul echo yes > D0S-DATA.INF"
 
{{Cl|OPEN}} "D0S-DATA.INF" {{Cl|FOR (file statement)|FOR}} {{Cl|APPEND}} {{Cl|AS}} #1
 
{{Cl|OPEN}} "D0S-DATA.INF" {{Cl|FOR (file statement)|FOR}} {{Cl|APPEND}} {{Cl|AS}} #1
{{Cl|IF}} {{Cl|LOF}}(1) {{Cl|THEN}} PathExist% = -1             'yes will be in file if path exists
+
{{Cl|IF}} {{Cl|LOF}}(1) {{Cl|THEN}} PathExist% = -1       'yes will be in file if path exists
 
{{Cl|CLOSE}} #1
 
{{Cl|CLOSE}} #1
{{Cl|KILL}} "D0S-DATA.INF"               'delete data file optional
+
{{Cl|KILL}} "D0S-DATA.INF"                 'delete data file optional
 
{{Cl|END FUNCTION}} '' ''
 
{{Cl|END FUNCTION}} '' ''
 
{{CodeEnd}}
 
{{CodeEnd}}
 
{{small|Code by Ted Weissgerber}}
 
{{small|Code by Ted Weissgerber}}
:''Explanation: IF Exist'' checks for the drive path. ''\Nul'' allows an emply folder at end of path. ''Echo'' prints '''yes''' in the file if it exists.
+
:''Explanation: IF Exist'' checks for the drive path. ''\Nul'' allows an empty folder at end of path. ''Echo'' prints '''yes''' in the file if it exists.
 +
: In '''QB64''' you can simply use the [[_FILEEXISTS]] statement for the same purpose of the example above.
  
  
''Snippet 1:'' When looking for '''printers''' this command gives you a file list with the default printer '''True''':
+
''Snippet 1:'' When looking for '''printers''' this command gives you a file list with the default printer marked as '''TRUE''':
{{TextStart}}
+
{{CodeStart}}
{{Cb|SHELL}} {{Cb|_HIDE}} "CMD /C" + "wmic printer get name,default > default.txt"
+
{{Cl|SHELL}} {{Cl|_HIDE}} "CMD /C" + "wmic printer get name,default > default.txt"
{{TextEnd}}
+
{{CodeEnd}}
'''Created file's text:'''
+
: Created file's text:
{{TextStart}}Default  Name                           
+
{{OutputStart}}Default  Name                           
 
   FALSE    Microsoft XPS Document Writer  
 
   FALSE    Microsoft XPS Document Writer  
 
   TRUE    HP Photosmart C7200 series     
 
   TRUE    HP Photosmart C7200 series     
 
   FALSE    HP Officejet Pro 8600         
 
   FALSE    HP Officejet Pro 8600         
 
   FALSE    Fax
 
   FALSE    Fax
{{TextEnd}}
+
{{OutputEnd}}
: ''Explanation:'' [[LINE INPUT]] could be used to find he printer names as [[STRING]]s.
+
: ''Explanation:'' [[LINE INPUT]] could be used to find the printer names as [[STRING]] variables.
  
  
 
''Snippet 2:'' Here is the code to set the default printer to the "HP Officejet Pro 8600":
 
''Snippet 2:'' Here is the code to set the default printer to the "HP Officejet Pro 8600":
{{TextStart}} SHELL _HIDE "CMD /C" + "wmic printer where name='HP Officejet Pro 8600' call setdefaultprinter"
+
{{CodeStart}}
{{TextEnd}}
+
{{Cl|SHELL}} {{Cl|_HIDE}} "CMD /C" + "wmic printer where name='HP Officejet Pro 8600' call setdefaultprinter"
: After executing this program, and then running the first snippet again, we see the following '''contents of the text file:'''
+
{{CodeEnd}}
{{TextStart}}Default  Name  
+
: After executing this program, and then running the first snippet again, we see the following contents of the text file:
 +
{{OutputStart}}Default  Name  
 
   FALSE    Microsoft XPS Document Writer  
 
   FALSE    Microsoft XPS Document Writer  
 
   FALSE    HP Photosmart C7200 series     
 
   FALSE    HP Photosmart C7200 series     
 
   TRUE    HP Officejet Pro 8600         
 
   TRUE    HP Officejet Pro 8600         
 
   FALSE    Fax
 
   FALSE    Fax
{{TextEnd}}
+
{{OutputEnd}}
 
 
  
''See example:'' [[FILELIST$ (function)]] (member file search routine)
 
  
''See Library:'' File Exist C++ Function that does not create a temp file. [[Windows_Libraries#File_Exist|FileExist Library Function]]
+
===More examples===
 +
''See examples in:''
 +
* [[FILELIST$ (function)]] (member-contributed file search routine)
 +
* ''File Exist'' C++ Function that does not create a temp file: [[Windows_Libraries#File_Exist|FileExist Library Function]]
  
  
''See also:''
+
{{PageSeeAlso}}
 
* [[SHELL (function)]], [[_SHELLHIDE]]
 
* [[SHELL (function)]], [[_SHELLHIDE]]
 
* [[FILES]], [[CHDIR]], [[MKDIR]]
 
* [[FILES]], [[CHDIR]], [[MKDIR]]
Line 138: Line 146:
 
* [[$SCREENHIDE]], [[$SCREENSHOW]] {{text|(QB64 [[Metacommand]]s)}}
 
* [[$SCREENHIDE]], [[$SCREENSHOW]] {{text|(QB64 [[Metacommand]]s)}}
 
* [[_SCREENHIDE]], [[_SCREENSHOW]]
 
* [[_SCREENHIDE]], [[_SCREENSHOW]]
* [[FILELIST$]], [[DIR$]] {{text|(member file list array functions)}}
+
* [[FILELIST$]], [[PDS_(7.1)_Procedures#DIR.24|DIR$]] {{text|(member-contributed file list array function)}}
  
  
----
+
===Extra reference===
''References:''
 
* [[DOS]], [[Batch Files]], [[VB Script]]
 
* [[WGET]] {{text|(HTTP and FTP file transfer)}}
 
* [http://www.computerhope.com/msdos.htm MSDOS commands], [[DOS#DIR|DIR]]
 
* [http://www.pixelbeat.org/cmdline.html Linux Commands]
 
* [http://ss64.com/osx/ Mac OSX commands]
 
 
* [[Windows_Libraries#File_Dialog_Boxes|Windows Open and Save Dialog Boxes]]
 
* [[Windows_Libraries#File_Dialog_Boxes|Windows Open and Save Dialog Boxes]]
 
* [[C_Libraries#Console_Window|C Console Library]]
 
* [[C_Libraries#Console_Window|C Console Library]]

Latest revision as of 18:35, 15 February 2021

The SHELL statement allows a program to run external programs or command line statements in Windows, macOS and Linux.


Syntax

SHELL [DOSCommand$]
SHELL [_DONTWAIT] [_HIDE] [DOSCommand$]


Description

  • If the DOSCommand$ STRING parameter isn't used, the "command console" is opened and execution is halted until the user closes it manually.
  • If _DONTWAIT is used, the QB64 program doesn't wait for the SHELLed program/command to end.
  • When the _HIDE action is used, the console window is hidden and screen info can be "redirected" (using redirection characters like >) to a file (recommended).
  • Commands are external commands, according to the user's operating system, passed as strings enclosed in quotes or string variables.
  • Commands can be a mixture of strings and string variables added together using the + concatenation operator.
  • Command text can be in upper or lower case. Use single spacing between items and options.
  • QB64 automatically uses CMD /C when using SHELL, but it is allowed in a command string. Note: CMD alone may lock up program.
    • Note: Some commands may not work without adding CMD /C to the start of the command line.
  • QB64 program screens will not get distorted, minimized or freeze the program like QBasic fullscreen modes would.
  • QB64 can use long path folder names and file names and SHELL command lines can be longer than 124 characters.
  • In Windows, use additional CHR$(34) quotation marks around folder or file names that contain spaces.
  • For other operating systems, both the quotation mark character and the apostrophe can be used to enclose a file name that contains spaces.
  • NOTE: Use CHDIR instead of CD as SHELL commands cannot affect the current program path.


QBasic/QuickBASIC

  • QBasic BAS files could be run like compiled programs without returning to the IDE when SYSTEM was used to end them.
  • A user would invoke it with SHELL "QB.EXE /L /RUN program.BAS"


Examples

Example 1: When working with file or folder names with spaces, add quotation marks around the path and/or file name with CHR$(34).

SHELL _HIDE "dir " + CHR$(34) + "free cell.ico" + CHR$(34) + " /b > temp.dir" SHELL "start Notepad temp.dir" ' display temp file contents in Notepad window

Contents of temp.dir text file:

Free Cell.ico


Example 2: Opening a Windows program (Notepad) to read or print a Basic created text file.

INPUT "Enter a file name to read in Notepad: ", filename$ SHELL "CMD /C start /max notepad " + filename$ ' display in Notepad full screen in XP or NT 'SHELL "start /min notepad /p " + filename$ ' taskbar print using QB64 CMD /C not necessary

Explanation: Notepad is an easy program to open in Windows as no path is needed. Windows NT computers, including XP, use CMD /C where older versions of DOS don't require any command reference. The top command opens Notepad in a normal window for a user to view the file. They can use Notepad to print it. The second command places Notepad file in the taskbar and prints it automatically. The filename variable is added by the program using proper spacing.
  • Start is used to allow a Basic program to run without waiting for Notepad to be closed.
  • /min places the window into the taskbar. /max is fullscreen and no option is a normal window.
  • Notepad's /p option prints the file contents, even with USB printers.


Example 3: Function that returns the program's current working path.

currentpath$ = Path$ ' function call saves a path for later program use PRINT currentpath$ FUNCTION Path$ SHELL _HIDE "CD > D0S-DATA.INF" 'code to hide window in QB64 OPEN "D0S-DATA.INF" FOR APPEND AS #1 'this may create the file L% = LOF(1) 'verify that file and data exist CLOSE #1 IF L% THEN 'read file if it has data OPEN "DOS-DATA.INF" FOR INPUT AS #1 LINE INPUT #1, line$ 'read only line in file Path$ = line$ + "\" 'add slash to path so only a filename needs added later CLOSE #1 ELSE : Path = "" 'returns zero length string if path not found END IF KILL "D0S-DATA.INF" 'deleting the file is optional END FUNCTION

Code by Ted Weissgerber
Explanation: The SHELL "CD" statement requests the current working path. This info is normally printed to the screen, but the > pipe character sends the information to the DOS-DATA.INF file instead(QB64 can use _HIDE to not display the DOS window). The function uses the OPEN FOR APPEND mode to check for the file and the data(INPUT would create an error if file does not exist). The current path is listed on one line of the file. The file is opened and LINE INPUT returns one line of the file text. The function adds a "\" so that the Path$ returned can be used in another file statement by just adding a file name. Save the Path$ to another variable for later use when the program has moved to another directory.
In QB64 you can simply use the _CWD$ statement for the same purpose of the example above.


Example 4: Determining if a drive or path exists. Cannot use with a file name specification.

LINE INPUT "Enter a drive or path (no file name): ", DirPath$ IF PathExist%(DirPath$) THEN PRINT "Drive Path exists!" ELSE PRINT "Drive Path does not exist!" END FUNCTION PathExist% (Path$) PathExist% = 0 IF LEN(Path$) = 0 THEN EXIT FUNCTION 'no entry IF LEN(ENVIRON$("OS")) THEN CMD$ = "CMD /C " ELSE CMD$ = "COMMAND /C " SHELL _HIDE CMD$ + "If Exist " + Path$ + "\nul echo yes > D0S-DATA.INF" OPEN "D0S-DATA.INF" FOR APPEND AS #1 IF LOF(1) THEN PathExist% = -1 'yes will be in file if path exists CLOSE #1 KILL "D0S-DATA.INF" 'delete data file optional END FUNCTION

Code by Ted Weissgerber
Explanation: IF Exist checks for the drive path. \Nul allows an empty folder at end of path. Echo prints yes in the file if it exists.
In QB64 you can simply use the _FILEEXISTS statement for the same purpose of the example above.


Snippet 1: When looking for printers this command gives you a file list with the default printer marked as TRUE:

SHELL _HIDE "CMD /C" + "wmic printer get name,default > default.txt"

Created file's text:

Default Name FALSE Microsoft XPS Document Writer TRUE HP Photosmart C7200 series FALSE HP Officejet Pro 8600 FALSE Fax

Explanation: LINE INPUT could be used to find the printer names as STRING variables.


Snippet 2: Here is the code to set the default printer to the "HP Officejet Pro 8600":

SHELL _HIDE "CMD /C" + "wmic printer where name='HP Officejet Pro 8600' call setdefaultprinter"

After executing this program, and then running the first snippet again, we see the following contents of the text file:

Default Name FALSE Microsoft XPS Document Writer FALSE HP Photosmart C7200 series TRUE HP Officejet Pro 8600 FALSE Fax


More examples

See examples in:


See also


Extra reference



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page