⬅️ **[[$-Tools|Tools]]**
***
# Windows Shell CMD
## CMD DOS
| Description | Command |
| -------------------------- | ----------------------------------- |
| Change Directory | cd in Bash, cd or chdir in DOS |
| List Contents of Directory | ls in Bash, dir in DOS |
| Move or Rename a File | mv in Bash, move and rename in DOS |
| Copy a File | p in Bash, copy in DOS |
| Delete a File | rm in Bash, del or erase in DOS |
| Use a Text Editor | vi or nano in Bash, edit in DOS |
| Create a Directory | mkdir in Bash, mkdir in DOS |
## Symbolic Links / Verknüpfungen
- A **soft link** is referenced as a **symbolic link** and works similarly to a standard shortcut. Soft links will have a shortcut arrow icon on them. For example, when you open a soft link to a folder, you will be redirected to the folder where the files are stored.
- A **hard link** _(or file hard link)_ makes it appear as though the file or folder actually exists at the location of the symbolic link, and your app won’t know any better. That can make hard symbolic links more useful in most situations. Hard links to a file will not have a shortcut arrow icon on them.
- **Junction Point** _(or directory hard link)_ is a type of hard link that acts like a representation of a _directory_, a _partition_ or another _volume_.
- `mklink [/D] | [/H] | [/J] <Verknüpfung> <Ziel>`
- D - Softlink => file, directory
- H - Hard Link => file
- J - Hard Link Junction Point => directory
- **Beispiel:** Symbolischer Link zu `C:\MCE-Nextcloud` mit `mklink /J C:\MCE-Nextcloud D:\Nextcloud-Windows-MCE\`
## Windows Batch Scripts
| Description | Command |
| ---------------------------- | ---------------------------------------------------------------------------------------------------- |
| `@echo off` | Disables path and user on echo statements. Cleaner logs! |
| `call <command|script>` | Call a command or script. In error case with stdErr output it the script would not stop. |
| `%<Variable Name>%` | Reference and use a variable. |
| `set <variableName> = value` | Initialize a new variable. |
| `echo.` | Echo a blank line. |
| `REN <comment>` | Comment in batch scripts. |
| `exit 0|1|2` | Exit script with exit code. |
| `%~n0%~x0` | Output of current filename & extension. |
| `:<FunctionName>` | Beginning of a function inside a script. All following indended statements relate to this functions. |
[Steve Jansen - Windows Batch Scripting: Advanced Tricks](http://steve-jansen.github.io/guides/windows-batch-scripting/part-10-advanced-tricks.html)
### If else Statements
https://www.tutorialspoint.com/batch_script/batch_script_if_else_statement.htm
### Check if a batch file ran successfully
You can use
`if errorlevel 1 echo Unsuccessful`
in some cases. This depends on the last command returning a proper exit code. You won't be able to tell that there is anything wrong if your program returns normally even if there was an abnormal condition.
### Exit
exit /B 0
> /B When used in a batch script, this option will exit
only the script (or subroutine) but not CMD.EXE
If executed on the command-line it will close CMD.exe
### Filename of current script
Use the special %0 variable to get the path to the current file.
Write %~n0 to get just the filename without the extension.
Write %~n0%~x0 to get the filename and extension.
Also possible to write %~nx0 to get the filename and extension.
### For Loop
```
for %%h in (%P01HOSTNAME% %P02HOSTNAME% %P03HOSTNAME% %DBHOSTNAME%) do (
echo.
echo === Check SSH Connection to server %%h ===
echo y | %BRANCHROOT%\06.Library\05.3rdPartyTools\plink.exe -l root -i "%PRIVKEY%" %%h "echo Connected to $(hostname) && exit" 2> %errFile%
call :CHECK_FOR_ERRORS
)
```
### For Loop - delete folder with wildcard
`for /d %%G in ("Path\to\Folder_WithWildcard*") do rd /s /q "%%~G"`
### Example with Choices, If, and related functions to be executed
```sh
@echo off
echo.
echo CPDM only:
echo 1) delete all except newest catalogue
echo 2) delete newest catalogue
echo 3) delete all catalogues
echo.
echo ISI only:
echo 4) delete all except newest catalogue
echo 5) delete newest catalogue
echo 6) delete all catalogues
echo.
echo CPDM and ISI:
echo 7) delete all except newest catalogues
echo 8) delete newest catalogues
echo 9) delete all catalogues
echo.
set /p choice="Your Choice: "
IF "%choice%" EQU "1" call :CPDM_DEL_ALL_EXCEPT_NEWEST
IF "%choice%" EQU "7" call :CPDM_DEL_ALL_EXCEPT_NEWEST
IF "%choice%" EQU "2" call :CPDM_DEL_NEWEST
IF "%choice%" EQU "8" call :CPDM_DEL_NEWEST
IF "%choice%" EQU "3" call :CPDM_DEL_ALL
IF "%choice%" EQU "9" call :CPDM_DEL_ALL
IF "%choice%" EQU "4" call :ISI_DEL_ALL_EXCEPT_NEWEST
IF "%choice%" EQU "7" call :ISI_DEL_ALL_EXCEPT_NEWEST
IF "%choice%" EQU "5" call :ISI_DEL_NEWEST
IF "%choice%" EQU "8" call :ISI_DEL_NEWEST
IF "%choice%" EQU "6" call :ISI_DEL_ALL
IF "%choice%" EQU "9" call :ISI_DEL_ALL
pause
exit /b
:CPDM_DEL_ALL_EXCEPT_NEWEST
echo delete from blabla_cfss_cardinalities where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_cfss_templates where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_cfss_type_map where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_combo where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_combo_in_tables where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_combo_out_fields where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_combo_out_structs where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_mapping_rule where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_sois_templates where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_subscription_types where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_mapping_info where version_number != (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
exit /b
:CPDM_DEL_NEWEST
echo delete from blabla_cfss_cardinalities where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_cfss_templates where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_cfss_type_map where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_combo where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_combo_in_tables where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_combo_out_fields where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_combo_out_structs where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_mapping_rule where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_sois_templates where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_subscription_types where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
echo delete from blabla_mapping_info where version_number = (select max(version_number^^^) from blabla_mapping_info^^^); | sqlplus.exe -S cw/cw@orcl
exit /b
```
#
***
Related:
- [[Windows]]