windows batch files

input and if statement in windows batch file

Search for: input and if statement in windows batch file

some documentation on windows batch files from ms

how do you detect empty arguments to windows batch file?

Search for: how do you detect empty arguments to windows batch file?


@echo off


@rem *****************************************************
@rem c:\satya\data      ==>>  ..\data
@rem c:\satya\readonly    ==>>  ..\readonly
@rem c:\work      ==>>  ..\work
@rem 
@rem 
@rem *****************************************************

@rem this is a back up file
@rem source: 
@rem xcopy c:\satya\readonly  ..\readonly /d /s /e /L

if "%1" == "list" goto list
if "%1" == "real" goto real
if "%1" == "" goto nothing
echo list selected

:nothing
echo You have to say list or real
goto done

:list
echo Listing files that have changed
xcopy c:\satya\readonly ..\readonly /d /s /e /L
echo listing files complete.
goto done

:real
echo real selected
I am going to copy the latest files from source to target
goto done

:done
echo done

You intentionally add quote around the input variables. Also notice I didn't use exit. If I did that then it will close the command window.

This script really works well as a poor mans backup facility

you may need to use set command often. here is the doc


@rem ***********************************************
@rem This file is at
@rem 
@rem C:\satya\data\codebase\webapps\akc-release-process\bin
@rem
@rem xcopy .\*.java c:\temp /s /L /D:11-22-2007
@rem ***********************************************

@rem turn off the command display
@rem just look for the output

@echo off

set srcdir=C:\satya\data\codebase\webapps\aspire_jsdk23_eclipse
set targetdir=.
xcopy %srcdir%\*.java %targetdir% /L /s /e /D:04-01-2013

@rem ***********************************************
@rem This file is copy-new-files-akc.cmd
@rem
@rem at
@rem 
@rem C:\satya\data\codebase\webapps\akc-release-process\bin
@rem 
@rem
@rem xcopy .\*.java c:\temp /s /L /D:11-22-2007
@rem ***********************************************

@rem turn off the command display
@rem just look for the output

@echo off

if "%1" == "" goto nothing

set srcdir=C:\satya\data\codebase\webapps\akc-sqlserver
set targetdir=%1

echo %targetdir%

xcopy %srcdir%\*.* %targetdir% /s /D:04-01-2013

goto done

:nothing
echo you have to enter a target sub directory
goto done

:done
echo done

See my notes on options for xcopy and copying recent files

Working with errors in windows batch files

Search for: Working with errors in windows batch files


EQU equal to
NEQ not equal to
LSS less than
LEQ less than or equal to
GTR greater than
GEQ greater than or equal to

if %errorlevel% EQU 0 (
goto xcopy-good
) else (
goto xcopy-bad
)
@rem just in case
goto done

This works


if %errorlevel% EQU 0 (
goto xcopy-good
) 
else (
goto xcopy-bad
)
@rem just in case
goto done

if errorlevel 0 (
goto xcopy-good
) else (
goto xcopy-bad
)
@rem just in case
goto done

what is setx in windows

Search for: what is setx in windows


/m is for system environment
default is local

//Example
setx /M _JAVA_OPTIONS "-Djava.net.preferIPv4Stack=true"

Search for: Edit Environment variables

See panel "System Properties"

Choose "Environment variables" button at the bottom

where you still have to search for "environment" variables

string operations functions in windows batch files

Search for: string operations functions in windows batch files

Find if substring is in a stirng

syntax of if command in windows batch files

Search for: syntax of if command in windows batch files

Block comments in windows dos batch files

Search for: Block comments in windows dos batch files

Apparently this is not there

workaround is to use go to

An article on that


@rem *****************************************************
@rem Something about a batch file
@rem this is a complement
@rem a simple @ at the begining of a line will exec that command silently
@rem *****************************************************

@rem setup a directory for the control file
@set td=C:\satya\data\utils\jl

@rem look for a filename
@set tfile=%td%\transfer-commands.txt

@rem read the file into a variable
@set /p vdir=<%tfile%

@echo Changing directory to %vdir%
@cd %vdir%

wiki windows batch scripting book

windows commands from ss.com

If is documented here

set command is here

Difference between call and start with wait

Difference between call and start with wait

Search for: Difference between call and start with wait

Nice tutorial on batch files

String operations at tut point


@echo off
@rem *****************************************************
@rem Something about a batch file
@rem this is a complement
@rem a simple @ at the begining of a line will exec that command silently
@rem *****************************************************

@rem setup a directory for the control file
@set td=C:\satya\data\utils\jl

@rem look for a filename
@set tfile=%td%\transfer-commands.txt

@rem read the file into a variable
@rem vdir: variable for keeping the directory name
@set /p vdir=<%tfile%

@rem if the directory is "none" then there is Nothing
@rem for this program to change directory to.
if "%vdir%" == "none" goto nothing

@echo Changing directory to %vdir%
@cd %vdir%
goto done

:nothing
@echo No need to change changeDirectory

:done

The colon operator in windows batch files

Search for: The colon operator in windows batch files

SOF link for color operator


set v1="some text"

#rem replace "text" with "string"
set v2=%v1:text=string"

#using a * for any number of chars
set v2=%v1:*text=string"

@rem first 5
set v2=%v1:~0,5%

@rem from 7, next 5
set v2=%v1:~7,5%

@rem skip 7
set v2=%v1:~7"%


@rem first to but last 5
set v2=%v1:~0,-5"

From SS64 partial strings