Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi ,
I need to write a batch script which will unzip the .Zip files from a folder and each .Zip file will have some .csv files inside,I need to unzip all the Zip files and appened the last 3 charcters of the .Zip file to the file name as below.
Zip file name - XYZ_2_21.Zip and inside these zip I have 3 .csv files.
My output should be
firstfile_2_21.csv
secondFile_2_21.csv
thirdfile_2_21.csvHow can I write the batch script to acheive this.
--Sunitha

how are the files named inside the zipfile? (ie what, if any, extension). you would need to put the unzipped files into their own directory unless they have unique extensions.
begin with:
@echo off & setlocal EnableDelayedExpansion
:make/change to temp directory
md temp
copy test.zip temp
cd temp
pkunzip test.zip
:if they always have a 3-byte extension, it's pretty simple:
for %%a in (*.*) do (
:same from here down both versions
set c=%%a
set c=!c:~0,-4!
set c=!c!.csv
ren %%a !c!)if the extensions are unique such that the files won't be mixed up with existing files, you wouldn't need to do any of the directory operations, just change the *.* to the unique extension. for ex. if extension is "txt" AND there are no files with .txt extension in the current working dir.:
@echo off & setlocal EnableDelayedExpansion
pkunzip test.zip
for %%a in (*.txt) do (and the rest would be the same from that point.
if the files inside the zipfile have various lengths of extensions and not always 3 chars, you would need more code to locate the dot then chop it off. without more specfic info, can't tell you much better than this.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |