Solved

bash script to unzip a file

Posted on 2016-10-18
5
106 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 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: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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
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…

622 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