Solved

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

Posted on 2011-02-28
7
323 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
  • 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 28

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

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

11 Experts available now in Live!

Get 1:1 Help Now