Showing posts with label Batch Programming. Show all posts
Showing posts with label Batch Programming. Show all posts

Thursday, May 30, 2013

About autorun file on removable medias

Whenever one insert any media like CD in his computer, it senses the new CD and looks for a file in root of CD called autorun.inf. This is the first file gets excuted on insertion of any CD. It contains the command to be run for autoplay, and the icon to be shown while veiwing on file explorer.

It's a plain text file, which can be edited using any text editor, like notepad. It basically contains key=value pair for commands. Sample of autorun.inf file:

[autorun]
open=setup.exe
icon=setup.ico

some other commands which can be written on autorun.inf file
shellexecute=[filepath\]filename
UseAutoPlay=1

This setup.exe and setup.ico should be on root directory of CD.
To test the autorun created by you, refer to Disabling autorun autoplay, to enable autorun for any particular type of drive, for example you can enable for FIXED drives and then test on your C/D/E drives.

For more information on creation of autorun file please refer to wiki page.

Wednesday, March 30, 2011

LS: unix command for listing files and directories

ls is a command to list files and directories in Unix and unix like systems.

if it is invoked without any arguments then will list files and folders in current working directory in bare format. In this bare format its difficult to distinguish files and folders and other parameters.
FORMAT: ls [Options] [Filename]
Following are the most common options to be used with "ls".
-a :  Include all files even which are starting with "."(Hidden files in Unix ), by default they are ignored.
-l  : Long format, lists permissions in unix format, size, owner , date modified etc.
-F : Appends a character to reveal the type of file e.g. * for executables '/' for directory
-R : list recursively all the directories and files
-d : list directories instead of content
-t : sort by modification time
-h : Print size in human readable format like 1M, 4G ( Use with -l)

Example: 
For listing files and directories with their parameters like size permissions and all.
> ls -l 
For listing in as ls -l along with size in human readable format..
>ls -h -l

Note: ls can also be used in windows environment with cygwin installation.

CACLS command for windows changing mode permission


It is a command line utility which displays and modify Access Control Lists (ACLs) in windows environment(Microsoft windows NT) of files and folders. An Access Control List is a list of permissions for securable object, such as a file or folder, that controls who can access it.
 
XCACLS.exe : It's a updated version for cacls for windows 2000 and later.
 
CACLS filename [/T] [/E] [/C] [/G user:perm] [/P user:perm [...]] [/D user [...]]
 
Options:
filename: Name of the file to be modify ( * wildcard for all files )
/T            Changes ACLs of specified files in the current directory and all subdirectories.
/E            Edit ACL instead of replacing it.
/C            Continue on access denied errors.
/G user:perm  Grant specified user access rights.
/P user:perm  Replace specified user's access rights.
/D user       Deny specified user access.--
 
Perm can be:
N  None
R  Read
W  Write
C  Change (write)
F  Full control
 
Examples:
1: to replace ACL with Full access permission to current directory and all subdirectories for user "test"
>cacls * /T /P test:F
2. To edit ACL for particular user "test" with Change access for abc.text file without changing other permission.
> cacls abc.text /E test:C
 
Also look for chmod a linux command for changing file permissions.
 

Wednesday, June 9, 2010

How to remove autorun virus from Hard disk or How to open HDD by double click

Some time you are not able to open your hard disk drives(HDD) by double clicking, so to enable that please follow following procedures:
> open command prompt
> type and enter "cd\", this will take you to the root of C: Drive
> type "attrib -h -r -s autorun.inf" and enter
> now type and enter "del autorun.inf" to delete autrun.inf file
> Similarly follow same steps for all the drives and then restart your computer
> Same steps can also be followed for pendrives or removable media.

> Now just enjoy the freedom of double clicking.

Wednesday, February 24, 2010

Commands for Command prompt

Type following commands on command window to perform the required task.
By typing "help" on command prompt you can get commands for most common tasks e.g. cd, dir, del, exit, copy etc.
Here I will write down some uncommon commands which will be helpful when working on windows XP using command prompt.

  • hostname: Gives hostname of the machine you are using
  • attrib : Shows or updates Attributes of a file, -h removes hidden attrib from file, while +h adds hidden attrib
  • cacls : Changes the ACLS (security settings) for files and folders, similar to chmod in linux
  • control : Unpublished, use "control userpasswords2"findstr: To find strings, similar to grep in linux
  • gpresult : gets the summary of group policy settings and user settings
  • ipconfig : Shows IP Configuration for your system, use "ipconfig -all " for all settings, "ipconfig /release" for releasing Ip Addresses, "ipconfig /renew" for renewing IP address
  • openfiles : Enables administrator to display a list of open files and folders that have been opened 
  •  reg : A console registry tool to edit registry settings
  • systeminfo : displays system information
  • tasklist: list of all running tasks
  • taskkill: to kill running task
  • taskmgr: To open Task manager from command window
  • tree : Really amazing utility, gives the list of files and folders in tree form , should try at least once.
  • shutdown: Will show help for shutting down the windows XP. This will be helpful when you want to shutdown or restart remote computer.
             shutdown -s : shutdown this computer
             shutdown -r : Restart this computer
             shutdown -m \\computername : Remote computer to shutdown/restart/logoff
             shutdown -i : GUI interface for shutdown

Check this lonk for more detailed info and all other available commands:
Microsoft windows command line tools

Monday, February 8, 2010

Environmental Variables

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer. these have been used for long in operating systems and are present in Unix, DOS and Windows environment.

Microsoft's definitions:

Environment variables are strings that contain information such as drive, path, or file name. They control the behavior of various programs. For example, the TEMP environment variable specifies the location in which programs place temporary files.

 Examples of environment variables include:
  • PATH - lists directories the shell searches, for the commands the user may type without having to provide the full path.
  • HOME (Unix-like) and userprofile (Microsoft Windows) - indicate where a user's home directory is located in the file system.
Shell scripts and batch files use environment variables to communicate data and preferences to child processes. They can also be used to store temporary values for reference later in the script, although in Unix other variables are usually used for this.

Examples of DOS Env Variables:
%ERRORLEVEL% : This variable points to the current error level. If there was an error in the previous command, this is what you need to check against to find out about that.
%PATH% : This variable contains a semicolon-delimited list of directories in which the command interpreter will search for executable files. Equivalent to the Unix $PATH variable (although note that PATH on Windows additionally performs the same task as LD_LIBRARY_PATH on Unix-like systems). Note that %PATH% can also be set like this PATH=c:\dos; where SET isn't required.
%TEMP% and %TMP% : These variables contain the path to the directory where temporary files should be stored.
Examples of UNIX ENV Variables:
$PATH : Contains a colon-separated list of directories that the shell searches for commands that do not contain a slash in their name (commands with slashes are interpreted as file names to execute, and the shell attempts to execute the files directly).
$HOME : Contains the location of the user's home directory. Although the current user's home directory can also be found out through the C functions getpwuid and getuid, $HOME is often used for convenience in various shell scripts (and other contexts). Using the environment variable also gives the user the possibility to point to an other directory.
$PWD : This variable points to the current directory. Equivalent to the output of the command pwd when called without arguments.
$TZ : Refers to Time zone. It can be in several formats, either specifying the timezone itself or referencing a file (in /usr/share/zoneinfo).
Example from Microsoft Windows: Case Insensitive
%CD% : This variable points to the current directory. Equivalent to the output of the command cd when called without arguments.
%DATE% : This variable expands to the current date. The date is displayed according to the current user's date format preferences. For more info on how to set date variable refer %DATE%
 
%RANDOM% : This variable returns a random number between 0 and 32767
%TIME% : This variable points to the current time. The time is displayed according to the current user's time format preferences.
 
Reference: 
Default values of Environment Variable on Microsoft windows
Further Related Study:
List of UNIX Utilities
 

What is difference between %cd% and %~dp0

%cd% is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory (which can change by using the CD command)

%~dp0 is only available within a batch file and expands to the drive letter and path in which that batch file is located (which cannot change). It is obtained from %0 which is the batch file's name.

An experiment like the following shows the difference

Here is E:\dirpath.bat:

echo this is %%cd%%  %cd%
echo this is %%~dp0 %~dp0

Run it from C:\ and this is what you see

Code:
C:\>E:\dirpath.bat
this is %cd%  C:\
this is %~dp0 E:\

Thursday, November 26, 2009

Batch Command: Subst

SUBST:

Syntax:
subst d: d:path
subst d: /D

Purpose: Substitutes a virtual drive letter for path designation.
Usage:
subst d: e:\system\path ; will map e:\system\path to virtual drive d:
subst d: /D ; will delete the mapped virtual drive d:
subst    ; will show available virtual mapped drives.

Monday, August 24, 2009

using 2>&1

It's basically shell command for redirecting the standard error.
1. cat food 2>&1 >file
cat: can't open food
2. cat food >file 2>&1
In first command shell sees 2>&1 first and redirects the standard error(file descriptor 2)
to same place as standard outpur(file descriptor 1) goes ie STDOUT, while in second command
standard output goes to "file" so Standard Error(2) will also be redirected to same loaction
i.e.  to the "file"

Monday, March 16, 2009

Information on %~dp0

This macro is used to get the path of batch file.

It doesn't matter from where you are calling batch file it will return the path where the batch file is stored.

So if you put the following in a batch file, no matter what directory you are in when you run it, it will print the directory the batch file is in, and do a directory listing.

e.g. :
echo Batch file path is %~dp0
dir %~dp0

Tips on Creating Batch Files

The PATH variable can be set in two ways:

SET PATH=c:\bat;c:\dos

or:

PATH c:\bat;c:\dos

In the first case, using SET, the PATH variable is set to c:\bat;c:\dos, in lower case, as specified.
In the second case, using PATH, the PATH variable is set to C:\BAT;C:\DOS, in upper case, no matter how it was specified.


DIFFERENCE BETWEEN %cd% and %~dp0

%cd% is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory (which can change e.g. by using the CD command)

%~dp0 is only available within a batch file and expands to the drive letter and path in which that batch file is located (which cannot change). It is obtained from %0 which is the batch file's name.

An experiment like the following shows the difference

Here is D:\dirshow.bat:

Code:
@echo off
echo this is %%cd%% %cd%
echo this is %%~dp0 %~dp0

Run it from C:\ and this is what you see

Code:
C:\>D:\dirshow.bat
this is %cd% C:\
this is %~dp0 D:\