8. Windows CMD

最後更新: 2019-05-17

目錄

 


multi-line cmd (^)

 

In the Windows Command Prompt the ^ is used to escape the next character on the command line.

copy file1.txt ^
 file2.txt

 


一行多 CMD

 

# 行完 dir 就行 echo

dir & echo foo

# second command to execute only if the first exited successfully

dir && echo foo

 


Temporary add exe to PATH

 

SET PATH=%PATH%;C:\cygwin\bin

 


Open command prompt in current folder

 

[Win10]

[A] Just go to your folder location and type "cmd" on the address bar(Alt + D / Ctrl + L)

[Win7]

In win7/8, try right click while holding shift key, should have cmd prompt here

win10 變了 powershell

 


Search File - dir

 

# /s - Displays files in specified directory and all subdirectories

cd C:\Windows

dir /s shutdown.exe

 


建立"空"檔

 

# NUL is a system reserved word,  大小寫沒分別(NUL/nul)

copy NUL 1.txt

type NUL > 2.txt

ping 127.0.0.1 > nul

 


findstr

 

它就是 Windows 版的 grep

Opts

  • /r       # regular expression
  • /s       # 在目前目錄并包含子目錄搜尋
  • /i       # 忽略大小寫差異
  • /v       #把不包含的那行內容輸出
  • /n       # 把符合條件的行數列出
  • /m      # 如果有找到符合的內容, 僅列出檔案名稱

i.e.

findstr /r "[^0-9]" t1.txt

netstat -a -p tcp -n | findstr 8443

 


find

 

i.e. Get the line count of a text file

# string 必須要 ""

#  /c         Displays only the count of lines containing the string.

find /c "string" myfile.txt

i.e. 數行數

# /V         Displays all lines NOT containing the specified string.

find /c /v ""

i.e 忽略大小寫

netstat -nao | find /i “estab”

 


日期時間

 

現在日期

date               # 顯示當前日期, 並問想設定的日期

date /t           # 只顯示, 不設定. 28/08/2024 Wed

現在時間

time /t                      # 只顯示, 不設定

@echo %time%        # Dynamic variable. 12:52:37.61

 


Process Management

 

tasklist

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0          8 K
System                           4 Services                   0      5,344 K
Registry                        88 Services                   0     26,244 K
smss.exe                       340 Services                   0        944 K
...

tskill / taskkill

tskill notepad

 * tasklist, taskkill 依賴 WMI Service

C:\>tasklist

ERROR: The specified module could not be found.

Fix

sc config winmgmt start= demand
net start winmgmt

tasklist

  1. # /F                         # 強制終止處理程序
  2. # /IM   imagename   # 指定要終止影像名稱, 可使用萬用字元 '*'

Permission

 

Opt

/D user       Deny specified user access.

i.e.

cacls C:\Windows\System32\shutdown.exe

C:\Windows\System32\shutdown.exe NT SERVICE\TrustedInstaller:F
                                 BUILTIN\Administrators:R
                                 NT AUTHORITY\SYSTEM:R
                                 BUILTIN\Users:R
                                 APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES:R
                                 APPLICATION PACKAGE AUTHORITY\ALL RESTRICTED APPLICATION PACKAGES:R

cacls filename /D username

 


Folder(mkdir / rmdir)

 

 

mkdir

建立 Folder

rmdir

刪除的Folder

The directory is not empty.

刪除未空的 Folder

/s      Removes all directories and files in the specified directory

/q      Quiet mode, do not ask if ok to remove a directory tree with /S

rmdir testFolder /S

testFolder, Are you sure (Y/N)? Y

 


Show Dsk Info

 

cmd

diskpart

list disk

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online          238 GB  2048 KB        *
  Disk 1    Online          465 GB  1024 KB   *
  Disk 2    Online          465 GB  1024 KB   *

 


tar

 

Win10 開始內置有它, 它是 bsd 版的 tar

Help: tar --help

Compress: -z, -j, -J, --lzma     # gzip/bzip2/xz/lzma

Usage Example:

cd c:\tmp\
dir /b *.bak > files.ls
FOR /F "tokens=*" %f IN (files.ls) DO tar -zvcf "%f".gz "%f"
FOR /F "tokens=*" %f IN (files.ls) DO del "%f"

 


Other

 

batch script

 

Creative Commons license icon Creative Commons license icon