Help with Batch / VBScript

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help with Batch / VBScript

#16 Post by aGerman » 28 Dec 2010 08:35

No, vbs accepts no wildcard (*). But you can call it with each found txt file in a FOR loop.
Try:

Code: Select all

@echo off &setlocal
for /f "delims=" %%a in ('dir /a-d /b *.txt') do (
  cscript //nologo "replacer.vbs" "%%~a"
)
pause


Regards
aGerman

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

Re: Help with Batch / VBScript

#17 Post by ghostmachine4 » 28 Dec 2010 08:56

aGerman wrote:No, vbs accepts no wildcard (*). But you can call it with each found txt file in a FOR loop.
Try:

Code: Select all

@echo off &setlocal
for /f "delims=" %%a in ('dir /a-d /b *.txt') do (
  cscript //nologo "replacer.vbs" "%%~a"
)
pause


Regards
aGerman


that would be too inefficient. You would have to invoke the cscript engine 1000 times if there are 1000 txt files in the directory. A better way to do it inside vbscript itself, and calling cscript just 1 time.

Code: Select all

....
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files     
       If objFS.GetExtensionName(strFile.Name) = "txt" Then
          ' open the file and do the replace here.
       End If
Next
....

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

Re: Help with Batch / VBScript

#18 Post by rfpd » 28 Dec 2010 09:38

ghost where do i put the code

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

Re: Help with Batch / VBScript

#19 Post by rfpd » 28 Dec 2010 09:42

thx guys i used aGerman code thanks anyways ghostmachine.

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

Re: Help with Batch / VBScript

#20 Post by rfpd » 28 Dec 2010 09:45

Sorry but i have one last question is it possible for this replace in all files of the computer (except Windows files).

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

Re: Help with Batch / VBScript

#21 Post by aGerman » 28 Dec 2010 10:02

I would NEVER(!!!!) try to do that.
1. The script works only for files with PLAIN TEXT contents.
2. You never know what happens if you replaced something and then is something else not working. Which of the thousands of changed files coused the error?

BTW ghostmachine4 is absolutly right. If you have to work over thousands of files you should use his suggestions (but before you have to learn VBScript if you don't know how to implement it).

Regards
aGerman

Post Reply