2 Mart 2020 Pazartesi

Windows Batch For Döngüsü

Giriş
Döngü için tanımlanan %%i değişkeni eğer batch dosyası içindeyse çift yüzde karakteri ile kullanılır. Komut satırında ise tek % karakteri ile kullanılır.

Açıklaması şöyle
Be careful however as setting and using variables inside of a loop may require delayed expansion
1. Tilde Magic
For döngülerinde bazen tilde karakteri görülüyor. Açıklaması şöyle
The tilde (~) sign is used in different ways in batch files:

- Argument quote removal. A tilde sign before an command-line argument (such as "%~1") indicates to remove the surrounding quotes from the parameter. Such if the value for %1 is "Hi" then %~1 will expand to only Hi.

- Substring processing. There is a way to extract substring from a string variable in batch file. The syntax is: %variable:~num_chars_skip,num_chars_keep%. Num chars skip means the point where to start in the string, or to exclude how much characters preceding the string variable. And num chars to keep indicates the number of characters after the start point. Num chars to keep is optional, but first is mandatory. If num chars to keep is not specified, only that num chars to keep'th character will be parsed.
Yani birinci amacı parametredeki quote karakterini kaldırmak

Örnek  - Argument Quote Removal
Elimizde şöyle bir kod olsun
@Echo off
For %%A IN (
    "this is a sentence"
    two words
    "some words"
) Do (
    Echo with tilde: "%%~A"
    Echo w/o  tilde: "%%A"
    Echo:
)
Çıktı olarak şunu alırız. Çıktıda "some words" parametresinin tilde ile gösterilirken quote karakterinin çıkarıldığı görülebilir.
> SU1397704.cmd
with tilde: "this is a sentence"
w/o  tilde: ""this is a sentence""

with tilde: "two"
w/o  tilde: "two"

with tilde: "words"
w/o  tilde: "words"

with tilde: "some words"
w/o  tilde: ""some words""

2. Magic Variables
For döngülerinde magic değişkenler de görülebilir. Açıklaması şöyle
- The magic variables %n contains the arguments used to invoke the file: 
- %0 is the path to the bat-file itself
- %1 is the first argument after, %2 is the second and so on.

Since the arguments are often file paths, there is some additional syntax to extract parts of the path. 
- ~d is drive
- ~p is the path (without drive), 
- ~n is the file name. They can be combined so ~dp is drive+path.

- %~dp0 is therefore pretty useful in a bat: it is the folder in which the executing bat file resides.

You can also get other kinds of meta info about the file: 
- ~t is the timestamp, 
- ~z is the size.
Örnek
Şöyle yaparız. Burada komut satırında çalışıldığı için tek % karakteri var. Batch dosyası olsaydı çift %% kullanmak gerekirdi
for %a in (*.txt) do ren "%~a" "%~na version 1%~xa"
Dosyaları şöyle rename eder. %~na ile a değişkenindeki dosya ismi alınır. %~xa ile a değişkenindeki uzantı alınır
File 1.txt  ->  File 1 version 1.txt
File 2.txt  ->  File 2 version 1.txt
File 3.txt  ->  File 3 version 1.txt
File 4.txt  ->  File 4 version 1.txt
...
3. @ Karakteri
Açıklaması şöyle. Eğer @echo off yapılmamışsa, normalde çalıştırılan komutu görürüz. Bunu da görmemek için komutun başına @ karakteri konulur
The @ symbol tells the command processor to be less verbose; to only show the output of the command without showing it being executed or any prompts associated with the execution. When used it is prepended to the beginning of the command, it is not necessary to leave a space between the "@" and the command.

When "echo" is set to "off" it is not necessary to use "@" because setting "echo" to "off" causes this behavior to become automatic. "Echo" is usually set to "on" by default when the execution of a script begins. This is the reason "@echo off" is commonly used, to turn echo off without displaying the act of turning it off.
4. Seçenekler
/D seçeneği - Directory
Örnek
C:\test altındaki ismi target olan dizinleri tamamıyla (alt dizinleri dahil) silmek için şöyle yaparız
for /d /r c:\test %%d in (target) do @IF EXIST "%%d" rd /s /q %%d
/F seçeneği - File Contents
Hem bir dosyayı hem de bir başka uygulamanın çıktısını okumak için de kullanılır

Örnek
Şöyle yaparız.
for /F %%x in (Test.txt) do @echo %%x
Örnek
Şöyle yaparız.
for /F %%I in (Test.txt) do (
   echo %%i
   echo "%%i"
   echo etc
)
/L seçeneği - Range of Numbers
Örnek
Şöyle yaparız. 1'den başla. 1 artır ve 94'e kadar (dahil) git anlamına gelir.
@echo off
setlocal enableDelayedExpansion
FOR /l %%N in (1,1,94) do (
  set "NUM=00%%N"
  set "DIRNAME=ch.!NUM:~-3!
  md !DIRNAME!
)
Örnek
Komut satırında tek % ile kullanarak şöyle yaparız
for /L %i in (0,1,60) do type source.txt>>dest.txt
Örnek
Batch dosyasında kullanmak için şöyle yaparız
for /L %%i in (0,1,%3) do type %1>>%2

:: %1 source file
:: %2 destination file
:: %3 number of inserts

Hiç yorum yok:

Yorum Gönder