Help with Batch / VBScript

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Help with Batch / VBScript

#1 Post by rfpd » 26 Dec 2010 19:13

Hey guys some time ago i created one file in visual basic script (a file that replaces text) that you just need to move the file to the program icon, and it replaces the text for what we want. But i want to know if it's possible to do what i said but in Batch.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with Batch / VBScript

#2 Post by aGerman » 26 Dec 2010 19:39

You should enhance your verbosity level. Nobody knows your VBScript and nobody knows what part of what string you want to replace with which substring. Give an example.

BTW: Did you read http://www.dostips.com/forum/viewtopic.php?f=3&t=1516?

Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Help with Batch / VBScript

#3 Post by ghostmachine4 » 26 Dec 2010 19:44

rfpd wrote:Hey guys some time ago i created one file in visual basic script (a file that replaces text) that you just need to move the file to the program icon, and it replaces the text for what we want. But i want to know if it's possible to do what i said but in Batch.

if you already have a vbscript for that, then why use batch? batch (cmd.exe commands) is not suitable for file processing/manipulation, period.

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Help with Batch / VBScript

#4 Post by rfpd » 27 Dec 2010 05:42

no guys loll u didn't understand, i got the code a code in the internet but i al ready deleted. But for a vbs work you need to move 1 file to the vbs and then code runs i wanted to do that but in batch.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with Batch / VBScript

#5 Post by aGerman » 27 Dec 2010 06:05

rfpd wrote:But for a vbs work you need to move 1 file to the vbs and then code runs i wanted to do that but in batch.

If I didn't get you wrong then "move" means "drag and drop" in this case?

Drop a file onto this batch:

Code: Select all

@echo off
echo %1
pause

As you can see the file comes as argument %1.

Regards
aGerman

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Help with Batch / VBScript

#6 Post by rfpd » 27 Dec 2010 10:30

sorry i explain very bad but what i was asking was a batch code that drags and drops other files into a vbs.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with Batch / VBScript

#7 Post by aGerman » 27 Dec 2010 11:13

rfpd wrote:sorry i explain very bad but what i was asking was a batch code that drags and drops other files into a vbs.


Yeah sometimes it's not easy to understand what you're looking for.

OK. You can call a VBS with arguments as well.
The first argument you will find in WScript.Arguments(0), the second in WScript.Arguments(1) etc.

For testing we have 3 Files in the same directory:

test.txt

Code: Select all

Texd dhad acds as desd.



test.vbs

Code: Select all

WScript.Echo Replace(WScript.CreateObject("Scripting.FileSystemObject").OpenTextFile(WScript.Arguments(0), 1).ReadAll, WScript.Arguments(1), WScript.Arguments(2))



test.bat

Code: Select all

@echo off &setlocal
cscript //nologo "test.vbs" "test.txt" "d" "t"
pause

As you can see "test.txt" is the first argument for the VBS, "d" the second and "t" the third.

Now you can double click test.bat to find out what happens.

Regards
aGerman

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Help with Batch / VBScript

#8 Post by rfpd » 27 Dec 2010 13:02

aGerman thanks but what i really whant is that the vbs replaces on the file no on the cmd only. Because what i want to do it's a batch that replaces strings in another batchs and multiple search

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Help with Batch / VBScript

#9 Post by ChickenSoup » 27 Dec 2010 15:27

I'd try to help but, I'm not understanding exactly what you want to do.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with Batch / VBScript

#10 Post by aGerman » 27 Dec 2010 15:36

@ChickenSoup
This is my problem as well.

@rfpd
http://www.dostips.com/forum/viewtopic.php?f=3&t=1075&start=0
Why don't you use it? What is wrong with this Xchange.vbs and what didn't you understand?

AND AGAIN: If you cannot explain what you need then use an example to show the problem!

Regards
aGerman

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Help with Batch / VBScript

#11 Post by rfpd » 27 Dec 2010 17:35

vbscript:

Code: Select all

'fix.vbs
findWhat    = Array("…",   "Ά",  "Έ")
replaceWith = Array("...", "'A", "'E")

For Each arg In WScript.Arguments
  Set Doc = GetObject(arg)
  Doc.Application.Visible = True
  For i = 0 To UBound(findWhat)
    Doc.Range(0, Doc.Application.Selection.End).Find.Execute _
      findWhat(i), True, False, False, False, False, True, 1, _
      False, replaceWith(i), 2, False, False, False, False
  Next 'i
Next 'arg


Video explaining:http://www.youtube.com/watch?v=w8riNo2XQsU
And i also want to the file do the replace in batch files not in doc.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with Batch / VBScript

#12 Post by aGerman » 27 Dec 2010 19:21

Now I think I know what you try to do: You want to replace code of a batch file using VBScript.

VBS:

Code: Select all

findWhat    = Array("…",   "Ά",  "Έ")
replaceWith = Array("...", "'A", "'E")

Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
text = oFSO.OpenTextFile(WScript.Arguments(0), 1).ReadAll
If Not text = "" Then
  For i = 0 To UBound(findWhat)
    text = Replace(text, findWhat(i), replaceWith(i))
  Next
End If

oFSO.CreateTextFile(WScript.Arguments(0)).Write text

Drag and drop your batch file onto this VBScript.

Regards
aGerman

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Help with Batch / VBScript

#13 Post by rfpd » 28 Dec 2010 06:03

Thanks aGerman. But imagin we have files:

-the one to be replace.bat
-replacer.vbs
-other.bat

It's possible to the file "other.bat" drag "the one to be replace.bat" and drop into "replacer.vbs"?

Ps: The vbs replaces hey for bye but if i put HeY that doesn't replace.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with Batch / VBScript

#14 Post by aGerman » 28 Dec 2010 07:24

rfpd wrote:It's possible to the file "other.bat" drag "the one to be replace.bat" and drop into "replacer.vbs"?

It's exactly what I told you some posts before. You can call the vbs with a filename as argument. You will find a dropped file or the first argument of a command line in "WScript.Arguments(0)" as well.

other.bat

Code: Select all

@echo off &setlocal
cscript //nologo "replacer.vbs" "the one to be replace.bat"



rfpd wrote:Ps: The vbs replaces hey for bye but if i put HeY that doesn't replace.

Yes, the replace function of VBScript is case sensitive by default. Two possible solutions:
1st - You have to write another element to both arrays for HeY.
2nd - You change the code of the vbs as follows:
text = Replace(text, findWhat(i), replaceWith(i), 1, -1, vbTextCompare)

Regards
aGerman

rfpd
Posts: 78
Joined: 06 Jul 2009 16:19
Location: Lisbon, Portugal
Contact:

Re: Help with Batch / VBScript

#15 Post by rfpd » 28 Dec 2010 08:28

thx a lot aGerman. But can i put it for replace multiple files?

Code: Select all

@echo off &setlocal
cscript //nologo "replacer.vbs" "*.txt"
pause


But that doesn't work. Thanks in advance.

Post Reply