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

x
?
Solved

Vb script to unzip a files and rename the files

Posted on 2016-09-22
12
Medium Priority
?
217 Views
Last Modified: 2016-10-05
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)
0
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
  • 7
  • 5
12 Comments
 
LVL 36

Expert Comment

by:Kimputer
ID: 41810958
VBscript usually doesn't monitor anything. You just run it (usually with Task Scheduler).
Please provide more info on the file names? What to do after it's done? Delete the file? (Otherwise, next run it will be unzipped again). Rename how?
You can also start by installing 7 zip and let me know the installation folder.
Then give more info on the unzipping logic.
0
 

Author Comment

by:V Thrusher
ID: 41812517
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 vb script to run once day (via task scheduler) and unzip a files and rename the .txt files in folder in D drive.
Thanks for your response Kim!!
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 41812566
Your response is probably not complete. You didn't tell me if I should delete the zip file or not (because otherwise the next run it will be processed again.
Also, your description about the renaming is not complete. If you don't tell me enough details, I can't code what you need.

Here's the simple version. Unzip files found in src_folder. Then DELETE the zip file. Then fixed rename.
So it expects ONE text file, to rename to another txt file (since you didn't provide enough information).
Adjust the variables on top of the script and you're good to go:

'variables to edit
src_folder = "D:\temp\"
ext = ".7z"
Sevenzip = "C:\Program Files\7-Zip\7z.exe"
src_file =  src_folder & "abc.txt"
dst_file =  src_folder & "abcd.txt"

'no need to edit under here
Set fso=wscript.CreateObject("Scripting.FileSystemObject")
Set r=fso.GetFolder(src_folder)


For each f in r.Files

	If (instr(f,ext) > 0) then
		Set objShell = CreateObject("WScript.Shell")
				objShell.Run """" & Sevenzip & """ e " & f,0,True
		Set objShell = Nothing
		f.Delete
	end if

Next 'For each f in r.Files

Set r=fso.GetFolder(src_folder)

For each f in r.Files

	If (instr(f,src_file) > 0) then
		f.Move dst_file
	end if

Next 'For each f in r.Files

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:V Thrusher
ID: 41815539
okay got it ...sorry Kim....its like one text file is renamed to another text file.
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.
0
 
LVL 36

Accepted Solution

by:
Kimputer earned 2000 total points
ID: 41815772
Recap:
find all files with the correct extension in the correct folder, unzip, rename only if fins.txt or finsp.txt
WILL OVERWRITE EVERYTHING to keep the job going (ex. if fins.txt or ofac.txt still exists from earlier run). Will delete original zip.


'variables to edit
src_folder = "D:\temp\"
ext = ".7z"
Sevenzip = "C:\Program Files\7-Zip\7z.exe"
src_file1 =  src_folder & "fins.txt" 'lcase!!
dst_file1 =  src_folder & "OFAC.txt"
src_file2 =  src_folder & "finsp.txt" 'lcase!!
dst_file2 =  src_folder & "OFAC_provider.txt"

'no need to edit under here
Set fso=wscript.CreateObject("Scripting.FileSystemObject")
Set r=fso.GetFolder(src_folder)


For each f in r.Files

	If (instr(f,ext) > 0) then
		Set objShell = CreateObject("WScript.Shell")
				objShell.Run """" & Sevenzip & """ e " & f & " -aoa" ,0,True
		Set objShell = Nothing
		f.Delete
	end if

Next 'For each f in r.Files

Set r=fso.GetFolder(src_folder)

For each f in r.Files

	If (instr(lcase(f),lcase(src_file1)) > 0) then
		if fso.fileexists(dst_file1) then
			fso.DeleteFile dst_file1
		end if
		f.Move dst_file1
	end if
	If (instr(lcase(f),lcase(src_file2)) > 0) then
		if fso.fileexists(dst_file2) then
			fso.DeleteFile dst_file2
		end if
		f.Move dst_file2
	end if
	
Next 'For each f in r.Files

Open in new window

0
 

Author Comment

by:V Thrusher
ID: 41827921
Sorry for the delay in response, i was out of town. Thanks Kim for the code...i will check it and we can close the questions by tomorrow.

Also can you pls help me in other question : https://www.experts-exchange.com/questions/28972917/Vb-script-to-restart-the-services.html
0
 

Author Comment

by:V Thrusher
ID: 41827935
It seems the unzipped file and zip file will be moved to archive path by batch job...so we don't have to delete any file...can you please tell me what are lines i should comment in above mention code ...to remove delete option
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 41827967
The zip is deleted on line 21
0
 

Author Comment

by:V Thrusher
ID: 41829852
what about line number 32 and 38 ? is it for existing .txt file ?
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 41829972
Correct, your directive was to rename the unzipped "fins.txt" to "OFAC.txt"
Therefore I delete an existing OFAC.txt first (otherwise the rename will fail, and I want you to have the newest file). And the other line is for the other rename.
0
 

Author Comment

by:V Thrusher
ID: 41830777
Thanks lot Kim, you are the best !!! It will really helpful if you can take a look at my other question. https://www.experts-exchange.com/questions/28972917/Vb-script-to-restart-the-services.html?anchor=a41829236#a41829236
0
 

Author Closing Comment

by:V Thrusher
ID: 41830782
Thanks for the help Kim
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

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