Popular Posts
Activate IIS GIZP compress 1. Set metabase editable for editing. 2. Add a new extension for gzip. 3. Edit the file c:\windows\system32\inetsrv\MetaBase.xml Add file... Sublime Text hotkeys 編輯 Ctrl + X 刪除行 Ctrl + Enter 插入下一行 Ctrl + Shift + Enter 插入前一行 Ctrl + Shift + ↑ 往上移動一行 C... Tomcat GET encoding edit server.xml : <Connector     URIEncoding="UTF-8"     connectionTimeout="20000"     port="8080"     prot...
Stats
Showing posts with label Batch. Show all posts
Showing posts with label Batch. Show all posts
ROBOCOPY: Robust File Copy for Windows
-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Wednesday, December 25, 2013 11:26:07 AM
              Usage :: ROBOCOPY source destination [file [file]...] [options]

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               file :: File(s) to copy  (names/wildcards: default is "*.*").

::
:: Copy options :
::
                 /S :: copy Subdirectories, but not empty ones.
                 /E :: copy subdirectories, including Empty ones.
             /LEV:n :: only copy the top n LEVels of the source directory tree.

                 /Z :: copy files in restartable mode.
                 /B :: copy files in Backup mode.
                /ZB :: use restartable mode; if access denied use Backup mode.
                 /J :: copy using unbuffered I/O (recommended for large files).
            /EFSRAW :: copy all encrypted files in EFS RAW mode.

  /COPY:copyflag[s] :: what to COPY for files (default is /COPY:DAT).
                       (copyflags : D=Data, A=Attributes, T=Timestamps).
                       (S=Security=NTFS ACLs, O=Owner info, U=aUditing info).


               /SEC :: copy files with SECurity (equivalent to /COPY:DATS).
           /COPYALL :: COPY ALL file info (equivalent to /COPY:DATSOU).
            /NOCOPY :: COPY NO file info (useful with /PURGE).
            /SECFIX :: FIX file SECurity on all files, even skipped files.
            /TIMFIX :: FIX file TIMes on all files, even skipped files.

             /PURGE :: delete dest files/dirs that no longer exist in source.
               /MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).

               /MOV :: MOVe files (delete from source after copying).
              /MOVE :: MOVE files AND dirs (delete from source after copying).

     /A+:[RASHCNET] :: add the given Attributes to copied files.
     /A-:[RASHCNET] :: remove the given Attributes from copied files.

            /CREATE :: CREATE directory tree and zero-length files only.
               /FAT :: create destination files using 8.3 FAT file names only.
               /256 :: turn off very long path (> 256 characters) support.

             /MON:n :: MONitor source; run again when more than n changes seen.
             /MOT:m :: MOnitor source; run again in m minutes Time, if changed.

      /RH:hhmm-hhmm :: Run Hours - times when new copies may be started.
                /PF :: check run hours on a Per File (not per pass) basis.

             /IPG:n :: Inter-Packet Gap (ms), to free bandwidth on slow lines.

                /SL :: copy symbolic links versus the target.

            /MT[:n] :: Do multi-threaded copies with n threads (default 8).
                       n must be at least 1 and not greater than 128.
                       This option is incompatible with the /IPG and /EFSRAW options.
                       Redirect output using /LOG option for better performance.

 /DCOPY:copyflag[s] :: what to COPY for directories (default is /DCOPY:DA).
                       (copyflags : D=Data, A=Attributes, T=Timestamps).

           /NODCOPY :: COPY NO directory info (by default /DCOPY:DA is done).

         /NOOFFLOAD :: copy files without using the Windows Copy Offload mechanism.

::
:: File Selection Options :
::
                 /A :: copy only files with the Archive attribute set.
                 /M :: copy only files with the Archive attribute and reset it.
    /IA:[RASHCNETO] :: Include only files with any of the given Attributes set.
    /XA:[RASHCNETO] :: eXclude files with any of the given Attributes set.

 /XF file [file]... :: eXclude Files matching given names/paths/wildcards.
 /XD dirs [dirs]... :: eXclude Directories matching given names/paths.

                /XC :: eXclude Changed files.
                /XN :: eXclude Newer files.
                /XO :: eXclude Older files.
                /XX :: eXclude eXtra files and directories.
                /XL :: eXclude Lonely files and directories.
                /IS :: Include Same files.
                /IT :: Include Tweaked files.

             /MAX:n :: MAXimum file size - exclude files bigger than n bytes.
             /MIN:n :: MINimum file size - exclude files smaller than n bytes.

          /MAXAGE:n :: MAXimum file AGE - exclude files older than n days/date.
          /MINAGE:n :: MINimum file AGE - exclude files newer than n days/date.
          /MAXLAD:n :: MAXimum Last Access Date - exclude files unused since n.
          /MINLAD:n :: MINimum Last Access Date - exclude files used since n.
                       (If n < 1900 then n = n days, else n = YYYYMMDD date).

                /XJ :: eXclude Junction points. (normally included by default).

               /FFT :: assume FAT File Times (2-second granularity).
               /DST :: compensate for one-hour DST time differences.

               /XJD :: eXclude Junction points for Directories.
               /XJF :: eXclude Junction points for Files.

::
:: Retry Options :
::
               /R:n :: number of Retries on failed copies: default 1 million.
               /W:n :: Wait time between retries: default is 30 seconds.

               /REG :: Save /R:n and /W:n in the Registry as default settings.

               /TBD :: wait for sharenames To Be Defined (retry error 67).

::
:: Logging Options :
::
                 /L :: List only - don't copy, timestamp or delete any files.
                 /X :: report all eXtra files, not just those selected.
                 /V :: produce Verbose output, showing skipped files.
                /TS :: include source file Time Stamps in the output.
                /FP :: include Full Pathname of files in the output.
             /BYTES :: Print sizes as bytes.

                /NS :: No Size - don't log file sizes.
                /NC :: No Class - don't log file classes.
               /NFL :: No File List - don't log file names.
               /NDL :: No Directory List - don't log directory names.

                /NP :: No Progress - don't display percentage copied.
               /ETA :: show Estimated Time of Arrival of copied files.

          /LOG:file :: output status to LOG file (overwrite existing log).
         /LOG+:file :: output status to LOG file (append to existing log).

       /UNILOG:file :: output status to LOG file as UNICODE (overwrite existing log).
      /UNILOG+:file :: output status to LOG file as UNICODE (append to existing log).

               /TEE :: output to console window, as well as the log file.

               /NJH :: No Job Header.
               /NJS :: No Job Summary.

           /UNICODE :: output status as UNICODE.

::
:: Job Options :
::
       /JOB:jobname :: take parameters from the named JOB file.
      /SAVE:jobname :: SAVE parameters to the named job file
              /QUIT :: QUIT after processing command line (to view parameters).
              /NOSD :: NO Source Directory is specified.
              /NODD :: NO Destination Directory is specified.
                /IF :: Include the following Files.

Reference: ROBOCOPY

MKLINK: Creates a directory symbolic link
MKLINK [[/D] | [/H] | [/J]] Link Target                                    
                                                                           
        /D      Creates a directory symbolic link.  Default is a file      
                symbolic link.                                             
        /H      Creates a hard link instead of a symbolic link.            
        /J      Creates a Directory Junction.                              
        Link    specifies the new symbolic link name.                      
        Target  specifies the path (relative or absolute) that the new link
                refers to.                                                 

Reference : Mklink

Related : junction

runas
RUNAS 使用方法:

RUNAS [ [/noprofile | /profile] [/env] [/netonly] ]
        /user: program

RUNAS [ [/noprofile | /profile] [/env] [/netonly] ]
        /smartcard [/user:] program

   /noprofile        指定使用者的設定檔不該載入。
                     這會導致應用程式載入速度更快,
                     但可能引起一些應用程式運作失常。
   /profile          指定應該載入使用者的設定檔。
                     這是預設值。
   /env              使用目前的環境,不用使用者設定的環境。
   /netonly          如果指定的憑證是供遠端存取時才使用。

   /savecred         使用之前由使用者儲存的認證。
                     此選項在 Windows XP Home Edition 上無法使用
                     而且會被略過。
   /smartcard        當智慧卡提供了認證時使用。

   /user              格式如下 USER@DOMAIN 或 DOMAIN\USER
   program         EXE 的命令列。範例如下

範例:
> runas /noprofile /user:mymachine\administrator cmd
> runas /profile /env /user:mydomain\admin "mmc %windir%\system32\dsa.msc"
> runas /env /user:user@domain.microsoft.com "notepad \"my file.txt\""

注意:  只有在提示時,才輸入使用者密碼。
注意:  USER@DOMAIN 與 /netonly 不相容。
注意:  /profile 與 /netonly 不相容。
Start/stop oracle
REM This batch file is used to start/stop the oracle instance
REM Simply invoke oracle.bat start/stop
cls
@ECHO off
REM ******************************************************************
REM ** Batch file used to stop/start oracle services
REM ******************************************************************

SET _db_=10g_home1
SET _sid_=CCIMES


if(%1)==(start) GOTO START
if(%1)==(stop) GOTO STOP
echo Invalid use. Usage is oracle.bat stop/start
GOTO END


:START
REM ** START the oracle services
echo Starting Oracle Services
REM change the instance/listener names to match what is in your services list
NET START OracleCSService
NET START OracleOraDB%_db_%iSQL*Plus
NET START OracleOraDb%_db_%TNSListener
NET START OracleService%_sid_%
NET START OracleDBConsole%_sid_%
REM NET START OracleJobScheduler%_sid_%
REM NET START "Oracle INNOVATEST VSS Writer Service"
echo Oracle Services Started
GOTO END


:STOP
echo Stopping Oracle Services
REM change the instance/listener names to match what is in your services list
REM NET STOP "Oracle INNOVATEST VSS Writer Service"
REM NET STOP OracleJobScheduler%_sid_%
NET STOP OracleDBConsole%_sid_%
NET STOP OracleOraDb%_db_%TNSListener
NET STOP OracleService%_sid_%
NET STOP OracleOraDB%_db_%iSQL*Plus
NET STOP OracleCSService
echo Oracle Services Stopped
GOTO END


:END
File properties
FOR %%? IN (file_to_be_queried) DO (
    ECHO File Name Only       : %%~n?
    ECHO File Extension       : %%~x?
    ECHO Name in 8.3 notation : %%~sn?
    ECHO File Attributes      : %%~a?
    ECHO Located on Drive     : %%~d?
    ECHO File Size            : %%~z?
    ECHO Last-Modified Date   : %%~t?
    ECHO Parent Folder        : %%~dp?
    ECHO Fully Qualified Path : %%~f?
    ECHO FQP in 8.3 notation  : %%~sf?
    ECHO Location in the PATH : %%~dp$PATH:?
)
Test file exist in batch
@echo off
SET file=mes.log
SET filename=mes

for /f %%a in ('dir /b /a-d %file%') do (
 if %%~na==%filename% GOTO :FileFound
)
GOTO :FileNotFound

:FileFound
ECHO FILE FOUND
GOTO :BatchEnd

:FileNotFound
ECHO FILE NOT FOUND

:BatchEnd
ECHO END
Change network configuration (windows)
Manual setting
@ECHO OFF
set varAdapterName="Local Area Connection"
set varIP=192.168.1.68
set varMask=255.255.255.0
set varGateway=192.168.1.1
set varDNS1=192.168.1.1
set varDNS2=192.168.1.1

ECHO Setting IP Address and Subnet Mask
netsh int ip set address name = %varAdapterName% source = static addr = %varIP% mask = %varMask%

ECHO Setting Gateway
netsh int ip set address name = %varAdapterName% gateway = %varGateway% gwmetric = 1

ECHO Setting Primary DNS
netsh int ip set dns name = %varAdapterName% source = static addr = %varDNS1%

ECHO Setting Secondary DNS
netsh int ip add dns name = %varAdapterName% addr = %varDNS2%
DHCP
@set varAdapterName="Local Area Connection"

netsh interface ip set address name=%varAdapterName% source=dhcp
netsh interface ip set dns name=%varAdapterName% source=dhcp register=primary
netsh interface ip set wins name=%varAdapterName% source=dhcp
Enable/disable network adapter
windows:
netsh interface set interface <interface name> DISABLED
netsh interface set interface <interface name> ENABLED
linux:
ifconfig eth1 down
ifconfig eth1 up
Get files name in batch
  • Windows Batch : get filename using [%%~ni]
  • Linux Shell Script : get filename using [awk, sed]
windows sample : rename all *.htm file to *.html
for %%i in (*.htm) do ren "%%~ni.htm" "%%~ni.html"
about the "for" command :
對一組檔案中的每個檔案執行指定的命令。

FOR %variable IN (set) DO 命令 [command-parameters]

  %variable 指定一個可以取代的參數。
  (set)      指定由一或多個檔案組成的檔案組。您可使用通配字元。
  command    指定命令來執行每一個檔案。
 command-parameters
             為所指定的命令指定變數或參數。

如果要在批次程式中使用 FOR 命令,請指定 %%variable,而不要指定
%variable。  變數名稱有大小寫的區分,所以 %i 不同於 %I。

如果您啟用擴充命令,則額外支援下列的 FOR 命令格式:

FOR /D %variable IN (set) DO command  [command-parameters]

    如果 set 中包含萬用字元,則指定與目錄名稱相符,而不是與檔案
    名稱相符。

FOR /R [[drive:]path] %variable IN (set) DO command  [command-parameters]

    在樹狀目錄中切換 [drive:]路徑,並於樹狀目錄的每一個目錄下執行
    FOR 陳述式。如果未在 /R 之後指定目錄規格,則採用目前的目錄。
    如果 set 只是單一句點 (.) 字元,則它只會列舉樹狀目錄結構。

FOR /L %variable IN (start,step,end) DO command [command-parameters]

    set 是從開頭到結尾一次跳一步的連續數字。所以 (1,1,5) 會產生
    連續值 (1 2 3 4 5) 而 (5,-1,1) 會產生連續值 (5 4 3 2 1)

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

    或,如果使用 usebackq 選項:

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters]
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]

    filenameset 可以是一個以上的檔案名稱。每個檔案都已開啟,
    讀取及處理過,才繼續進行下個檔案名稱組。處理檔案讀取的一致
    性,將它分成獨立的文字行,然後將每一行分析成零或更多的字串。
    用已找到的字串值為變數值,來呼叫 For 迴圈的內容。預設狀態,
    /F 傳出每個檔案的每一行中,以空格分隔的第一個字串。空白行
    會被略過。您可以指定選擇性的 "options" 參數來覆寫預設的分析行為。 
    這是有引號的字串,包含一個以上的關鍵字,來指定不同的分析
    選項。關鍵字是:

        eol=c           - 指定一個行尾註解字元 (只有一個)
        skip=n          - 指定在檔案開頭要掠過的行數。
        delims=xxx      - 指定分隔符號的集合。  這會取代預設的空白
                          與定位字元的分隔符號集合。
        tokens=x,y,m-n  - 指定每一行的哪些文字串應該被傳到 for 的內
                          部以用來進行每一個重複操作。這會導致額外的
                          變數名稱被配置。 m-n 格式代表一個範圍,指
                          定了第 m 個到第 n 個字串項。如果在 tokens=
                          字串的最後一個字元是星號,則會配置一個額外
                          的變數來接收最後一個字串項被分析後的其他文
                          字。
        usebackq        - 指定新語義開始作用。其中反括號的字串會被當
                          作命令來執行,而單引號字串是純文字字串。此
                          外還允許使用雙引號來引用在 filenameset 內
                          的檔名。

    以下是一個範例:

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

    這會分析 myfile.txt 檔案中的每一行,它不會去管以分號開頭的行數
    ,直接將第 2 個及第 3 個語法從每一行傳到 for 主體,而其語法是
    用逗號和/或空格分開的。請注意,for 主體陳述式參照 %i 以取得第
    二個語法,參照 %j 以取得第三個語法,使用 %k 取得第三個語法之
    後的剩餘字串。因為檔案名稱含有空格,您必須用雙引號來括住檔案名
    稱。要這樣使用雙引號,您必須使用 usebackq 參數。否則雙引號會被
    解譯成用來定義一般文字。

    使用 %i 明白地在 for 陳述式中宣告,並透過 tokens= option 使用
    %j 作暗式性的宣告。您可以藉由 tokens= line 來指定最多 26 個語
    法,前提是它宣告的變數不能高於字母 'z' 或 'Z'。請記住,FOR 變
    數是單一字元的,同時在任一時間內,您不能同時使用超過 52 個 FOR
    變數。

    您也可以使用 FOR /F 命令在立即字串中分析邏輯,方法是將括弧之間的
    filenameset 變成一個引號字串。它會被視為從檔案輸入的單行,並加
    以分析。

    最後,您可以使用 FOR /F 命令來分析一個命令的輸出。方法是將括弧
    內的 filenameset 變成單引號字串。它將被視為一個命令列,這個命令
    行將會傳到子 CMD.EXE,而輸出將會被擷取到記憶體中,當成檔案來分
    析。所以下列的範例:

        FOR /F "delims==" %i IN ('set') DO @echo %i

    將列舉目前環境中的環境變數名稱。

此外,已經加強了 FOR 變數參考的取代功能。
您現在可以選用下列選擇性的語法:

    %~I         - 展開 %I 且移除包圍的引號 (")
    %~fI        - 展開 %I 為一個完全符合的路徑名稱
    %~dI        - 只展開 %I 為磁碟機代號
    %~pI        - 只展開 %I 為路徑
    %~nI        - 只展開 %I 為檔名
    %~xI        - 只展開 %I 為副檔名
    %~sI        - 展開的路徑只包含短檔名
    %~aI        - 展開 %I 為檔案的檔案屬性
    %~tI        - 展開 %I 為檔案的日期/時間
    %~zI        - 展開 %I 檔案的長度
    %~$PATH:I   - 搜尋所有列在 PATH 環境變數內的目錄且展開 %I
                   為第一個找到的完全符合檔名。如果沒有定義環境
                   變數名稱或是搜尋找不到檔案,則這個修飾元會展
                   開為空字串。

修飾元可以合併使用以獲得綜合的結果:

    %~dpI       - 只展開 %I 為磁碟機代號與路徑
    %~nxI       - 只展開 %I 為檔名與副檔名
    %~fsI       - 只展開 %I 為含短檔名的完全路徑
    %~dp$PATH:I - 為 %I 搜尋所有列在 PATH 環境變數內的目錄
                   且展開第一個找到的項目為磁碟機代號及路徑。
    %~ftzaI     - 展開 %I 為像 DIR 一樣的輸出行

在上面的範例中 %I 和 PATH 能用其他的合法值取代。%~ 語法是由合法的
FOR 變數名稱來終止。如果選用像 %I 的大寫名稱可以增加可讀性而且避免
和修飾元的混淆,因為這些並不區分大小寫。