User Tools

Site Tools


batch

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
batch [2011/11/17 17:57] – created skipidarbatch [2020/12/27 20:35] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +===== Fallpits =====
 +<code>
 +#when using variables no empty spaces after equal
 +VAR TEST1="WILL WORK"
 +VAR TEST2 ="VAR WILL BE EMPTY"
 +
 +</code>
 +
 +
 +
 +===== call =====
 +To call another *.bat use
 +<code>
 +call "D:\...\compile.cmd"
 +</code>
 +
 +===== start =====
 +To start a programm in the console, and pass it some **parameters with empty spaces** use the following syntax:
 +
 +<code>
 +start "WINDOWNAME" /B "D:\Programme Portable\Putty\putty.exe" -ssh ubuntu@ec2-1-22-33-44.eu-east-3.compute.amazonaws.com:22 -l ubuntu -i "D:/Path to Key/ssh-putty-ubuntukey-private.ppk"
 +</code>
 +
 +The first argument of "start" - is the window title. Pass an empty one, for the next command to be recognized as the path to the programm.
 +
 +**/B** flag makes windows not creating a new window.
 +
 +Documentation start command: http://ss64.com/nt/start.html
 +
 +
 +Start cm with a command works like this. Use **/k** to start cmd with a commad.
 +<code>
 +start cmd /k "robocopy c:/path/to/dir c:/path/to/backup /E"
 +</code>
 +
 +
 +===== Printers =====
 +
 +Put the following into the "run" field to see all possible commands.
 +<code>
 +rundll32 printui.dll,PrintUIEntry /?
 +</code>
 +
 +
 +<code>
 +:: delete network printer per batch
 +rundll32 printui.dll,PrintUIEntry /dn /n "\\brian\OKI C5100 (v2)"
 +</code>
 +
 +<code>
 +:: install a network printer per batch
 +rundll32 printui.dll,PrintUIEntry /q /in /n "\\brian\HPColorSekri"
 +</code>
 +
 +== Counter ==
 +
 +To use Counters - deplayed expanation must be used in batch.
 +The Variables are then translated to numers eveny loop.
 +  * It must be enabled by **setlocal ENABLEDELAYEDEXPANSION**
 +  * variables are loaded by **!var!** von %var%
 +
 +
 +<code>
 +setlocal ENABLEDELAYEDEXPANSION
 +
 +set /A cnt=1
 +
 +for %%f in (*.cer) do (
 +            echo %%~nf
 +            set /A cnt=cnt+1
 +     echo !cnt!
 +)
 +</code>
 +
 +
 +
 +=== Commands ===
 +
 +== SUBST==
 +Create a virtual disk from a LOCAL folder
 +<code>
 +subst z: b:\user\betty\forms
 +
 +# delete
 +subst z: b:\user\betty\forms \D
 +</code>
 +
 +
 +
 +=== SCRIPT to find java process by command line argments ===
 +
 +Name it findJavaProcessByCommandline.bat
 +Usage:
 +
 +<code>
 +call findJavaProcessByCommandline.bat javaCommandLinePart
 +if errorlevel 1 echo NoFindings & exit /b 1
 +</code>
 +
 +|if errorlevel 1 | check result of nested script. 1 if nothing found|
 +|echo NoFindings | Echo|
 +|& exit /b 1| and exits the parent script. Instead you can do whatever you want with this info |
 +
 +<code>
 +@echo off
 +
 +:: check if empty
 +IF [%1] == [] GOTO noarg
 +
 +:: use the argument
 +set likeString=%1
 +
 +
 +setlocal EnableDelayedExpansion
 +set processIdFinding=
 +for /f %%i in ('wmic process where "Caption Like 'java.exe' AND CommandLine Like '%%!likeString!%%' " get ProcessId') do (
 + set processIdFinding=!processIdFinding!%%i
 +)
 +
 +if "%processIdFinding%"==""
 +  goto nofindings
 +) else (
 +  goto findings
 +)
 +
 +:nofindings
 +echo Nothing Found 
 +exit /b 1
 +
 +:findings
 +echo Finding: %processIdFinding%
 +exit /b 0
 +
 +:noarg
 +echo Please provide the Stringto be searched withing java process commandline as an argument
 +exit /b 1
 +</code>