Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

bash script to unzip a file

Posted on 2016-10-18
5
Medium Priority
?
128 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:V Thrusher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:V Thrusher
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 2000 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:V Thrusher
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 25

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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