2020年6月22日 星期一

利用PING,ARP快速清查區網IP

[CMD]利用PING,ARP快速清查區網IP

之前寫VBS快速清查IP, 不過真正要用時才發現程式放在另外地方。在不想安裝IPSCAN的情況下,用指令是最簡單的方式。由於現在很多機器的防火牆都不給PING了,所以換個方法就搭配PING 和ARP,剛好可以查出對應卡號。
叫出命令視窗後,依照以下順序輸入..
Step1.
 arp -d
Step2.
for /L %i in (1,1,254) do ping 192.168.128.%i -n 1 -w 200 > NUL
Step3.
arp -a |find "192.168" | find "dynamic"
步驟一的目的是清除記憶體內的網卡紀錄
步驟二則是利用迴圈測試192.168.128附近的連線
步驟三則是將找到的網卡號列出,如果有列出就至少是有在使用。
操作畫面如下:

參考資料:
  1. 利用VBS清查IP使用狀況 @ Haoming-跟著滑鼠去旅行 (挨踢日記) :: 隨意窩 Xuite日誌
  2. [CMD]Ping - 毛窩- 點部落
  3. Ping Command Details and Examples

資料來源

2020年6月19日 星期五

ml5js 圖片辨識

Richard Demohttps://thiocyanic-refurbis.000webhostapp.com/index.html

Hello ml5

Hello there! If you've landed here, that probably means you're interested in building your first ml5.js project. If so, wonderful! We invite you to read on.

ml5.js is being developed to make machine learning more accessible to a wider audience. Along with supporting education and critical engagement with machine learning, the ml5 team is working actively to wrap exciting machine learning functionality in a friendlier and easier-to-use way. The following example introduces you ml5.js through a classic application of machine learning: image classification.

This example showcases how you can use a pre-trained model called MobileNet -- a machine learning model trained to recognize the content of certain images -- in ml5.js. The example aims to highlight a general pattern for how ml5.js projects are setup.

ml5.js is growing every day, so be sure to see some of the other applications of ml5 in the reference section and their accompanying examples for the latest offerings.

Setup

If you've arrived here, we assume you've checked out our quickstart page to get a simple ml5.js project set up. To get this to run, you'll need:
  • 📝 A text editor (e.g. AtomVSCodeSublimetext)
  • 💻 Your web browser: Chrome & Firefox preferred
  • 🖼 An image to run your classification on
Your project directory should look something like this:
|_ /hello-ml5
  |_ 📂/images
    |_ 🖼 bird.png
  |_ 🗒index.html
  |_ 🗒sketch.js
Where:
  • 📂/hello-ml5: is the root project folder
    •   📂/images: is a folder that contains your image
    •       🖼 bird.png: is a .png image of a bird (it can also be something else!)
    •   🗒index.html: is an .html file that has your html markup and library references
    •   🗒sketch.js: is where you'll be writing your javascript

Demo

This example is built with p5.js. You can also find the same example without p5.js here.

Code

Your index.html

Here you can see that we read in the javascript libraries. This includes our ml5.js version as well as p5.js. You can copy and paste this into your index.html file or for good practice you can type it all out. Make sure to save the file and refresh your browser after saving.
<html>

<head>
  <meta charset="UTF-8">
  <title>Image classification using MobileNet and p5.js</title>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/addons/p5.dom.min.js"></script>
  <script src="https://unpkg.com/ml5@0.3.1/dist/ml5.min.js"></script>
</head>

<body>
  <h1>Image classification using MobileNet and p5.js</h1>
  <script src="sketch.js"></script>
</body>

</html>

Your sketch.js

Inside your sketch.js file you can type out (or copy and paste) the following code. Notice in this example we have a reference to "images/bird.png". You'll replace this with the name of your image.
// Initialize the Image Classifier method with MobileNet. A callback needs to be passed.
let classifier;

// A variable to hold the image we want to classify
let img;

function preload() {
  classifier = ml5.imageClassifier('MobileNet');
  img = loadImage('images/bird.png');
}

function setup() {
  createCanvas(400, 400);
  classifier.classify(img, gotResult);
  image(img, 0, 0);
}

// A function to run when we get any errors and the results
function gotResult(error, results) {
  // Display error in the console
  if (error) {
    console.error(error);
  } else {
    // The results are in an array ordered by confidence.
    console.log(results);
    createDiv('Label: ' + results[0].label);
    createDiv('Confidence: ' + nf(results[0].confidence, 0, 2));
  }
}

Our sketch.js explained in 4 steps

Step 1: Define your variables

Here we define our variables that we will assign our classifier and image to.
// Initialize the Image Classifier method with MobileNet. A callback needs to be passed.
let classifier;

// A variable to hold the image we want to classify
let img;

Step 2: Load your imageClassifier and image

Use p5's preload() function to load our imageClassifier model and our bird image before running the rest of our code. Since machine learning models can be large, it can take time to load. We use preload() in this case to make sure our imageClassifier and image are ready to go before we can apply the image classification in the next step.
function preload() {
  classifier = ml5.imageClassifier('MobileNet');
  img = loadImage('images/bird.png');
}

Step 3: Setup, classify, and display

In p5.js we use the setup() function for everything in our program that just runs once. In our program, we use the setup() function to:
  1. create a canvas to render our image
  2. call .classify() on our classifier to classify our image
  3. render the image to the canvas
You will notice that the .classify() function takes two parameters: 1. the image you want to classify, and 2. a callback function called gotResult. Let's look at what gotResult does.
function setup() {
  createCanvas(400, 400);
  classifier.classify(img, gotResult);
  image(img, 0, 0);
}

Step 4: Define the gotResult() callback function

The gotResult() function takes two parameters: 1. error, and 2. results. These get passed along to gotResult() when the .classify() function finishes classifying the image. If there is an error, then an error will be logged. If our classifier manages to recognize the content of the image, then a result will be returned.

In the case of our program, we create a div that displays the label and the confidence of the content of the image that has been classified. The nf() function is a p5 function that formats our number to a nicer string.
// A function to run when we get any errors and the results
function gotResult(error, results) {
  // Display error in the console
  if (error) {
    console.error(error);
  } else {
    // The results are in an array ordered by confidence.
    console.log(results);
    createDiv('Label: ' + results[0].label);
    createDiv('Confidence: ' + nf(results[0].confidence, 0, 2));
  }
}

And voila!

You've just made a simple machine learning powered program that:
  1. takes an image,
  2. classifies the content of that image, and
  3. displays the results all in your web browser!
Not all of our examples are structured exactly like this, but this provides a taste into how ml5.js is trying to make machine learning more approachable. Try using different images and seeing what kinds of things get returned.

Some guiding questions you might start to think about are:
  1. When classifying an image with MobileNet, does the computer see people? If not, why do you think that is?
  2. Do you notice that MobileNet is better at classifying some animals over others? Why do you think that is?

Source

2020年6月18日 星期四

字符串查找增強 findstr

在文件中尋找字符串。
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/F:file ]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ ...]]
  /B在一行的開始配對模式。
  /E在一行的結尾配對模式。
  /L按字使用搜索字符串。
  /R將搜索字符串作為一般表達式使用。
  /S在當前目錄和所有子目錄中搜索
              匹配文件。
  /I指定搜索不分大小寫。
  /X打印完全匹配的行。
  /V只打印不包含匹配的行。
  /N在匹配的每行前打印行數。
  /M如果文件含有匹配項,只打印其文件名。
  /O在每個匹配行前打印字符偏移量。
  /P忽略有不可打印字符的文件。
  /OFF[LINE]不跳過帶有脫機屬性集的文件。
  /A:attr指定有十六進位數字的顏色屬性。請見"color /?"
  /F:file從指定文件讀文件列表(/代表控制台)。
  /C:string使用指定字符串作為文字搜索字符串。
  /G:file從指定的文件獲得搜索字符串。(/代表控制台)。
  /D:dir查找以分號為分隔符的目錄列表
  strings要查找的文字。
  [drive:][path]filename指定要查找的文件。

除非參數有/C前綴,請使用空格隔開搜索字符串。
例如: 'FINDSTR "hello there" xy'在文件xy中尋找"hello"或
"there" 。'FINDSTR /C:"hello there" xy'在文件xy尋找"hello there"。
-------------------------------------------------- -------------------------------------------------- --

1.基本格式:findstr " strings " [drive:][path]filename

Strings是要查找的內容。
[rive:][path]filename指定要查找的文件,路徑可缺省,缺省情況下為當前目錄。

例1
findstr "icq" 123.txt
在123.txt中查找包含有“icq”這三個字符串的行。
-------------------------------------------------- -------------------------------------------------- --

/I 指定搜索不分大小寫。

例2
findstr /i "MSN" 123.txt
在123.txt中查找包含有“MSN”這三個字符的行,且不區分大小寫。
-------------------------------------------------- -------------------------------------------------- --

★/R將搜索字符串作為正則表達式使用。參數/R強調以正則表達式規則來解讀字符串。R - Right右即為正。我們
都說右手是正手,因此引申為右為正,左為反。

例3
findstr /r "icq msn" 123.txt
在123.txt中查找包含有“icq”或“msn”的行,查找的多個字符串間用空格隔格開。
-------------------------------------------------- -------------------------------------------------- --

 /S 在當前目錄和所有子目錄中搜索。

例4
findstr /s /i "MSN" *.txt
在當前目錄和所有子目錄中的txt文件中搜索字符串"MSN"(不區分字母大小寫)。
-------------------------------------------------- -------------------------------------------------- --

/C:string 使用指定字符串作為文字搜索字符串。

例5
findstr /c:"icq msn" 123.txt
在123.txt中查找包含有“icq msn”這幾個字符的行。注意,這裡“icq msn”是一整體的。
此參數多用於查找含有空格的字符串。
-------------------------------------------------- -------------------------------------------------- --

★在使用findstr "我你他" test.txt的時候,並不能查找到內容,但是,加上開關/i或者/r之後就正確無誤
了,可能是在查找多個純中文字符串的時候的一個bug吧;單個的純中文字符串沒有任何問題。
-------------------------------------------------- -------------------------------------------------- --


2.findstr 命令中正則表達式的用法規則

一般表達式的快速參考:
  .通配符:任何字符
  *重複:以前字符或類別出現零或零以上次數
  ^行位置:行的開始
  $行位置:行的終點
  [class]字符類別:任何在字符集中的字符
  [^class]補字符類別:任何不在字符集中的字符
  [xy]範圍:在指定範圍內的任何字符
  \x Escape:元字符x的文字用法
  \<xyz字位置:字的開始
  xyz\>字位置:字的結束
--------------------------------------------- -------------------------------------------------- -------

●通配符和重複符規則,即.和*
通配符,即一個句點,代表任何一個字符,而且只能是一個,包括字母、數字、半角符號還有空格。
重複符,即型號*代表前面字母的出現次數(出現次數從0到多次,0表示沒有)。

findstr . 123.txt或findstr "." 123.txt
在文件123.txt中查找任意字符,不包括空行。

例6
findstr .* 2.txt或findstr ".*" 2.txt
在文件123.txt中查找任意字符,包括空行。

例7
findstr ac* 123.txt
在文件123.txt中查找出現一個“a”字符串,以及a後面出現過0次或者任意次c的字符行。
如:
a
ac
acc
addc
等都匹配。

例8
findstr ak5* 123.txt
在文件123.txt中查找出現一個“ak”字符串,以及ak後面出現過0次或者任意次5的字符行。如:
ak
ak5
akbbb
ak125
ak555
等都匹配。
-------------------------------------------------- -------------------------------------------------- --

●行首、行尾符規則,即^ 和 $

例9
findstr "^step" 123.txt
在文件123.txt中查找行首為step字符串的行。
如:
stepkdka
step 456
這兩行都匹配的。

例10
findstr "step$" 123.txt
在文件123.txt中查找行尾為step字符串的行。
如:
123 dstep
123step
這兩行也匹配的。

例11
findstr "^step$" 123.txt
在文件123.txt中查找行首為step,且行尾也為step的行,即step獨自一行。
-------------------------------------------------- -------------------------------------------------- --

●字符集規則,即[class]
①表示含有集裡的任意一個字符的即匹配。
②該字符集裡的元素可以是字母和數字和一般的半角字符,如:}{ ,.][等,但雙引號"不被識別。不能是漢字,漢字
不被正確解釋(漢字不是ASCII碼)。
如果在字符集內插入通配符和重複符號,即"[.*]"將會把.和*視為普通字符,沒有通配和重複的含義。

例12
findstr "[0-9]" 123.txt
在文件123.txt中查找數字0-9的任意之一的行。
如:
4kkb
1 lkka cc
這兩行都匹配。

例13
findstr "[a-zA-Z]" 123.txt
在文件123.txt中查找包括任意字母行。

例14
findstr "[abcezy]" 2.txt
在文件123.txt中查找包括abcezy其中任意一字母的行。

例15
findstr "[a-fl-z]" 2.txt
在文件123.txt中查找小寫字符a到f或l到z的任意一字母的行,但不包含gh I jk這幾個字母。

例16
findstr "M[abc][123]Y" 2.txt
在文件123.txt中查找可以匹配Ma1Y , Mb1Y, Mc1Y; Ma2Y , Mb2Y, Mc2Y; Ma3Y , Mb3Y, Mc3Y的行。
-------------------------------------------------- -------------------------------------------------- --

● 減法規則,即[^class]

例17
findstr "[^0-9]" 123.txt
如果是純數字的行便過濾掉,例如2323423423這樣的字符串被過濾,345hh888這樣的形式則過濾不了。
注意,純數字的行不能有空格,不論行首行尾或者是行中都不能有空格,否則過濾失敗!

例18
findstr "[^az]" 123.txt
如果是純字母的行便過濾掉,例如sdlfjlkjlksjdklfjlskdf這樣的字符將被過濾,如果是sdfksjdkf99999這樣的形
式則過濾不了。
注意,純字母的行不能有空格,不論行首行尾或者是行中都不能有空格,否則過濾失敗!

例19
findstr "[^add]" 123.txt
過濾僅含有由add三個字母組成的純字母字符串的行。
如:
a
ad
ddaadd
dd
這些行都會被過濾。
注意,僅含有由add三個字母組成的純字母字符串的行不能有空格,不論行首行尾或者是行中都不能有空格,否則
過濾失敗!

例20
findstr "[^echo]" 123.txt
過濾僅含有由e c h o四個字母組成的純字母字符串的行。
如:
e
c
ec
cho
chooo
這些行都會被過濾。
-------------------------------------------------- -------------------------------------------------- --

●單詞前綴後綴定位規則,即\<xyz和xyz\>

該xyz可以是英文單詞或數字,但不適用於漢字。符號\理解為轉義符,化解小於號和大於號的重定向命令含義。
該規則是匹配類似單個英文單詞的。

例21
findstr "\<echo" 123.txt
所有含有以echo為前綴的字符串的行,都匹配。
如:
echo:kkk a add
jjkk echo
這兩行都匹配。
(思考:為什麼:echo也匹配?)

例22
findstr "echo\>" 123.txt
所有含有以echo為後綴的字符串的行,都匹配。
如:
qq bbecho這一行也匹配。
(思考:為什麼kkkk echo:也匹配?)

例23
findstr "\<end\>123.txt
這裡是用來精確查找單詞。查找單詞end的行,
注意:
ended
cdkend
bcd-end-jjkk
這類詞都不匹配。
(思考:為什麼end echo和end也匹配?因為\<xyz\>格式要查找的是單個英文單詞。)
------------------------------- -------------------------------------------------- ---------------------

●轉義符\
把表達式中的特殊字符(元字符)轉化為普通字符。常見寫法:
\.
\*
\\
??
\-

例24
findstr "\.abc" 123.txt
在文件123.txt中查找可以匹配“.abc”字符串的行,這裡\。是把。給轉義了。

例25
findstr "1\\" 123.txt或findstr "1\\\\" 123.txt
在文件123.txt中查找可以匹配“1\”字符串的行,這裡\\是把\給轉義了。

★要查找的字符串含有\時,可以用\\把\給轉義;或者把\變成\\\\。如果目標字符串的\後面還有內容,則搜索字符
串\除了要變成\\(本身的轉義要求),還可以在它後面再加一個字符,如\\.
————————————————
版权声明:本文为CSDN博主「郁闷阳光」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/playboy1/java/article/details/6869384

DOS Batch File 常用指令


DOS指令 -- FOR迴圈

DOS指令  --  FOR迴圈
因為最近有在寫Dos的指令,有感而發寫一些自己有在用的東西
先解釋基本的For指令
再說明遞增/遞減的 For迴圈和搜索檔案用的迴圈
最後就是筆者最常用的For迴圈
For指令常常用在重複執行類似的動作
如 :
在字串最後面加上數字
像是 :
朋友1
朋友2
朋友3
執行指令並依序處理傳回的文字
執行 dir 並比對回傳的檔案
開啟文字文件依序讀取每一行的文字,並依不同狀況執行
後兩種是我最常使用的功能,先介紹基本的 For 指令的使用方式
FOR %%a IN (香蕉,你的,巴拉) DO (
  echo %%a
)
輸出會像:
(  ) 內的東西都用光以後,迴圈就會自動結束。
而每次取得的值,這個值會存在變數 %%a 裡面,並可以在 DO 後面的 ( ) 內執行指令
說明一下每個指令的意思
FOR -- 要開始一個For迴圈最前面一定需要的文字 For
%%a -- 用來儲存For迴圈產出的值,這個變數可以由使用者自行決定,可以是 %%b 或 %%g。
             前面一定要有兩個%%後面加上一個英文字。注意 : 這個英文字有大小寫之分
             在這個例子中用來儲存 香蕉.....
IN -- 指令文字,後面一定要接  (香蕉...) <- (裡面可以放很多東西)
(香蕉...) -- 文字串,在For裡面依序使用的東西 (後面會說到裡面還可以放檔案、指令)
DO -- 指令文字, 後面一定要接 (echo  ....)
(echo  ....) -- 要執行的指令,在這裡可以使用For的變數 %%a。
                     如果指令有兩個以上可以像例子中一樣用 ( ) 包起來。
以上就是最基本的For,接著說明數字的遞增和遞減
數字的遞增和遞減
For /L %%a in (start,step,stop) DO ( Do something)
例子
For /L %%a in (1,2,7) DO (
  echo %%a
)
輸出 :
/L -- For後面可以下不同的參數,這會改變For對 IN 後面的 ( ) 所做的動作。
        /L 會讓For 認定 ( ) 內的是數字,並且是遞增或遞減
start -- 起始數字,For迴圈從這個數字開始
step -- 每次迴圈變化的量,1 就是一次加一,-1就是一次減一。
           例子中是每次加二
stop -- 當數字超過stop所設定的數字的時候迴圈就會終止。
           (超過可能是比它大或比它小,端看start的數字與stop的數字的比較)
           當數字等於stop的時候,並不會停止迴圈。
搜索檔案用的迴圈
For /R [[drive:]path] %%a in (檔名,檔名) do (Do something)
例子
For /R c:\users\myaccount\Desktop %%a in (生日*,會議*) do (
echo %%a
)
輸出:
c:\users\myaccount\Desktop\生日壽星.txt
c:\users\myaccount\Desktop\生日蛋糕.doc
c:\users\myaccount\Desktop\會議.doc
/R -- 設定為搜索檔案的For迴圈
drive: -- 搜索的磁碟機位置,如C:\或D:\等
              可以省略不輸入,當沒有輸入時會認定為執行目錄的磁碟機,一般是 C:\
path -- 開始搜索的資料夾,重這個資料夾開始往子資料夾搜索
            可以省略不輸入,當drive和path都沒有輸入時,會使用執行目錄為預設路徑
(檔名,檔名...) -- 輸入要尋找的檔名,其中檔名要加上 * 號
                           * 號代表萬用字元,例子中,生日*,"生日"後面不管接什麼名字都算符合
                           也可以放在前面,*生日.doc,也就變成檔名最後是生日的都算符合
最後是筆者最常使用的For迴圈
指令或讀取檔案的For迴圈
For /F "option" %%a in (檔案,檔案...) do (Do something)
For /F "option" %%a in ("字串,字串") do (Do something)
For /F "option" %%a in ('指令') do (Do something)
稍微說明一下,in後面 ( )中 :
不加任何符號 (除了 , )代表這是一個檔案,可以被開啟讀取
前後加上 " " ,表示裡面是字串
前後加上 ' ' ,表示裡面是個指令,For會執行這個指令並收集回傳值
如果檔案的檔名有空白會造成For迴圈的判讀錯誤,因為Dos的For 會把空白當作分行,空白前後是不同檔案,這個時候option要加上 "usebackq",此時For迴圈的判讀為
For /F "usebackq" %%a in ("檔案,檔案...") do (Do something)
For /F "usebackq" %%a in ('字串,字串') do (Do something)
For /F "usebackq" %%a in (`指令`) do (Do something)
 ( )內用 " ",代表檔案,這樣可以把有空白檔名的檔案包在裡面
' ', ' = " + shift, "符號再按shift就是這個符號,這個符號會被判定為字串
` `, 最左上角的按鈕 ( 1 的左邊),這個符號會被判定為指令
例子:
For /F  "tokens=4,5" %%A in ('dir') do (
echo 檔名:%%B 檔案大小:%%A
)
輸出:
檔名:.. 檔案大小:<DIR>
檔名:.. 檔案大小:<DIR>
檔名:project.doc 檔案大小:4414
檔名:readme.txt 檔案大小:12
檔名:runforlife.mov 檔案大小:65434567
其中option有很多選項,整個option要用 " " 包起來,如 "tokens=1,2 eol=+ skip=2"
eol -- 當碰到設定的符號時,就不會再儲存後面的文字 (只能指定一個字元)
skip -- 略過開頭的 n 行,設定幾行就會跳過幾行
delims -- 設定分隔符號,預設的分隔符號為 空白 ,可以指定多個符號
               如"delims=+=:a ",指定的符號有 +號 =號 :號 a英文字和一個空白
tokens -- 當回傳的文字被分隔符號 (delims)切隔成多個時,就要用tokens來取得想要的文字
               預設是 1 ,可以用"tokens=1,3,5-9,*",可以一次指定一個或用 - 符號指定多個
               也可以用 * 符號把從這個以後的文字都設定在同一個變數中。
               (tokens的使用會在下面介紹)
usebackq 改變 /F 讀取的格式            
這邊說明一下tokens
tokens的使用會引用額外的變數,如 :
For /F "tokens=1,2,3 %%d in (aaa bbb ccc ddd) do (
echo %%d
echo %%e
echo %%f
echo %%g
)
會顯示
aaa
bbb
ccc
%g
一旦指定tokens以後,For迴圈會自動增加變數,並依照英文符號第增
如果指定的變數是大寫,如%%C,自動增加的變數也會是大寫%%D、%%E...
最後一個會顯示 %g 是因為沒有指定token,所以第四個變數沒有文字可以儲存
如果tokens使用 m-n的方式,如:
For /F "tokens=1,2-3,4 %%D in (aaa bbb ccc ddd) do (
echo %%D
echo %%E
echo %%F
echo %%G
)
會顯示
aaa
bbb ccc
ddd
%G
最後一種tokens的用法是tokens=1,*,星號代表從這個 (在這邊是1) 以後的文字都存在同一個變數,如 :
For /F "tokens=1,2,* %%D in (aaa bbb ccc ddd) do (
echo %%D
echo %%E
echo %%F
echo %%G
)
會顯示
aaa
bbb
ccc ddd
%G
這裡做簡單的小結
For 後面代不同的參數會使For有不同的運作和判斷
For ------ 最基本的For迴圈,筆者還沒用過
For /L -- 讓數字遞增或遞減的For迴圈,像是讓一個指令重複執行10次
For /R -- 搜索檔案,可以搜索特定檔案或全部搜索,並回傳各種檔案的屬性
For /F -- 讀取檔案或執行指令,並儲存回傳的值 (常用),並且有很多個option可用
For /D -- 使用延伸指令,重來沒用過
底下介紹筆者在用的使用方法
讀取電腦的IP
各位應該有很多種抓取電腦IP的方法,這邊用For迴圈加ipconfig指令的方式
@setlocal
@echo off
for /F "tokens=16" %%a in ('ipconfig ^| findstr /R /C:"10.1.1" ^| findstr /R /C:"IPv4"') do (
 set ip=%%a
)
echo %ip%
endlocal
輸出:
10.1.1.20
更進一步的,只要10.1.1.20裡面的最後一個數字 20 ,可以在迴圈裡面再新增一個迴圈
@setlocal
@echo off
for /F "tokens=16" %%a in ('ipconfig ^| findstr /R /C:"10.1.1" ^| findstr /R /C:"IPv4"') do (
 for /F "tokens=4 delims=." %%c in ("%%a") do (set ip=%%c)
)
echo %ip%
endlocal
輸出:
20
說明一下這邊用到的參數和指令
tokens=16 -- 可能有人會問,為什麼知道是第16個位置,那是因為我用試的試出來的
                      方法有點像 : tokens=1,2,3,4,5,6,7,8,9,10,11,12,然後用echo %%a-1, %%b-2,....
                      這樣的方法找出位置
delims=. ----  因為IP之間是用 . 隔開,因此設定間隔符號為 . 。
ipconfig -- 顯示電腦IP和其它相關資訊
findstr ---- 在文件中尋找文字,在這邊用來尋找 ipconfig 執行的結果
^| --------- 這裡有兩個符號 ^ 和 | 。
                 ^ 在批次檔內代表逃脫符號,會讓下一個符號的特殊能力失效,變成普通的文字。
                 | 這個是管線符號 (pipe) 會把左邊執行的結果傳到右邊當作輸入
                 會用到 ^| ,是因為For裡面會認定 | 為特殊符號,會中斷For的執行
這邊用到兩個迴圈,For裡面還有另一個For,這可以降低撰寫的行數
兩個For所使用的變數建議用不一樣的,比較好區分
For裡面設定(set)變數
這邊用漸進的方式解釋變數在For迴圈理的現象
這邊使用For /R 去累計總檔案的大小,例子中的檔案大小是編出來的
@setlocal
echo off
set size=0
For /R  %%A in (Joo*) do (
echo %%~zA
)
pause
endlocal
輸出:
10
20
30
>40
現在加上一個變數來累加總共的檔案大小
@setlocal
echo off
set size=0
For /R  %%A in (Joo*) do (
set /a size=%size% + %%~zA
echo 這個檔案大小 : %%a
echo 目前累計 : %size%
)
echo 總共累計 : %size%
pause
endlocal
輸出:
這個檔案大小 : 10
目前累計 : 0
這個檔案大小 : 20
目前累計 : 0
這個檔案大小 : 30
目前累計 : 0
這個檔案大小 : 40
目前累計 : 0
總共累計 : 40
這個例子多了set /a size=%size% + %%~zA用來累加每個檔案的大小
並在下一行使用echo %size%告知現在size的累計量
從結果看,在迴圈內的 size 變數沒有被加總的感覺
最後一個檔案大小是 40 ,跟"總共累計"大小一樣,也就是 size變數只有加到最後一次
因為在 For 裡面的 % % 變數,是取用For之前的變數做的位置
因此每次執行set /a size=%size% + %%~zA這個裡面的%size%,是取用最前面set size=0 的這個位置
也就是在For裡面呼叫%size%時,每次呼叫的值都是 0。
底下說明如何在For迴圈裡面使用 set 變數。
新增參數 enableextensions enabledelayedexpansion
@setlocal enableextensions enabledelayedexpansion
echo off
set size=0
For /R  %%A in (Joo*) do (
set /a size=!size! + %%~zA
echo 這個檔案大小 : %%a
echo 目前累計 : !size!
)
echo 總共累計 : %size%
pause
endlocal
輸出:
這個檔案大小 : 10
目前累計 : 10
這個檔案大小 : 20
目前累計 : 30
這個檔案大小 : 30
目前累計 : 60
這個檔案大小 : 40
目前累計 : 100
總共累計 : 100
在 setlocal 後面再加上參數enableextensions enabledelayedexpansion,可以使用 ! ! 呼叫變數
在For裡面呼叫 %size%會一直是零,原因上面有提到,為了要反應迴圈內的變數要使用 !size! 去呼叫變數。
這樣的呼叫方式第一次會取set size=0的值存在另一個位置
而後在迴圈內每次呼叫!size!都會使用這個新的位置進行儲存和讀取
結論就是:
要在回圈中使用變數要增加參數enableextensions enabledelayedexpansion
迴圈中呼叫變數的方法為 !變數!
迴圈外呼叫變數的方法還是 %變數%
最後展示某個資料夾即子資料夾的檔案,超過修改時間7天後會被刪除
@setlocal enableextensions enabledelayedexpansion
echo off
set size=0

For /R  T:\abc\def\ %%A in (*) do (
rem 把檔案的日期存到變數tmp
set tmp=%%~tA

For /F "tokens=1,2,3 delims=/ " %%c in ("%%~tA") do (
  rem 檢查檔案日期的年份,如果比現在的年份小就刪除 
  if %date:~0,4% GTR %%c (
    del /f /q "%%A"
 echo %%A
 set /a size=!size! + %%~zA
  ) else (
    rem 檢查檔案日期的月份,如果比現在的月份小就刪除
    if %date:~5,2% GTR %%d (
   del /f /q "%%A"
   echo %%A
   set /a size=!size! + %%~zA
 ) else (
 set tmp_2=%%e
 rem 因為DOS變數的關係,要檢查日期是不是兩位數並做一些處理
 if "!tmp_2:~0,1!" == "0" (
   set /a tmp_2=!tmp_2:~1,1! + 6
   if  "!tmp_2:~0,1!" == "0" (set tmp_2=0!tmp_2! ) 
 ) else (
   set /a tmp_2=!tmp_2:~0,2! + 6 
 )
   rem 檢查檔案月份的天數,如果比現在的天數小7天就刪除
   if %date:~8,2% GTR tmp_2 (
     del /f /q "%%A"
        echo %%A
     set /a size=!size! + %%~zA
   )))
)

)
echo 總刪除的檔案大小 : %size%
pause
endlocal
怕有人使用後誤刪檔案,這個例子中的路徑是亂設定的。
例子內並沒有真的準確7天,在年和月的部分有可能差不到7天
可以用類似( set /a xx=30-%date:~8,2%+6) & if xx GTR 7 )的方式比較月的部分有沒有超過7天

For printing file contents
type "file"

Like windows regedit
reg /?
REG Operation [Parameter List]
  Operation  [ QUERY   | ADD    | DELETE  | COPY    |
               SAVE    | LOAD   | UNLOAD  | RESTORE |
               COMPARE | EXPORT | IMPORT  | FLAGS ]
Return Code: (Except for REG COMPARE)
  0 - Successful
  1 - Failed
For help on a specific operation type:
  REG Operation /?
Examples:
  REG QUERY /?
  REG ADD /?
  REG DELETE /?
  REG COPY /?
  REG SAVE /?
  REG RESTORE /?
  REG LOAD /?
  REG UNLOAD /?
  REG COMPARE /?
  REG EXPORT /?
  REG IMPORT /?
  REG FLAGS /?