Thursday, September 22, 2016

Rename Multiple Files with DOS Command RENAME

The Syntax

RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.


Note that you cannot specify a new drive or path for your destination file.

For example,

ren c:\temp\test.txt abc.txt
ren *.txt abc.txt

The first parameter is the file selection criteria. It selects all files that match this criteria, then renames the files based on the second parameter.

The selected files are renamed one at a time, with character by character mode. As it works through the files, it stops as soon as an error is encountered, and the rests of the files will be left untouched.

Wildcards

Wildcards * and ? can be used with REN for matching and renaming.

The * wildcard will match any sequence of characters
               (0 or more, including NULL characters)

The ? wildcard will match a single character
               (or a NULL at the end of a filename)


Consider Renaming the Following Files

X-test-01.txt
Y-test-02.txt
Z-test-03.txt

Some middle characters are fixed throughout the files while they varies at the beginning and the ending part of the names.


Renaming Middle Part, Same Number of Characters

Renaming the middle part is straightforward when the targeted result has the same number of characters.

For example,

ren ?-test-0?.txt ?-good-0?.txt
ren ??test???.txt ??good???.txt
ren ??test???.txt ??good*

These rename middle part "test" to "good". i.e. they become

X-good-01.txt
Y-good-02.txt
Z-good-03.txt


Renaming Middle Part, Different Number of Characters

It is a bit tricky if you want to rename the middle part but with different number of characters. This cannot be done with a single REN command, because REN works in character by character mode.

If you try these,

ren ??test???.txt ??goodjobs???.txt

"job" will not be inserted, but copied over character by character. The result will be,

X-goodjobs.txt
Y-goodjobs.txt
Z-goodjobs.txt

Insertion will work only if the characters immediately after the part you intend to change is fixed across all files.

For example, you can rename the following files,

X-test-a.txt
Y-test-a.txt
Z-test-a.txt

to

X-goodjob-a.txt
Y-goodjob-a.txt
Z-goodjob-a.txt

Using the following command:

ren ??test??.txt ??goodjob-a.txt

Returning to the original set of files, the following batch script works:



for %%i in (??test???.txt) do (set fname=%%i) & call:rename
goto finish

:rename
ren %fname% %fname:~0,2%goodjob%fname:~-7%

goto :eof

:finish



%fname:~0,2% selects the first two characters from the filename.
%fname:~-7% selects the last seven characters from the filename.

Note:
Use LFNFOR to turn on long file name support when processing for command.

Tuesday, September 20, 2016

Recreate TEMP Tablespace

Step 1: Create another temporary tablespace, TEMP2

create temporary tablespace TEMP2
tempfile 'd:\oracle\oradata\sid\TEMP02.DBF' size 100m
extent management local uniform size 100m;

alter database tempfile 'd:\oracle\oradata\sid\TEMP02.DBF' autoextend on next 100m maxsize unlimited;

Step 2: Set it as default temporary tablespace

alter database default temporary tablespace TEMP2;

Step 3: Make sure No sessions are using your Old Temp tablespace

Skip this step if you are recreating temp tablespace without any user accessing the db.

Find Session Number from V$SORT_USAGE:

SELECT USERNAME, SESSION_NUM, SESSION_ADDR FROM V$SORT_USAGE;

Find Session ID from V$SESSION:

If the resultset contains any tows then your next step will be to find the SID from the V$SESSION view. You can find session id by using SESSION_NUM or SESSION_ADDR from previous resultset.

SELECT SID, SERIAL#, STATUS FROM V$SESSION WHERE SERIAL#=SESSION_NUM;
OR
SELECT SID, SERIAL#, STATUS FROM V$SESSION WHERE SADDR=SESSION_ADDR;

Kill Session:

Now kill the session with IMMEDIATE.

ALTER SYSTEM KILL 'SID,SERIAL#' IMMEDIATE;

Step 4: Drop Old TEMP tablespace

drop tablespace TEMP including contents and datafiles;

Step 5: Recreate TEMP tablespace

create temporary tablespace TEMP
tempfile 'd:\oracle\oradata\sid\TEMP02.DBF' size 512m
extent management local uniform size 100m;

alter database  tempfile 'd:\oracle\oradata\sid\TEMP01.DBF' autoextend on next 100m maxsize unlimited;

Step 6: Set TEMP as the default Temporary Tablespace

alter database default temporary tablespace TEMP;

Step 7: Drop TEMP2

drop tablespace TEMP2 including contents and datafiles;


ORA-25152: TEMPFILE cannot be dropped at this time

The temporary tablespace can have more than one tempfile. You can drop the tempfile by running the following command:

Advice: You should do this when no user is using the database.

Oracle 9i R2 and above:

alter database tempfile 'd:\oracle\oradata\sid\temp04.dbf' drop including datafiles;

Oracle 10g R2 and above:

alter tablespace temp drop tempfile 'd:\oracle\oradata\sid\temp04.dbf';

The commands above are usually successful if there is no active session using the tempfile. The tempfile do not need to be in OFFLINE status in order for it to be dropped. However, you can bring it OFFLINE first before dropping with the following command:

alter database tempfile 'd:\oracle\oradata\sid\temp04.dbf' offline;

A tempfile cannot be dropped is usually caused by active sessions.

If you receive error such as below, try to kill the active session:

ORA-25152: TEMPFILE cannot be dropped at this time

Find out the active session:

SQL> SELECT s.sid, s.username, s.status, u.tablespace, u.segfile#, u.contents, u.extents, u.blocks
  2  FROM v$session s, v$sort_usage u
  3  WHERE s.saddr=u.session_addr
  4  ORDER BY u.tablespace, u.segfile#, u.segblk#, u.blocks;

       SID USERNAME   STATUS   TABLESPACE  SEGFILE#  CONTENTS  EXTENTS BLOCKS
---------- ---------- -------  ----------- --------- --------  ------- ------
       213 DBUSER1    INACTIVE TEMP        203       TEMPORARY 5279    675712


SQL> select sid,serial# from v$session where sid = 213;

       SID    SERIAL#
---------- ----------

       213      48649

Kill the session:

SQL> alter system kill session '213,48649';

System altered.

Retry drop the tempfile:

SQL> alter database tempfile 'd:\oracle\oradata\oradb\temp04.dbf' drop including datafiles;


Database altered.

If the command above is successful, but the physical tempfile is not deleted. You might have to restart the database and delete the physical file at OS level.