Solved

Vb script to unzip a files and rename the files

Posted on 2016-09-22
12
93 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

929 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

15 Experts available now in Live!

Get 1:1 Help Now