Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vbs per controllo/sostituzione caratteri

260 views
Skip to first unread message

Andrea

unread,
Jan 22, 2007, 5:12:28 AM1/22/07
to
salve a tutti,
cercavo in rete uno script VBS che mi permettesse di usare il
trova/sostituisci automaticamente, dandogli in input un file di testo.
Non ho trovato nulla neanche sul sito Microsoft (per i vbs), qualcuno
di voi sa dirmi se c'è qualcosa del genere e dove posso trovarlo?

grazie mille
bye

Fosco

unread,
Jan 22, 2007, 10:00:27 PM1/22/07
to
Andrea:

> cercavo in rete uno script VBS che mi permettesse di usare il
> trova/sostituisci automaticamente, dandogli in input un file di testo.


'--the Replace function; replaces occurences of text in a file.
'--it will replace what is entered, not necessarily on a whole-word basis.

dim fil, s, r, s1, s2, snew, r1
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

'--get filename and open it. read all text into a string var.
r = inputbox("Enter the path of file", "What file?", "C:\test.txt")
if fso.FileExists(r) = false then
msgbox "There's no such file!", 64, "That's not it"
wscript.Quit
end if
set fil = fso.OpenTextFile(r, 1)
s = fil.ReadAll
fil.Close
set fil = nothing

'--get text to replace and new text.
s1 = inputbox("Enter text to be replaced.", "What to replace?")
s2 = inputbox("Enter new text.", "Replacement text")

r1 = msgbox("Is the replacement case-sensitive?", 36, "Case Sensitive?")
if r1 = 6 then
snew = replace(s, s1, s2, 1, -1, 0) '-- 1 is starting pos., -1 is to replace all occur., 0 is for case sens.
else
snew = replace(s, s1, s2, 1, -1, 1) '-- 1 is for not case sens.
end if

'--delete the old file and write the changed string in its place.
fso.DeleteFile r, true
set fil = fso.CreateTextFile(r)
fil.Write snew
fil.Close
set fil = nothing
msgbox "All done", 0


' stesso file ma gia' in vbs se hai problemi col word wrap qui
http://www.rogepost.com/n/9942488077

altro esempio (basta trascinarci sopra i file)

Set oArgs = WScript.Arguments
If oArgs.Count <> 1 Then
WScript.Echo "Wrong number of arguments!"
End If
sDirectory= oArgs(0)
'sDirectory = "C:\WINDOWS\TEMP"
Set oFSO = CreateObject("Scripting.FileSystemObject")
set oShell = CreateObject("WScript.Shell")
Set oFolder = oFSO.GetFolder(sDirectory)
For Each oFile In oFolder.Files
Set oFSO = CreateObject("Scripting.FileSystemObject")
sContents = oFSO.OpenTextFile(ofile).ReadAll
sContents = Replace(sContents, "cippo", "lippo")
oFSO.CreateTextFile(ofile, True).Write sContents

Next

msgbox "fatto"


--
Fosco

Andrea

unread,
Jan 23, 2007, 3:33:43 AM1/23/07
to

Fosco ha scritto:

>
> ' stesso file ma gia' in vbs se hai problemi col word wrap qui
> http://www.rogepost.com/n/9942488077
>
> altro esempio (basta trascinarci sopra i file)
>

grazie!
è possibile mettere anche un contatore per capire quanti caratteri è
andato a sostituire?

Fosco

unread,
Jan 23, 2007, 11:45:32 AM1/23/07
to
Fosco:
> Andrea:

> altro esempio (basta trascinarci sopra i file)

Set oArgs = WScript.Arguments
'If oArgs.Count <> 1 Then
' WScript.Echo "Wrong number of arguments!"
'End If
'sDirectory= oArgs(0)
'sDirectory = "C:\WINDOWS\TEMP"
Set oFSO = CreateObject("Scripting.FileSystemObject")
set oShell = CreateObject("WScript.Shell")
'Set oFolder = oFSO.GetFolder(sDirectory)

For Each oFile In oArgs

Fosco

unread,
Jan 23, 2007, 9:54:42 PM1/23/07
to
Andrea:
> grazie!
> č possibile mettere anche un contatore per capire quanti caratteri č
> andato a sostituire?

'Si

' la base

s = "cippolippowashere"
MsgBox Len(s)

' come implementarlo effettivamente >>> studiare :


Questo sotto e' l'help del WSH con numerosi esempi ( sopra il mega)
http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-
1C4099D7BBB9&displaylang=en

snip : http://snipurl.com/jtur

Download :
http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe

snip : http://snipurl.com/jtut

Download and install WSH 5.6 from here:
http://msdn.microsoft.com/downloads/list/webdev.asp
9x
http://www.microsoft.com/downloads/details.aspx?FamilyId=0A8A18F6-249C-4A72-BFCF-
FC6AF26DC390&displaylang=en
XP
http://www.microsoft.com/downloads/details.aspx?FamilyId=C717D943-7E4B-4622-86EB-
95A22B832CAA&displaylang=en


http://www.microsoft.com/downloads/details.aspx?FamilyID=c717d943-7e4b-4622-86eb-
95a22b832caa&DisplayLang=it

scipt56 italiano per 2000 e XP

per 98 e NT :
http://www.microsoft.com/downloads/info.aspx?na=22&p=18&SrcDisplayLang=
it&SrcCategoryId=12&SrcFamilyId=&u=%2fdownloads%2fdetails.aspx%3fFamilyID%3d0a8a18f6-
249c-4a72-bfcf-fc6af26dc390%26DisplayLang%3dit

--
Fosco

Andrea

unread,
Jan 24, 2007, 6:37:04 AM1/24/07
to
On 24 Gen, 03:54, Fosco <f...@fake.invalid> wrote:

> Questo sotto e' l'help del WSH con numerosi esempi ( sopra il mega)http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207...
> 1C4099D7BBB9&displaylang=en
>
> snip : http://snipurl.com/jtur
>
> Download :http://download.microsoft.com/download/winscript56/Install/5.6/W982KM...


>
> snip : http://snipurl.com/jtut
>
> Download and install WSH 5.6 from here:http://msdn.microsoft.com/downloads/list/webdev.asp

> 9xhttp://www.microsoft.com/downloads/details.aspx?FamilyId=0A8A18F6-249...
> FC6AF26DC390&displaylang=en
> XPhttp://www.microsoft.com/downloads/details.aspx?FamilyId=C717D943-7E4...
> 95A22B832CAA&displaylang=en
>
> http://www.microsoft.com/downloads/details.aspx?FamilyID=c717d943-7e4...


> 95a22b832caa&DisplayLang=it
>
> scipt56 italiano per 2000 e XP

ottimo! grazie per i link!

Fosco

unread,
Jan 24, 2007, 10:06:33 AM1/24/07
to
Fosco:
> Fosco:

Guardando in giro nel mio HD ho notato che avevo gia fatto
qualcosa di simile tempo fa'. l'ho modificato leggermente
cosi si puo' trascinare sopra allo script una cartella contenente i
file da modificare verranno procesati TUTTI i files contenuti
ed ogni accenno di pippo verra sostituito con topolino

'> se devo cambiare la parola "pippo" in "topolino" in un file,
'> so farlo facilmente con il normale editor (Blocco note)
'> di Windows (basta fare: Modifica ---> Sostituisci...)
'> Ma se devo fare la stessa cosa su circa 200 file diversi,
'> come posso fare senza perderci dei mesi??

'L' ho fatto al volo e NON ho il tempo di testarlo a fondo
'metti tutti i tuoi file (fatti un backup PRIMA) in c:\windows\temp
'o cambia il percorso nello script e poi prova a lanciare questo sotto :

' modificato per trascinarci sopra direttamente la directory
' contenente i file da modificare


Set oArgs = WScript.Arguments
If oArgs.Count <> 1 Then
WScript.Echo "Wrong number of arguments!"
End If
sDirectory= oArgs(0)


'sDirectory = "C:\WINDOWS\TEMP"
Set oFSO = CreateObject("Scripting.FileSystemObject")
set oShell = CreateObject("WScript.Shell")
Set oFolder = oFSO.GetFolder(sDirectory)

msgbox oFolder & " " & oArgs(0)


For Each oFile In oFolder.Files

Set oFSO = CreateObject("Scripting.FileSystemObject")
sContents = oFSO.OpenTextFile(ofile).ReadAll

sContents = Replace(sContents, "pippo", "topolino")
oFSO.CreateTextFile(ofile, True).Write sContents

Next

msgbox "fatto"

'Copia incolla su notepad e salva con estensione .vbs
'tutti i file (*.*) poi ci clicchi sopra. (per esempio Test.vbs)
'se ti da' errore disattiva l'antivirus

'(precedente versiobe se si impostava una directory fissa al posto del oArgs(0) )
'PS:
'IMPORTANTE tieni lo script fuori dalla cartella operativa
'altrimenti verra modificato ANCHE quello


--
Fosco

Fosco

unread,
Jan 24, 2007, 10:19:14 AM1/24/07
to
Andrea:
> č possibile mettere anche un contatore per capire quanti caratteri č
> andato a sostituire?

ahh forse avevo capito male , prova questo se non
riesci a copioincollarlo dimmelo che lo metto su un upload


'--give it a file path and a string. it'll tell you the number of
'--occurences of that string.
'---------------------------------------------------
Dim fso, f, ftext, r, stext, lstart, i, ls, fok


Set fso = CreateObject("Scripting.FileSystemObject")


'--get the path of the file. (There's a bug in Inputbox that
'will cause an error if --Cancel is clicked.)
on error resume next
ftext = inputbox("Enter the path of the file for processing.","FILENAME")
if ftext = "" then
set fso = nothing
wscript.quit
end if
'--check whether the path is valid:
If fso.fileexists(ftext) = false then
fok = false
do while fok = false
r = msgbox("The path is wrong, there's no such file.", 36, "Woops!")
if r = 7 then
set fso = nothing
wscript.quit
else
ftext = inputbox("Enter the filename for processing.", "FILENAME")
if fso.fileexists(ftext) = true then
fok = true
exit do
end if
end if
loop
end if

'--by this point there's a valid path.
'--ask for the string being counted:

fs = inputbox("What is the string you're looking for?", "Enter string")
if fs = "" then
set fso = nothing
wscript.quit
end if

'--open the file and read it into the variable stext:
set f = fso.opentextfile(ftext)
stext = f.readall
f.close
set f = nothing
set fso = nothing

'--the search - i is the counter. InStr looks for the string, starting
' at lstart.
'-- every time it's found, i is increased by 1 and lstart is set
' to ls + 1
'-- ( 1 character after where the string was found).
'When InStr returns 0 there are no --more instances of the string.

lstart = 1
i = 0
ls = 1
do until ls = 0
ls = instr(lstart, stext, fs, 1)
If ls <> 0 then
i = i + 1
lstart = ls + 1
end if
loop

msgbox i

--
Fosco

0 new messages