Practical examples with batch

Diese Seite gibt es auch in Deutsch

Handling variables in Windows Batch (command prompt)

Read all variables

Variables can be read out in the command prompt with the set command:

set

Output:

C:\Users\username>set
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\username\AppData\Roaming
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=MY-COMPUTERNAME
ComSpec=C:\WINDOWS\system32\cmd.exe
FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer
FPS_BROWSER_USER_PROFILE_STRING=Default
HOMEDRIVE=C:
HOMEPATH=\Users\username
LOCALAPPDATA=C:\Users\username\AppData\Local
...

Set variable

SET Variable=hallo

Use variable

echo %Variable%

Output:

C:\Users\username>echo %Variable%
hallo

Replace string in a variable

SET Variable=hallo
echo %Variable:hall=hell%

Output:

C:\Users\username>SET Variable=hallo
C:\Users\username>echo %Variable:hall=hell%
hello

Date

set jahr=%date:~-4%
set monat=%date:~-7,2%
set tag=%date:~-10,2%


Example: echo %jahr%%monat%%tag%
Output:

C:\Users\username>set jahr=%date:~-4%
C:\Users\username>set monat=%date:~-7,2%
C:\Users\username>set tag=%date:~-10,2%
C:\Users\username>echo %jahr%%monat%%tag%
20161017

Time

set stunde=%time:~0,2%
set minute=%time:~3,2%


set stunde=%time:~0,2%
set minute=%time:~3,2%
Example: echo %stunde%:%minute%

Output:

C:\Users\username>set stunde=%time:~0,2%
C:\Users\username>set minute=%time:~3,2%
C:\Users\username>echo %stunde%:%minute%
11:01

Create a file, add to it:

echo hello > temp.txt
this command line creates a temp.txt file and writes hello into it! if the temp.txt file already exists, the whole content of the file will be overwritten!

echo hello >> temp.txt
adds the text hello to temp.txt, i.e. with each time the command is called, there is once more hello in temp.txt,
the content of the file remains!

Automate FTP:

Commands:

open ... Opens the FTP connection; requires: Server, username and password
put ... sends a file
get ... downloads a file
lcd ... change directory on the computer
cd ... change directory on server
bye ... terminates the FTP connection

For image and program files you have to use the binary mode,
for text files the ASCII mode.

Example:

OPEN www.ftpserver.at
benutzername
passwort

CD html
ASCII
PUT C:\eigene~1\homepage\index.htm
CD ..

CD bilder
BINARY
put C:\eigene~1\homepage\bilder\bild.gif bild.gif
bye


Explanation: the file opens the FTP connection, switches to ASCII mode (for text files);
copies all index.htm file to the server;
then change to the image directory on the computer, switch to BINARY - mode and send the image.gif- file!

the whole you save best in a .ftp-file, this is then called with a batch file as follows:
Batch file call:
ftp -s:dieftpfile.ftp

automatically download files:

wget for Windows see http://gnuwin32.sourceforge.net/packages/wget.htm

download.exe
to be found at: www.sql-und-xml.de

Syntax:
download https://www.libe.net/seite/bild.gif c:\download\image.gif user password

Edit images

Import and export to numerous graphic formats
multi-frame TIFF, animated GIF & ICO
Resize image
Adjust brightness, contrast, ...
Change color depth
Apply filters (blur, relief, ...)
Apply effects (lens, wave, ...)

with the cmd tool: nconvert
see: www.xnview.com

Change IP address:

netsh interface ip set address local static 192.168.0.1 255.255.255.0   

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!


Questions / Comments