Batch etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
Batch etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

14 Temmuz 2022 Perşembe

Windows Batch pushd

Giriş
Açıklaması şöyle
To remember a folder, you could use the pushd command together with the popd command.

To remember a folder :

pushd
To return to the remembered folder :

popd
These commands use a stack that can remember more than one folder.

17 Eylül 2020 Perşembe

Windows Batch SET

/A seçeneği
Örnek 
Şöyle yaparız
@echo off
set /a a=30 %% 35
echo %a%
pause

10 Haziran 2020 Çarşamba

Windows Batch ECHO OFF

Giriş
Batch dosyaları otomatik olarak echo on olarak başlarlar. Bunun tarihsel bir sebebi var. Açıklaması şöyle
ECHO ON was chosen as the default setting when interpreting batch files to preserve backwards compatibility. In PC-DOS 1.0, COMMAND.COM displayed each command as it interpreted it, and this couldn’t be disabled. ECHO was added in PC/MS-DOS 2.0, with a dual purpose (displaying messages, and controlling the display of batch file commands); its default is ON so that the behaviour of batch files written for DOS 1.0 doesn’t change.
Dosyanın başına @echo off yazılır. Böylece komutların çıktısı ekranda görünmez.

@ karakterinden 1 veya daha fazla olması fark yaratmaz.

Örnek
Şöyle yaparız.
@@@@@@@@echo off
ping localhost
ping google
echo test string
Normalde şöyle yaparız.
@echo off
ping localhost
ping google
echo test string

6 Mart 2020 Cuma

Windows Batch Kodlama - ERRORLEVEL gömülü değişkeni

Giriş
Açıklaması şöyle.
A pseudo environment variable named errorlevel stores the exit code:
Örnek
Şöyle yaparız. EQU 0 'dan sonra gelen açılış parantezi için bir boşluk olmalı
@echo off
my_nify_exe.exe
if %ERRORLEVEL% EQU 0 (
   echo Success
) else (
   echo Failure Reason Given is %errorlevel%
   exit /b %errorlevel%
)

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

Windows Batch CALL komutu - Başka Bir Bat Dosyasını Çalıştırır

Giriş
Açıklaması şöyle. Yani aslında sadece bit bat dosyasından, başka bir bat dosyasını çalıştırmak için kullanılmalı.
Calls one batch program from another, calls a subprogram within a single batch program, or, as an undocumented behavior, starts a program. In particular, suspends the execution of the caller, starts executing the callee, and resumes the execution of the caller if and when the callee finishes execution.
...

Beware that calling a batch program from a batch without using the call keyword results in the execution never returning to the caller once the callee finishes.

The callee inherits environment variables of the caller, and unless the callee prevents that via SETLOCAL, changes made by the callee to environment variables become visible to the caller once it resumes execution.
Örnek
Şöyle yaparız.
@echo off

cd C:\Program Files (x86)\Project
CALL init_env.bat
Örnek
Şöyle yaparız. command1, command2.cmd ve command3 sırayla çalışır
command1 & call command2.cmd & command3

23 Aralık 2019 Pazartesi

Windows Batch START komutu - Yeni Bir Process Başlatır

Giriş
Tek başına çalışan bir process başlatır. Açıklaması şöyle
If you type start filename.ext in command prompt, the file is run using explorer's engine, and as such whatever is associated to that file extension will start the file.
Örnek
Bir kısayolu (shortcut) çalıştırmak için şöyle yaparız.
start mmsys.cpl
Örnek
Şöyle yaparız.
start winword
Örnek - pencere ismi
Şöyle yaparız
start "" "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC"

:: # Or more elaborately:
start "Optional Window Title" "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC"
wait seçeneği
Açıklaması şöyle.
start a command in a new window, wait for it to complete, then run another command locally and call a batch file.
Şöyle yaparız.
start "" /wait command1 & command2 & call command3.cmd