Solved

Script Which recieves 2 arguments from file and does a few manipulations

Posted on 2011-02-28
7
326 Views
Last Modified: 2012-05-11
Hi Guys , I know my share of some python and batch ,
I Need some help to ease a few things im doing and was wondering if someone could help me
I Have a certain csv files which is built with 2 columns

i.e -
LooLoo,Poopik
Sha,lala
and so on..

I need a script which will accept those 2 arguments (which are in a file and separated by comma) and will do the following

if i take a look at example 1
it will create a directory called LooLoo , copy a certain file (blabla.txt) to that directory , rename the file to LooLoo.txt , then search for a certain argument (it will always be the same argument) - and replace it with the text in Column 2 - i.e - Poopik

Any Help would be appreciated
thanks!

0
Comment
Question by:m0tek
[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
  • 3
  • 3
7 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35002316
Hi, I think this is what you're after.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1

strInputFile = "Arguments.csv"
strBaseDir = "C:\Temp"
strFileToCopy = "c:\Temp\blabla.txt"
strArgToReplace = "[REPLACEME]"

Set objTemplate = objFSO.OpenTextFile(strFileToCopy, ForReading, False)
strTemplate = objTemplate.ReadAll
objTemplate.Close

Set objFile = objFSO.OpenTextFile(strInputFile, ForReading, False)
If Right(strBaseDir, 1) = "\" Then strBaseDir = Left(strBaseDir, Len(strBaseDir) - 1)
While Not objFile.AtEndOfStream
	strLine = objFile.ReadLine
	If InStr(strLine, ",") > 0 Then
		strParam1 = Split(strLine, ",")(0)
		strParam2 = Split(strLine, ",")(1)
		If objFSO.FolderExists(strBaseDir & "\" & strParam1) = False Then objFSO.CreateFolder strBaseDir & "\" & strParam1
		Set objText = objFSO.CreateTextFile(strBaseDir & "\" & strParam1 & "\" & strParam1 & ".txt", True)
		objText.Write Replace(strTemplate, strArgToReplace, strParam2)
		objText.Close
	End If
Wend
objFile.Close

Open in new window

0
 
LVL 29

Expert Comment

by:pepr
ID: 35004956
If you really want to see the same in Python, leave a note here.
0
 

Author Comment

by:m0tek
ID: 35015698
Rob , is there a way to add to the vbscript replace of 2 strings? not just one?
if so - how do i do that?
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!

 
LVL 65

Expert Comment

by:RobSampson
ID: 35022710
Hi, sure it is ;-)   As many as you want in fact.

If you want to replace both (or more) strings with the data in column 2, then this will do it.  Add more to arrArgsToReplace if you want.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1

strInputFile = "Arguments.csv"
strBaseDir = "C:\Temp"
strFileToCopy = "c:\Temp\blabla.txt"
arrArgsToReplace = Array("[REPLACEME]", "[SWAPME]")

Set objTemplate = objFSO.OpenTextFile(strFileToCopy, ForReading, False)
strTemplate = objTemplate.ReadAll
objTemplate.Close

Set objFile = objFSO.OpenTextFile(strInputFile, ForReading, False)
If Right(strBaseDir, 1) = "\" Then strBaseDir = Left(strBaseDir, Len(strBaseDir) - 1)
While Not objFile.AtEndOfStream
	strLine = objFile.ReadLine
	If InStr(strLine, ",") > 0 Then
		strParam1 = Split(strLine, ",")(0)
		strParam2 = Split(strLine, ",")(1)
		If objFSO.FolderExists(strBaseDir & "\" & strParam1) = False Then objFSO.CreateFolder strBaseDir & "\" & strParam1
		Set objText = objFSO.CreateTextFile(strBaseDir & "\" & strParam1 & "\" & strParam1 & ".txt", True)
		strNewText = strTemplate
		For Each strArgToReplace In arrArgsToReplace
			strNewText = Replace(strNewText, strArgToReplace, strParam2)
		Next
		objText.Write strNewText
		objText.Close
	End If
Wend
objFile.Close

Open in new window

0
 

Author Comment

by:m0tek
ID: 35025044
trying to do this , but seems like its not possible ;(
0
 

Author Comment

by:m0tek
ID: 35025060
forgot to add snippet

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1

strInputFile = "dbs.csv"
strBaseDir = "C:\arik"
strFileToCopy = "c:\arik\DB2"
strArgToReplace = "change1"
strArgToReplace2 = "swap2"

Set objTemplate = objFSO.OpenTextFile(strFileToCopy, ForReading, False)
strTemplate = objTemplate.ReadAll
objTemplate.Close

Set objFile = objFSO.OpenTextFile(strInputFile, ForReading, False)
If Right(strBaseDir, 1) = "\" Then strBaseDir = Left(strBaseDir, Len(strBaseDir) - 1)
While Not objFile.AtEndOfStream
      strLine = objFile.ReadLine
      If InStr(strLine, ",") > 0 Then
            strParam1 = Split(strLine, ",")(0)
            strParam2 = Split(strLine, ",")(1)
            If objFSO.FolderExists(strBaseDir & "\" & strParam1) = False Then objFSO.CreateFolder strBaseDir & "\" & strParam1
            Set objText = objFSO.CreateTextFile(strBaseDir & "\" & strParam1 & "\" & strParam1 & ".arik.properties", True)
            objText.Write Replace(strTemplate, strArgToReplace, strParam2)
            objText.Write Replace(strTemplate, strArgToReplace2, strParam1)
            
            objText.Close
      End If
Wend
objFile.Close
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35033046
I see, try this.  When doing mutiple replaces, you'll need to make the replacements into a new variable, then write the final output after that.

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1

strInputFile = "dbs.csv"
strBaseDir = "C:\arik"
strFileToCopy = "c:\arik\DB2"
strArgToReplace = "change1"
strArgToReplace2 = "swap2"

Set objTemplate = objFSO.OpenTextFile(strFileToCopy, ForReading, False)
strTemplate = objTemplate.ReadAll
objTemplate.Close

Set objFile = objFSO.OpenTextFile(strInputFile, ForReading, False)
If Right(strBaseDir, 1) = "\" Then strBaseDir = Left(strBaseDir, Len(strBaseDir) - 1)
While Not objFile.AtEndOfStream
	strLine = objFile.ReadLine
	If InStr(strLine, ",") > 0 Then
		strParam1 = Split(strLine, ",")(0)
		strParam2 = Split(strLine, ",")(1)
		If objFSO.FolderExists(strBaseDir & "\" & strParam1) = False Then objFSO.CreateFolder strBaseDir & "\" & strParam1
		Set objText = objFSO.CreateTextFile(strBaseDir & "\" & strParam1 & "\" & strParam1 & ".arik.properties", True)
		strNewText = strTemplate
		strNewText = Replace(strNewText, strArgToReplace, strParam2)
		strNewText = Replace(strNewText, strArgToReplace2, strParam1)
		objText.Write strNewText
		objText.Close
	End If
Wend
objFile.Close

Open in new window

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

733 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