Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

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

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
m0tek
Asked:
m0tek
  • 3
  • 3
1 Solution
 
RobSampsonCommented:
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
 
peprCommented:
If you really want to see the same in Python, leave a note here.
0
 
m0tekAuthor Commented:
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
Technology Partners: 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!

 
RobSampsonCommented:
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
 
m0tekAuthor Commented:
trying to do this , but seems like its not possible ;(
0
 
m0tekAuthor Commented:
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
 
RobSampsonCommented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now