windows batch files
satya - 3/10/2013 6:32:00 PM
input and if statement in windows batch file
input and if statement in windows batch file
satya - 3/10/2013 6:39:47 PM
some documentation on windows batch files from ms
satya - 3/10/2013 6:42:53 PM
how do you detect empty arguments to windows batch file?
how do you detect empty arguments to windows batch file?
Search for: how do you detect empty arguments to windows batch file?
satya - 3/10/2013 6:52:43 PM
Here is an example
@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
satya - 3/10/2013 6:53:55 PM
Notice how the empty string and other strings are tested
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.
satya - 3/10/2013 6:58:09 PM
This script really works well as a poor mans backup facility
This script really works well as a poor mans backup facility
satya - 3/10/2013 7:28:29 PM
you may need to use set command often. here is the doc
satya - 5/31/2013 12:12:01 PM
Another example
@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
satya - 5/31/2013 3:47:06 PM
Another example
@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
satya - 5/3/2014 12:26:37 PM
See my notes on options for xcopy and copying recent files
satya - 8/10/2016, 9:35:16 AM
Working with errors in windows batch files
Working with errors in windows batch files
satya - 8/10/2016, 9:40:05 AM
if operators
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
satya - 8/10/2016, 9:58:49 AM
else is very tricky and so is checking errorlevel
if %errorlevel% EQU 0 (
goto xcopy-good
) else (
goto xcopy-bad
)
@rem just in case
goto done
This works
satya - 8/10/2016, 9:59:41 AM
And this doesn't work
if %errorlevel% EQU 0 (
goto xcopy-good
)
else (
goto xcopy-bad
)
@rem just in case
goto done
satya - 8/10/2016, 10:12:53 AM
This should work as well but is not!
if errorlevel 0 (
goto xcopy-good
) else (
goto xcopy-bad
)
@rem just in case
goto done
satya - 9/1/2019, 4:35:59 PM
what is setx in windows
what is setx in windows
satya - 9/1/2019, 4:41:37 PM
Set an environment variable from a command line for a user or a system
/m is for system environment
default is local
//Example
setx /M _JAVA_OPTIONS "-Djava.net.preferIPv4Stack=true"
satya - 9/1/2019, 4:45:23 PM
How can I see what are the system environment variable
Search for: Edit Environment variables
See panel "System Properties"
Choose "Environment variables" button at the bottom
satya - 9/1/2019, 4:48:48 PM
It appears there is an app "Settings"
where you still have to search for "environment" variables
satya - 9/1/2019, 5:51:36 PM
string operations functions in windows batch files
string operations functions in windows batch files
Search for: string operations functions in windows batch files
satya - 9/2/2019, 12:14:12 PM
syntax of if command in windows batch files
syntax of if command in windows batch files
satya - 5/6/2020, 10:03:54 AM
Block comments in windows dos batch files
Block comments in windows dos batch files
satya - 5/6/2020, 10:04:07 AM
Apparently this is not there
Apparently this is not there
satya - 5/6/2020, 10:05:08 AM
workaround is to use go to
workaround is to use go to
satya - 6/27/2020, 9:55:38 PM
An example to read a file into a variable
@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%
satya - 6/29/2020, 12:22:47 PM
Difference between call and start with wait
satya - 6/29/2020, 12:22:59 PM
Difference between call and start with wait
Difference between call and start with wait
satya - 7/2/2020, 10:41:07 AM
Another sample batch file
@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
satya - 7/2/2020, 11:03:23 AM
The colon operator in windows batch files
The colon operator in windows batch files
satya - 7/2/2020, 11:13:32 AM
Briefly
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"