Sunday, 4 February 2018

Java program to rename files inside a directory

package renamefile;

package renamefile;

import java.io.File;

import java.io.IOException;
public class renamefile {

public static void main(String[] argv) throws IOException {
File folder = new File("C:\\Users\\Desktop\\Files\\");

File[] listOfFiles = folder.listFiles(); int count=0;

System.out.println("File Inside the folder are");

for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {

count++;

File f = new File("C:\\Users\\Desktop\\Files\\"+listOfFiles[i].getName());  System.out.println("old name:"+f.getName());

f.renameTo(new File("C:\\Users\\Desktop\\Files\\"new_name"));                    System.out.println("New name:"+f.getName());

}

}

System.out.println("Total number of files renamed are :"+count);
} }

Java Program to count and name files inside a directory

package fileCountAndName;

import java.io.File;

import java.io.IOException;
public class fileCountAndName {

public static void main(String[] argv) throws IOException {
File folder = new File("C:\\Users\\Desktop\\Files\\");

File[] listOfFiles = folder.listFiles(); int count=0;

System.out.println("File Inside the folder are");

for (int i = 0; i < listOfFiles.length; i++)

{
if (listOfFiles[i].isFile())

{

count++;

File f = new File("C:\\Users\\Desktop\\Files\\"+listOfFiles[i].getName()); System.out.println(f.getName());

}

}

System.out.println("Total number of files are :"+count);
}

}

Thursday, 1 February 2018

SHELL SCRIPTING #2 : Shell script Special Variables(create and run script with Special Variables)

Below are some of the Special Variables in Shell scripting:

1] $0 - Display the name of the current script.

2] $1..$n - Display the arguments passed $n referes to the position.
EX: If we pass below arguments:
Shell Script for the world
$1(First arg) - Shell
$2(Second arg) - Script
$3(Third arg) - for
$4(Fourth arg) - the
$5(Fifth arg) - world

3] $# - Total number of arguments passed.

4] $* - Display all the arguments.

5] $@ - Display all the arguments.
Note the difference between $* and $@:
If written without double quotes ("") - Both will simply display the args in single line seperated by space
If $* Written within double quotes("$*") - Takes all the args as a single arg and display in single line
If $@ Written within double quotes("$@") - Display each arg with line break.

6] $? - Exit status of the last command.

7] $$ - Process id of the current shell.



STEP 1: Open a text file, write the below commands and save it as test.sh .

#!/bin/bash
echo "special variables"
echo "current script name : "$0
echo "first argument : "$1
echo "second argument: "$2
echo "total no of arguments Passed : "$#
echo "all the arguments are displayed : "$*
echo "all the arguments are displayed : "$@
echo "the exit status of the last command : "$?
echo "                                  IF EXIT STATUS IS ZERO COMMAND WAS SUCCESS FULL"
echo "                                  IF EXIT STATUS IS ONE COMMAND WAS UNSUCCESS FULL"
echo "process number or id of the currrent shell : "$$

STEP 2:
Copy it to a folder in your unix machine.
Add caption



STEP 3 : Navigate to the folder : cd /apps/test/

STEP 4 : Run with command : ./test.sh
No args are passed.




STEP 5 :
Run command with arguments : ./test.sh shell script for world
Args passed are: shell script for world (see explanation above)

Check out below link for creating and running your first shell script.
https://techeateassy.blogspot.com/2018/01/shell-scripting-1-create-and-run-your.html

Monday, 29 January 2018

SHELL SCRIPTING #1 : Create and run your first .sh file

STEP 1: Open a text file, write the below commands and save it as test.sh .

               #!/bin/bash

               echo "THIS IS MY FIRST SHELL"

STEP 2: Copy it to a folder in your unix machine.
Add caption



STEP 3 : Navigate to the folder : cd /apps/test/


STEP 4 : Run with command : ./test.sh



STEP 5 : Output : THIS IS MY FIRST SHELL





Handaling XLS File In Java

This code will help you to handle xls file in java using apache poi. package test; import java.io.File; import java.io.FileInputStream...