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

x
?
Solved

Vb script to unzip a files and rename the files

Posted on 2016-09-22
12
Medium Priority
?
256 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
  • 7
  • 5
12 Comments
 
LVL 37

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 37

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 37

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 37

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 37

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

972 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