Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BAT TO VBS, cant find the right vbs script

Status
Not open for further replies.

Rrow

Programmer
May 4, 2010
2
NL
Hello everybody,

I'm trying to convert a BAT script to VBS, i'm also new in the VBS world.
I've already tried some scripts frome google.

This is the BAT script:
Code:
@echo off
pushd %LOGONSERVER%\netlogon\LimitLogin\

For /F "Tokens=1" %%I in ('TYPE Z:\LimitLogin\logs\%USERNAME%.txt') Do Set COMPNAME=%%I

IF "%COMPNAME%" EQU "%COMPUTERNAME%" DEL "Z:\LimitLogin\logs\%USERNAME%.txt"

:END
popd

This is the VBS script which i found on google.com, the VBS script doesn't work.
Code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
set WSHShell = wscript.createObject("wscript.shell")
set objNetwork = WScript.CreateObject("WScript.Network")
Set fso = CreateObject("Scripting.FileSystemObject")
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

CompName = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") 
UserName = WshShell.ExpandEnvironmentStrings("%USERNAME%") 
LogServer = WshShell.ExpandEnvironmentStrings("%LOGONSERVER%") 

Dim fso, myFolderName, objFolder, myFileName, objFile, myFQFilename, objTextStream, fileContents
Set fso = CreateObject("Scripting.FileSystemObject")
Dim forReading, forWriting, forAppending 
forReading = 1 
forWriting = 2 
forAppending = 8
myFileName = (""& LogServer &"\NETLOGON\LimitLogin\Logs\"& UserName &".txt") 
Set objTextStream = fso.OpenTextFile(myFileName, forReading)
fileContents =   objTextStream.ReadAll

If objTextStream.ReadAll ("strComputerName") then
fso.DeleteFile (""& LogServer &"\NETLOGON\LimitLogin\Logs\"& UserName &".txt"), True

Else
WScript.quit

END

The VBS script must read the .txt file and if the name in the .txt file contains the same name as the computer himself, the script must remove the .txt file.

Thanks, Rowan



 
Try this:

Code:
Dim WshShell, LogServer, CompName, UserName
Dim fso, myFileName, objTextStream
Dim forReading, oRegEx, lineContents
Dim strMatch, i

Set WshShell = CreateObject("WScript.Shell")

LogServer = WshShell.ExpandEnvironmentStrings("%LOGONSERVER%")
CompName = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
UserName = WshShell.ExpandEnvironmentStrings("%USERNAME%")

Set oRegEx = CreateObject("VBScript.RegExp")
oRegEx.Pattern = CompName
oRegEx.IgnoreCase = True

forReading = 1
i = 0

Set fso = CreateObject("Scripting.FileSystemObject")
myFileName =  LogServer & "\NETLOGON\LimitLogin\Logs\" & UserName & ".txt"

Set objTextStream = fso.OpenTextFile(myFileName, forReading)
Do Until objTextStream.AtEndOfStream
	lineContents = objTextStream.ReadLine
	strMatch = oRegEx.Test(lineContents)
	If strMatch Then
		i = i + 1
	End If
Loop
objTextStream.Close

If i > 0 Then
	fso.DeleteFile(myFileName), True
	MsgBox(myFileName & " has been deleted.")
Else
	MsgBox("Computer name not found in " & myFileName & ".")
End If

Set fso = nothing
Set WshShell = nothing
 
Correction:

This line -

myFileName = LogServer & "\NETLOGON\LimitLogin\Logs\" & UserName & ".txt"

should be -

myFileName = "" & LogServer & "\NETLOGON\LimitLogin\Logs\" & UserName & ".txt
 
[1] If you want to quite rigorously mimic what is done in the bat file (even under some situations), it would look something like this.
[tt]
dim WshShell, CompName, UserName, LogServer, fso
dim sfile, s, t, a

set WshShell=createobject("wscript.shell")
CompName = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
UserName = WshShell.ExpandEnvironmentStrings("%USERNAME%")
LogServer = WshShell.ExpandEnvironmentStrings("%LOGONSERVER%")
set WshShell=nothing

t=""
sfile=LogServer & "\netLogon\LimitLogin\logs\" & UserName & ".txt"

set fso=createobject("scripting.filesystemobject")
if fso.fileexists(sfile) then
if fso.getfile(sfile).size<>0 then
s=fso.opentextfile(sfile,1).readall 'suppose it is encoded in ascii
's=fso.opentextfile(sfile,1,false,-1).readall 'if it is encoded in ucs2
a=split(s,vbcrlf)
for i=ubound(a) to 0 step -1
t=trim(a(i))
if t<>"" then
t=split(t," ")(0)
exit for
end if
next
end if
end if
if t<>"" then
if strcomp(t,CompName,1)=0 then
fso.deletefile sfile, true
end if
end if
set fso=nothing
[/tt]
[1.1] This trascription is conditioned by the existence and accessibility of share netLogon.

[2] In the batch file itself, there seems to be some inconsistent as to the path pointing to an file in the logon server. But, I would pass in asking a clarification. In any case, if the path I used is not exact, feel free to add another level of directory in the path.
 
Thank you all,

I found a other script :
Code:
Const ForReading = 1

Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
set objNetwork = WScript.CreateObject("WScript.Network")
Set obj = CreateObject("Scripting.FileSystemObject") 
Set wshShell = WScript.CreateObject ("WSCript.shell")

CompName = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%") 
UserName = WshShell.ExpandEnvironmentStrings("%USERNAME%") 
LogServer = WshShell.ExpandEnvironmentStrings("%LOGONSERVER%") 

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
(""& LogServer &"\NETLOGON\LimitLogin\Logs\"& UserName &".txt", ForReading)
txtpc = objTextFile.ReadLine

If txtpc = strComputerName Then
obj.DeleteFile(""& LogServer &"\NETLOGON\LimitLogin\Logs\"& UserName &".txt"), force

Else
Wscript.Quit
End If

It works for me :D

Rowan
 
What is force? And it works, probably. As it is not yours, I pass.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top