Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

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

Batch file to delete all files from folder

Below code shows how to delete all files from a folder matching the specific criteria.

FORFILES /p "E:\test\Result" /s /m *.* /D -5 /C "cmd /c del *.* /F /Q"

The above code will delete all files in the folder "E:\test\Result" recursively in the sub folder matching all the files which are older than 5 days.

No pop-up and prompt will be displayed and file will be deleted

Useful Batch file commands

Batch files help user to execute series of commands in the batch file. We can execute large number of commands in one go using bat file.

Some useful batch commands:

@echo off does not echo the text post the command and only displayes the execution result

@echo on the text post the command and only displays the execution result

Pause- the batch file execution is halted until further intervention of user by pressing keys.

CALL - calling one batch file from another batch file

cd -  Change directory

cls - clean the window

copy -copy files from source to destination

DIR - List of directory and files

ERASE - delete files

FC - Compares files
 
IF - Conditional statement

mkdir - Make a new directory/folder

move - move files or folder

ping - Check TCP/IP connection to a remote IP address

rmdir - remove folder/directory

SC - Managing services

set - manipulate environment variable 

taskkill - kills an active process

taslist - List active processes

REM - remark statement
Arguments %1….. – This is the first argument to be provided from the
 command prompt as parameter for the batch command.


Reference: http://www.robvanderwoude.com/batchcommands.php


Starting with batch files:

Let us take a simple example of using batch file to display Create a new file with extension as .bat. Suppose we need to compare two text files. The content of batch file for this will look like


@echo off

Fc %1 %2 /n>%3

Pause


To execute this bat file, we have to write in command prompt  as:

D:\new.bat d:\testsrc.txt d:\testdest.txt d:\testlog.txt