Solved

Vb script to unzip a files and rename the files

Posted on 2016-09-22
12
71 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:vishnu adithya
  • 7
  • 5
12 Comments
 
LVL 35

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:vishnu adithya
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 35

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
 

Author Comment

by:vishnu adithya
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 35

Accepted Solution

by:
Kimputer earned 500 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:vishnu adithya
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:vishnu adithya
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 35

Expert Comment

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

Author Comment

by:vishnu adithya
ID: 41829852
what about line number 32 and 38 ? is it for existing .txt file ?
0
 
LVL 35

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:vishnu adithya
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:vishnu adithya
ID: 41830782
Thanks for the help Kim
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

746 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now