Solved

Vb script to unzip a files and rename the files

Posted on 2016-09-22
12
106 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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
 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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