Solved

bash script to unzip a file

Posted on 2016-10-18
5
34 Views
1 Endorsement
Last Modified: 2016-10-23
i need a script to monitor whether any files are available in the folder  (with specific name)  and if its available then i need to unzip the file and rename it (specific name)
The input files are placed to folder in :"D" drive. Then this files will be unzipped manually and it will have a somename.txt & we rename this .txt file to application specific name.txt. This files will be then picked by the batch job and processed accordingly. I need a script to run once day (via task scheduler) and unzip a files and rename the .txt files in folder in D drive.

File Name:      FINS.txt and FINSP.txt respectively
Rename File to:      OFAC.txt and OFAC_provider.txt respectively

This files are picked by the batch job and its archived by the batch job itself. So we just need to unzip a file and rename it.
1
Comment
Question by:vishnu adithya
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:Marius-Constantin Postolache
ID: 41849031
Hello,

Please take a look at this function:

:: Unzip a file
:unZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs%  echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%


I am calling it this way:

call :log "DEBUG" "Extracting the archive..."
call :unZipFile "%~dp0" "%~dp0files.zip"
call :log "DEBUG" "The archive was successfully extracted"


Good luck!
0
 

Author Comment

by:vishnu adithya
ID: 41849541
what about renaming the extracted files to a specific name ? i'm new to batch scripting, so can you explain the above code
0
 
LVL 1

Accepted Solution

by:
Marius-Constantin Postolache earned 500 total points
ID: 41849589
Hello,

Maybe this is more clear:




@echo off
cls

rem global variable, used in the logs:
set dateTime=''

rem parameters:
set directoryName=%1

rem *****************
rem ** Main script **
rem *****************

rem list all the file from a directory
for /r %%i in ("%directoryName%\*") do (
      call :log "DEBUG" "File %%i"
      
      rem unzip the .zip files
      for %%a in ("%%i") do (
            if "%%~xa"==".zip" (
                  call :log "DEBUG" "Extracting %%i ..."
                  call :unZipFile "%~dp0" "%%i"
            )
      )
)


rem *************************************
rem ** the functions are defined below **
rem *************************************

:: Set the dateTime value
:setDateTime

set year=%date:~-4%

set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%

set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%

set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%

set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%

set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%

set msecs=%time:~9,2%
if "%msecs:~0,1%" == " " set msecs=0%secs:~1,1%

set datetime=%year%-%month%-%day% %hour%:%min%:%secs%,%msecs%

exit /b 0


:: Log messages to stout
:log

call :setDateTime
echo. %datetime% - %~1 - %~2

exit /b 0


:: Unzip a file
:unZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs%  echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%

exit /b 0



Put it in a .bat file and run it like this:

script.bat  nameOfYourDirectory



With thre renaming problem I do not understand exactly what you want, but you should use the ren function.

Good luck!
0
 

Author Comment

by:vishnu adithya
ID: 41851068
after extracting the zipped, you will couple txt files. we have rename those file to a specific file name e.g
File Name:      FINS.txt and FINSP.txt respectively
Rename File to:      OFAC.txt and OFAC_provider.txt respectively
0
 
LVL 23

Expert Comment

by:NVIT
ID: 41851077
Add this to end of Marius' code, above the exit /b 0 line:
ren FINS.txt FINSP.txt
ren OFAC.txt OFAC_provider.txt

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now