Skip to content

Latest commit

 

History

History
128 lines (104 loc) · 3.73 KB

dos.adoc

File metadata and controls

128 lines (104 loc) · 3.73 KB

DOS DOS

Commands

command description

@

Does not echo back the text after the @ symbol. often used as @ECHO OFF to prevent any of the commands in the batch file from being displayed.

%*

for everything

%0

the command used to call the batch file (could be foo, ..\foo, c:\bats\foo, etc.)

%1

is the first command line parameter,

…​

%9

is the 9th command line.

::

One of two ways of adding remarks into the batch

:LABEL

A label allows you to skip to certain sections of a batch file, see GOTO

GOTO LB

Used to go to a certain label (LB).

CALL

A call is used to run another batch file within a batch file.

START

Used for Windows 95 and later to start a Windows application

ECHO

Will echo a message in the batch file.

Paths

from command line arguments

command description

%~nx0

the actual name of the batch file, regardless of calling method (some-batch.bat)

%~dp0

drive and path to the script (d:\scripts)

%~dpnx0

is the fully qualified path name of the script (d:\scripts\some-batch.bat)

filename without extension

  %var:~0,-4% // (1)
  1. the -4 means that the last 4 digits (presumably .ext) will be truncated

Execute the script in its actual path

@echo off
SET me=%~dp0
pushd
cd /d %me%
echo %cd% -- %me% // (1)
popd
  1. %cd% equals %me%

Error handling

if ERROR (
  ECHO this is the error.
  GOTO:error
)
REM DO YOUR JOB HERE
REM ... till the end
GOTO:EOF

:error
ECHO.
ECHO Press any key to exit.
PAUSE >nul
GOTO:EOF

Check a command is available

Checks if the command exists before I run it. i.e. if the command is reachable in PATH.

WHERE mycommand
IF %ERRORLEVEL% NEQ 0 ECHO mycommand wasn't found

Check for administrator privileges

@echo off
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    REM Display the error and quit
) else (
    REM Do whatever needs admin rigth
)

Loop over files

FORFILES /S /M *.doc /C "cmd /c echo @fname : @fsize"
Keyword Description

@FILE

filename.

@FNAME

filename wo extension.

@EXT

extension.

@PATH

absolute path.

@RELPATH

relative path.

@ISDIR

TRUE if it is a directory, FALSE otherwise.

@FSIZE

File size in bytes.

@FDATE

Last modification date.

@FTIME

Last modification time.

Mapped drives reconnection error (at startup)

symptoms

At startup, windows shows error "Failed to reconnect network drives.". But once you click on the drive, it become working.

solution:

A: Not proven: ensure all devices share the same network name (eg: WORKGROUP) B: Not working: Enable Linked Connections in registry. Create a reg file (eg. nameg nas.reg), copy-paste the following and execute.

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
"EnableLinkedConnections"=dword:00000001