http://www.robvanderwoude.com/batchcommands.php
Checking to see if a var exists
Start command runs in a new window and sort of 'forks' (that is the original window is free to do other stuff while the 'start'ed process is running
start "console name" bash -c "tail -f log.txt"
#File Existance IF EXIST %FILENAME% GOTO gotoTarget IF NOT EXIST %FILENAME% GOTO gotoTarget
Apache log backup to filename appended with date and time and .txt
Usage from cygwin:
cmd logrotate.bat *.log exit
@echo off
SET STOP=T
IF %STOP% EQU T (..\bin\Apache -k stop)
FOR /F "tokens=2-4 delims=/ " %%a IN ('date /t') DO (SET DATE=%%c-%%a-%%b)
REM echo The date is %DATE%
FOR /F "tokens=1-3 delims=: " %%a IN ('time /t') DO IF %%c EQU PM (SET /A HOUR=%%a + 12) ELSE (SET HOUR=%%a)
REM echo The hour is %HOUR%
FOR /F "tokens=1-3 delims=: " %%a IN ('time /t') DO (SET MINUTE=%%b)
REM echo The minute is %MINUTE%
SET PRE=%DATE%-%HOUR%-%MINUTE%-
REM echo The pre is %PRE%
SET SUF=-%DATE%-%HOUR%-%MINUTE%
REM echo The suf is %SUF%
FOR %%A IN (%1) DO (
IF EXIST %%A%SUF%.txt (echo Already Rotated to %%A%SUF%.txt)
IF NOT EXIST %%A%SUF%.txt (
REM echo %%A
REM echo %%A%SUF%.txt
COPY %%A %%A%SUF%.txt
DEL %%A
)
)
IF %STOP% EQU T (..\bin\Apache -k start)
Note: Don't let it recursively affect files
@echo off
FOR %%A IN (%1) DO FOR /F "tokens=1-3 delims=/ " %%a IN ("%%~tA") DO (
REM name to use: %%~nxA-%%c-%%a-%%b.txt
IF NOT EXIST %%c-%%a-%%b-%%~nxA.txt (
REM echo %%A
REM echo %%c-%%a-%%b-%%~nxA.txt
REN %%A %%c-%%a-%%b-%%~nxA.txt
)
)
IF NOT EXIST log.txt GOTO NOFILE del log.txt :NOFILE IF EXIST log.txt GOTO DOIT1 exit 1 :DOIT1 exit 0
SET MYLIST=one two three four FOR %%i IN (%MYLIST%) DO ( echo do one thing to %%i echo do another thing to %%i )
SET MyVAR=fred
echo MyVar contains %MyVAR%
Note: You can type help {commandname} to get help on the commands from a command shell
| Description | Command | Details | Example |
|---|---|---|---|
| Assignment | SET {varname}={value} | SET PROGNAME=Unknown | |
| Comment | :: | Begins a Comment line | :: Comment 1 |
| Compare Strings | if (%var1%) == (%var2%) {?command?}Compare Strings | Works if either is empty string as well | |
if "%var1%" == "%var2%" {?command?} | |||
| File Existance | IF EXIST %FILENAME% | IF EXIST %FILENAME% GOTO ItIs | |
| For Loop | FOR %%i IN (%SET%) DO ( | Loop variable must be 1 letter long | SET MYLIST=one two three four FOR %%i IN (%MYLIST%) DO ( echo do one thing to %%i echo do another thing to %%i ) |
| Label | :{labelname} | :label1 | |
| Shift args | shift | Shifts args %2 becomes %1 and so on |
| %1 | First Parameter |
| %0 | Program name |
| %* | All Parameters (except program name) |
| %~f1 | Expands to fully qualified path name of %1 |
| %~d1 | Expands to the drive number from %1 |
| %~p1 | Expands to the path of %1 |
| %~n1 | Expands to the filename of %1 (without the extension) |
| %~x1 | Expands to the extension of %1 |
| %~s1 | Expands to the short name of %1 |
| %~sn1 | Expands to the filename of the short name of %1 |
| %~sx1 | Expands to the extension of the short name of %1 |