Solved

bash script to unzip a file

Posted on 2016-10-18
5
44 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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 …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 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

20 Experts available now in Live!

Get 1:1 Help Now