Batch Script To Unzip and Rename File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Batch Script To Unzip and Rename File
# 1  
Old 03-15-2013
Batch Script To Unzip and Rename File

Hello All,

I need help in writing a batch script.

I have 100 zip files in a folder. Each zip file has a unique name starting with XYZ_12345.zip Each zip file contains single csv file

I would like to batch extract the files and also rename the extracted csv as per the original zip name (only the text name as in XYZ and not the XYZ_12345)

Can someone please help with this script. I would be extremely grateful for the help.

Many thanks in anticipation.
# 2  
Old 03-15-2013
Pls post a sample input and expected output?

I'm assuming , batch script you mean a shell script?
# 3  
Old 03-15-2013
Hi Panyam

Thanks. Yes I meant Shell Script
Input file example would be NAME_BATCHNO.zip
Output that I need after extraction should be just the NAME from the zip file

Thanks again
# 4  
Old 03-15-2013
Something like this ?

Code:
for i in *.zip
do 
n=$(unzip -lqq $i | awk '{print $NF}')
e=${n#*.}
unzip $i && mv $n ${i%%_*}".$e"
#rm $i
done

(Uncomment the rm when you've make sure it behaves the way you need ... )
These 2 Users Gave Thanks to ctsgnb For This Post:
# 5  
Old 03-15-2013
Awesome. Many thanks ctsgnb. it worked like a charm.

Once Again Many Thanks. Cheers!
# 6  
Old 03-15-2013
@ctsgnb:
n=$(unzip -lqq $i | awk '{print $NF}') could have used basename for this Smilie
# 7  
Old 03-15-2013
Quote:
Originally Posted by PikK45
@ctsgnb:
n=$(unzip -lqq $i | awk '{print $NF}') could have used basename for this Smilie
@Pikk45
Wrong : basename would have given the name of the zip file, and not the name of the csv file that is in the zip archive . Smilie
This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

2. Shell Programming and Scripting

Unzip the .zip file without using unzip utility in UNIX

I have .zip file, i want to list all the files archived in the zip file. unzip utility is not working for me in unix. Please help me resolve this issue Thanks ganesh. (3 Replies)
Discussion started by: Ganesh L
3 Replies

3. Shell Programming and Scripting

Batch rename files that do not match pattern

Hi all, I do not have programming experience and mostly use one-liner or sometimes more to get the job done. I am having problem to batch rename the files that do not match a particular pattern. Sample file-names from directory: Meeting_Packages.pdf 13_textfile0 19_textfile0 ... (3 Replies)
Discussion started by: atulkakrana
3 Replies

4. Shell Programming and Scripting

try to batch rename using sed (if this is best)

hi gooday I need some help with a rename I am attempting. I'd like to rename a bunch of files in a folder example list.dat.old to list_N.dat query.dat.old to query_N.dat note the two periods in (.dat.old) to become _N.dat I tried using sed like this ls *.dat.old | sed... (3 Replies)
Discussion started by: johnstrong
3 Replies

5. Shell Programming and Scripting

Batch rename files after internal search pattern

Hello all I am trying to do a script that would allow me to mass rename files after a grep search within them. They are XML files and the pattern I would like to append to the file name is easy to find (e.g. <filename>foo</filename>), but I can't for the life of me find out how to do it and... (2 Replies)
Discussion started by: aavv
2 Replies

6. UNIX for Dummies Questions & Answers

Batch rename recursively

I would like to replace multiple underscores with hyphens but I have 26,000 files to rename. They are all in one file structure and multiple sub-directories. It would be much simpler if I had a script to do it. Here are some samples of the file names: Example 1... (3 Replies)
Discussion started by: ..Chris..
3 Replies

7. Shell Programming and Scripting

How to Unzip a .ZIP file in Unix without using unzip cmd..?????

Hi All I have ftped a .ZIP file (zipped using WinZip in Windows) to my Unix server (HP-UX). I don't have unzip cmd available in my curent Unix version Please let me know any cmd in UNIX (other than unzip) using which I can unzip this .ZIP file . Please elaborate on the commands aval and... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

8. UNIX for Advanced & Expert Users

rename the file in batch

In my dir there are files like a.xml b.xml abnc.xml 12.abc.xml 12.anc.sfoioi.xml I need to remove .xml from all the files Is there any direct way without using any for/do loop Right now i am using for file in * do ... (7 Replies)
Discussion started by: reldb
7 Replies

9. HP-UX

How to Unzip a .ZIP file in Unix without using unzip cmd..?????

Hi All I have ftped a .ZIP file (zipped using WinZip in Windows) to my Unix server (HP-UX). I don't have unzip cmd available in my curent Unix version Please let me know any cmd in UNIX (other than unzip) using which I can unzip this .ZIP file . Please elaborate on the commands aval and... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

10. Shell Programming and Scripting

rename in batch

example test1 will have m1234567.12a I would like to rename in batch but I don't Please help me on this. cd /a1/a2/a3 test1=$(basename /a1/a2/a3/*.*) >> /tmp/t echo $test1 echo "Extracting 8 th position" >> /tmp/t2 awk '{print substr($1,8,1); }' $test1 >> /tmp/t3 echo "extraction ... (3 Replies)
Discussion started by: kathy18
3 Replies
Login or Register to Ask a Question