Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

bash script to unzip a file

Posted on 2016-10-18
5
Medium Priority
?
142 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
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

916 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