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.

Pls post a sample input and expected output?

I'm assuming , batch script you mean a shell script?

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

Something like this ?

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 ... )

2 Likes

Awesome. Many thanks ctsgnb. it worked like a charm.

Once Again Many Thanks. Cheers!

@ctsgnb:
n=$(unzip -lqq $i | awk '{print $NF}') could have used basename for this :slight_smile:

@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 . :wink:

1 Like