Differences
This shows you the differences between two versions of the page.
Previous revision | |||
— | techwiki:dos [2024/01/11 12:58] (current) – [PowerShell] ying | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Windows CMD windows ====== | ||
+ | * make default window bigger: right click menu on title bar, Defaults > Layout; Screen Buffer size 300x100 | ||
+ | |||
+ | ====== basic operation ====== | ||
+ | |||
+ | determine the file itself <code dos> | ||
+ | |||
+ | @echo Full path and filename: %~f0 | ||
+ | @echo Drive: %~d0 | ||
+ | @echo Path: %~p0 | ||
+ | @echo Drive and path: %~dp0 | ||
+ | @echo Filename without extension: %~n0 | ||
+ | @echo Filename with extension: %~nx0 | ||
+ | @echo Extension: %~x0 | ||
+ | @echo Filename as given on command line: %0 | ||
+ | @echo Filename as given on command line minus quotes: %~0 | ||
+ | @REM Build from parts | ||
+ | @SETLOCAL | ||
+ | @SET drv=%~d0 | ||
+ | @SET pth=%~p0 | ||
+ | @SET fpath=%~dp0 | ||
+ | @SET fname=%~n0 | ||
+ | @SET ext=%~x0 | ||
+ | @echo Simply Constructed name: %fpath%%fname%%ext% | ||
+ | @echo Fully Constructed name: %drv%%pth%%fname%%ext% | ||
+ | @ENDLOCAL | ||
+ | pause | ||
+ | </ | ||
+ | |||
+ | **add other exe like python into path of cmd** | ||
+ | * note: put added path of python in front of %path% works, while behind it not work | ||
+ | * code <code dos> | ||
+ | SET PATH=D: | ||
+ | </ | ||
+ | |||
+ | **call python file with same name**< | ||
+ | @echo off | ||
+ | call python %~dp0%~n0.py | ||
+ | pause | ||
+ | </ | ||
+ | |||
+ | **call custom python to run python file with passing parameter (Basic version)**< | ||
+ | set CustPython=D: | ||
+ | %CustPython%python.exe %~dp0%~n0.py %1 | ||
+ | pause | ||
+ | </ | ||
+ | |||
+ | <code python SameNameAsPyFile.py> | ||
+ | import os,sys | ||
+ | input_count = len(sys.argv) | ||
+ | print(' | ||
+ | if input_count> | ||
+ | print(' | ||
+ | </ | ||
+ | |||
+ | **call custom python to run python file with passing parameter (Advanced version)**< | ||
+ | :: dont type out cmds in cmd window | ||
+ | @echo off | ||
+ | set CustPython=R: | ||
+ | |||
+ | :: Check for Python Installation | ||
+ | python --version 2>NUL | ||
+ | if errorlevel 1 goto NoPython | ||
+ | |||
+ | ::====== hasPython ====== | ||
+ | call python.exe %~dp0%~n0.py %1 && goto done | ||
+ | goto console | ||
+ | |||
+ | ::====== noPython ====== | ||
+ | :NoPython | ||
+ | IF EXIST %CustPython% ( | ||
+ | echo %CustPython% | ||
+ | call %CustPython%python.exe %~dp0%~n0.py %1 && goto done | ||
+ | goto console | ||
+ | ) ELSE ( | ||
+ | echo Error^: Python not detected | ||
+ | ) | ||
+ | |||
+ | :console | ||
+ | pause | ||
+ | :done | ||
+ | </ | ||
+ | |||
+ | <code python SameNameAsPyFileAdvanced.py> | ||
+ | import os,sys | ||
+ | input_count = len(sys.argv) | ||
+ | print(' | ||
+ | if input_count> | ||
+ | print(' | ||
+ | sys.exit(0) # 0: success, 1-127: bad error | ||
+ | else: | ||
+ | print(' | ||
+ | sys.exit(1) | ||
+ | </ | ||
+ | |||
+ | copy file and directory | ||
+ | <code dos> | ||
+ | copy d: | ||
+ | xcopy d:\download g:\dn /s /e | ||
+ | |||
+ | REM "copy without asking, replace by default | ||
+ | copy /y d: | ||
+ | </ | ||
+ | |||
+ | edit file and directory attribute | ||
+ | <code dos> | ||
+ | attrib +r +a +s +h secureFile.txt | ||
+ | </ | ||
+ | |||
+ | ===== String and Logic Operation ===== | ||
+ | |||
+ | * EQU, NEQ, substring, combine string <code dos> | ||
+ | @echo off | ||
+ | REM YourPythonFileName_w.bat will launch without console | ||
+ | REM YourPythonFileName.bat will launch with console | ||
+ | set file=%~n0 | ||
+ | if " | ||
+ | start pythonw %~dp0%file: | ||
+ | goto done | ||
+ | ) | ||
+ | call python %~dp0%~n0.py | ||
+ | pause | ||
+ | :done | ||
+ | </ | ||
+ | |||
+ | ===== cmd output direction ===== | ||
+ | |||
+ | * the > use as output direction | ||
+ | * 1>, and 2> means if the cmd has 2 output vales, and map each output to separate place | ||
+ | * stream 1 is the standard input/ | ||
+ | * 2> nul, means mute the 2nd output value. only catch 1st output result | ||
+ | |||
+ | |||
+ | ===== start a process ===== | ||
+ | |||
+ | * both call and start or direct app.exe can start a process, normally no difference | ||
+ | * but if you want to start another batch file, then | ||
+ | * CALL will start it in the same window and the called batch has access to the same variable context. | ||
+ | * START will create a new cmd.exe for the called batch and without /b it will open a new window. As it's a new context, variables can't be shared. | ||
+ | * Starting a new process with CALL, is very similar to running START /wait, in both cases the calling script will (usually) pause until the second script has completed. | ||
+ | |||
+ | |||
+ | **Outlook** | ||
+ | |||
+ | * create a email with content python version (the main code block) <code python> | ||
+ | # rough support code for reference | ||
+ | config[' | ||
+ | |||
+ | # main code | ||
+ | app = self.getOptionPath(config[' | ||
+ | address_list = [' | ||
+ | subject = 'Daily Feedback' | ||
+ | text_list = ['line 1', 'line 2'] | ||
+ | cmd = '" | ||
+ | if os.path.isfile(app): | ||
+ | print(cmd) | ||
+ | subprocess.Popen(cmd) | ||
+ | |||
+ | # support function for reference | ||
+ | def getOptionPath(self, | ||
+ | # updated: 2019.01.09 | ||
+ | if name not in dict.keys(): | ||
+ | print(' | ||
+ | return | ||
+ | option_list = [] | ||
+ | if not isinstance(dict[name], | ||
+ | option_list = [ dict[name] ] | ||
+ | else: | ||
+ | option_list = dict[name] | ||
+ | if all == 0: | ||
+ | found = None | ||
+ | for option in option_list: | ||
+ | if ' | ||
+ | # wildchar search | ||
+ | import glob | ||
+ | sub_option_list = glob.glob(option) | ||
+ | if len(sub_option_list) > 0: | ||
+ | sorted_option_list = sorted(sub_option_list) | ||
+ | print(' | ||
+ | found = sorted_option_list[-1] | ||
+ | break | ||
+ | else: | ||
+ | if os.path.exists(option): | ||
+ | found = option | ||
+ | break | ||
+ | if found is not None: | ||
+ | found = found.replace(' | ||
+ | print(' | ||
+ | return found | ||
+ | else: | ||
+ | all_option = [] | ||
+ | for option in option_list: | ||
+ | if ' | ||
+ | # wildchar search | ||
+ | import glob | ||
+ | sub_option_list = glob.glob(option) | ||
+ | all_option.extend(sorted(sub_option_list, | ||
+ | else: | ||
+ | if os.path.exists(option): | ||
+ | all_option.append(option) | ||
+ | standard_path_option = [] | ||
+ | for each_path in all_option: | ||
+ | standard_path = each_path.replace(' | ||
+ | if standard_path not in standard_path_option: | ||
+ | standard_path_option.append(standard_path) | ||
+ | print(standard_path_option) | ||
+ | return standard_path_option | ||
+ | </ | ||
+ | * outlook attach file (/c imp.note need or not need?) <code dos> | ||
+ | * ref: https:// | ||
+ | ===== system operation ===== | ||
+ | |||
+ | * assign a path with driver letter: Subst | ||
+ | * ref: https:// | ||
+ | |||
+ | * export out all system environment variable <code dos>set > filename.txt</ | ||
+ | * display all or a environment variable value <code dos>set | ||
+ | set path | ||
+ | echo %PATH% | ||
+ | reg query HKEY_CURRENT_USER\Environment /v path | ||
+ | reg query " | ||
+ | </ | ||
+ | |||
+ | * cust system var setup | ||
+ | * Method 1: in run > SystemPropertiesAdvanced : environment< | ||
+ | Computer → Properties → Advanced System Settings → Advanced (tab) → Environment Variables | ||
+ | |||
+ | // For user variable use | ||
+ | path = %PATH%; | ||
+ | path = D:/ | ||
+ | path = C:/ | ||
+ | |||
+ | // For system variable use | ||
+ | path = C:/ | ||
+ | </ | ||
+ | * Method 2: in command window <code dos> | ||
+ | REM set variable for current session environment (means not working after you close this cmd) | ||
+ | set z_sys=D:/ | ||
+ | |||
+ | REM set permanent variable for user environment | ||
+ | setx z_sys D:/z_sys | ||
+ | |||
+ | REM set permanent variable for system environment with admin rights | ||
+ | setx /M z_sys D:/z_sys | ||
+ | </ | ||
+ | * Method 3.1: into register editor by cmd < | ||
+ | REM set permanent variable for user environment | ||
+ | reg add HKEY_CURRENT_USER\Environment /v z_sys /d " | ||
+ | |||
+ | REM set permanent variable for system environment | ||
+ | reg add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment /v z_sys /d " | ||
+ | </ | ||
+ | * Method 3.2: into register editor by python with Extra Precision update instead of dump system environment value into user environment value <code python> | ||
+ | # because the path will give both user path and system path, | ||
+ | # here is more detail on update only user path content without having system path repeated in user path | ||
+ | import sys, subprocess | ||
+ | userPathValue=subprocess.check_output(" | ||
+ | userPath = userPathValue.split(' | ||
+ | myExtraPath = " | ||
+ | os.system(' | ||
+ | </ | ||
+ | * Method 4: insert register entry by reg file < | ||
+ | Windows Registry Editor Version 5.00 | ||
+ | |||
+ | [HKEY_CURRENT_USER\Environment] | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | * get cpu core < | ||
+ | wmic cpu get NumberOfCores | ||
+ | </ | ||
+ | |||
+ | ===== explorer operation ===== | ||
+ | |||
+ | * open explorer and select a item in path < | ||
+ | explorer /select, D: | ||
+ | </ | ||
+ | |||
+ | ===== file operation ===== | ||
+ | |||
+ | * force delete a file < | ||
+ | del /q /f " | ||
+ | </ | ||
+ | * rename file < | ||
+ | ====== CMD quick actions ====== | ||
+ | |||
+ | * clean run cmd list \\ < | ||
+ | reg delete " | ||
+ | </ | ||
+ | * cleanup recent file listing \\ < | ||
+ | del %appdata%\microsoft\windows\recent\automaticdestinations\* | ||
+ | del %appdata%\Microsoft\Windows\Recent\* | ||
+ | </ | ||
+ | |||
+ | ====== Batch file ====== | ||
+ | ===== Batch for registery edit ===== | ||
+ | * add \\ < | ||
+ | regedit | ||
+ | add: | ||
+ | Windows Registry Editor Version 5.00 | ||
+ | [HKEY_CURRENT_USER\SomeKey] | ||
+ | " | ||
+ | |||
+ | e.g | ||
+ | REG ADD HKCR\Control Panel\Desktop /v Wallpaper /t REG_SZ /d " | ||
+ | </ | ||
+ | * remove \\ < | ||
+ | [-HKEY_CURRENT_USER\SomeKey] | ||
+ | </ | ||
+ | * empty \\ < | ||
+ | [HKEY_CURRENT_USER\SomeKey] | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | ====== Windows specific command ====== | ||
+ | |||
+ | * list all the service <code dos>net start</ | ||
+ | * start telnet server <code dos>net start telnet</ | ||
+ | |||
+ | * list all port < | ||
+ | ===== Dos disk related operation ===== | ||
+ | <code dos> | ||
+ | REM " | ||
+ | format /FS:FAT32 /Q X: | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== control panels and admin tools ===== | ||
+ | |||
+ | * windows component window can be open with cmds like explorer.exe, | ||
+ | * code (canonical name, GUID, module dll)< | ||
+ | explorer.exe shell: | ||
+ | explorer.exe shell::: | ||
+ | control.exe netconnections | ||
+ | ncpa.cpl | ||
+ | control ncpa.cpl | ||
+ | rundll32.exe shell32.dll, | ||
+ | |||
+ | control.exe sysdm.cpl,, | ||
+ | control /name Microsoft.NetworkAndSharingCenter /page Advanced | ||
+ | control /name Microsoft.InternetOptions /page 4 | ||
+ | explorer shell::: | ||
+ | |||
+ | # folder option | ||
+ | control /name Microsoft.FolderOptions | ||
+ | control folders | ||
+ | rundll32.exe shell32.dll, | ||
+ | # folder option - view | ||
+ | rundll32.exe shell32.dll, | ||
+ | </ | ||
+ | * ref: | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | ^ management panel ^^ | ||
+ | | services.msc | services panel | | ||
+ | | compmgmt.msc | computer manager | | ||
+ | | devmgmt.msc | device manager | | ||
+ | | fonts | fonts folder | | ||
+ | | control | control panel | | ||
+ | | control userpasswords2 | user manager | | ||
+ | | control date/time | date and time | | ||
+ | | control desktop | display property | | ||
+ | | control color | appearance | ||
+ | |||
+ | ^ control panel ^ control somefile.cpl,< | ||
+ | |access.cpl |Accessibility controls Keyboard(1), | ||
+ | |appwiz.cpl |Add/ | ||
+ | |desk.cpl |Display properties Themes(5), Desktop(0), Screen Saver(1), Appearance (2), Settings(3)| | ||
+ | |hdwwiz.cpl |Add hardware | | ||
+ | |inetcpl.cpl |Configure Internet Explorer and Internet properties General(0), | ||
+ | |intl.cpl |Regional settings Regional Options(1), Languages(2), | ||
+ | |joy.cpl |Game controllers | | ||
+ | |main.cpl |Mouse properties and settings Buttons(0), | ||
+ | |main.cpl, | ||
+ | |mmsys.cpl |Sounds and Audio Volume(0), Sounds(1), Audio(2), Voice(3), Hardware(4)| | ||
+ | |ncpa.cpl |Network properties | | ||
+ | |nusrmgr.cpl |User accounts | | ||
+ | |powercfg.cpl |Power configuration Power Schemes, Advanced, Hibernate, UPS (Tabs not indexed)| | ||
+ | |sysdm.cpl |System properties General(0), | ||
+ | |telephon.cpl |Phone and modem options Dialing Rules(0), Modems(1), Advanced(2) | | ||
+ | |timedate.cpl |Date and time properties Date & Time(0), Time Zone(1), Internet Time (no index)| | ||
+ | |||
+ | |||
+ | ===== Windows 3rd party command-line utility ===== | ||
+ | |||
+ | ** NirCmd ** | ||
+ | |||
+ | ^ download | http:// | ||
+ | ^ set default sound device | nircmd.exe setdefaultsounddevice “Speakers_TheNameInPanel” | | ||
+ | ^ close folder windows | nircmd.exe win close class " | ||
+ | ^ close empty cmd windows | nircmd.exe win close title " | ||
+ | ^ off monitor | nircmd.exe monitor off | | ||
+ | ^ off computer | nircmd.exe exitwin poweroff force | | ||
+ | ^ pop dialog | nircmd.exe trayballoon " | ||
+ | ^ windows control | http:// | ||
+ | |||
+ | ** MS sysinternal suite ** | ||
+ | |||
+ | * http:// | ||
+ | |||
+ | |||
+ | ===== PowerShell ===== | ||
+ | |||
+ | * enable script run on powershell | ||
+ | * run powershell as admin, then < | ||
+ | |||
+ | * add ssh < | ||
+ | # Install the OpenSSH Client | ||
+ | Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0 | ||
+ | |||
+ | # Install the OpenSSH Server | ||
+ | Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 | ||
+ | </ | ||
+ | * after install ssh, go Run services, and set ssh server to startup automatic, then start ssh server | ||
+ | * you need put vim on server machine to be able to edit text file: | ||
+ | * https:// |