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

How to read a text file in vbscript

1,783 views
Skip to first unread message

kerkuk...@gmail.com

unread,
Mar 22, 2013, 8:51:00 AM3/22/13
to
Hallo,

I'm looking for a vbscript that reads a text file line by line till the last line. And within the same script there is a icacls command. So the script is supposed to read the text file and execute the command in cmd.

Please help i really need this.

R.Wieser

unread,
Mar 22, 2013, 9:51:12 AM3/22/13
to
What have you already tried yourself ?

Mind you, as willing we are to *help* you, most of us have no desire to just
write whatever you ask for ...

Also, what you describe sounds as homework, and that you definitily need to
do yourself.

Suggestion: look for the "OpenTextFile", "AtEndOfStream", "ReadLine" and
"Close" commands.

Regards,
Rudy Wieser


-- Origional message:
<kerkuk...@gmail.com> schreef in berichtnieuws
ec8f83b2-2bd2-4201...@googlegroups.com...

Dave "Crash" Dummy

unread,
Mar 22, 2013, 10:23:02 AM3/22/13
to
Reading the text file line by line is easy. Using that line as a command
may or may not be. Can you post some examples?
--
Crash

"Never underestimate the power of the Dark Side."
~ Obi-Wan Kenobi ~

kerkuk...@gmail.com

unread,
Mar 22, 2013, 10:28:56 AM3/22/13
to
Op vrijdag 22 maart 2013 15:23:02 UTC+1 schreef Dave Crash Dummy het volgende:
first of all, it's not a homework. I'm just not so good in scripting. I have a network of over 2500 users and i want to change the owner of the sub-folders of the home directories and the admin is the owner of the directory it self.

this is what i want to have, not sure this might work, but i hope this gives you guys a view of what i'm trying to get.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\userdirlist.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Read(1)
Wscript.Echo strCharacters
Loop

For i = 1 to (the last line)
readline 1
$userfolder = readline 1
icacls /setown D:\Data\userhome\$userfolder DC01\$userfolder /t
icacls /setown D:\Data\userhome\$userfolder dc01\administrators
nextline
next

End

R.Wieser

unread,
Mar 22, 2013, 12:10:18 PM3/22/13
to
> first of all, it's not a homework.

My apologies.

Have you already executed that VBS code ? I think you either got a *lot*
of pop-up boxes (by using wscript thru double-clicking the VBS file), or a
loooong list of lines with just one character per line (by using cscript in
a console window).

Suggestion: change the "read(1)" to "ReadLine". With that you read a whole
line, not one character at a time (which you than have to combine yourself
until you reach an end-of-line).

After that its just a matter of replacing that "Wscript.echo" command with
an "objFSO.run" command.

Hope that helps.
Rudy Wieser


-- Origional message:
<kerkuk...@gmail.com> schreef in berichtnieuws
71d87fd2-0d77-4496...@googlegroups.com...

Michael Bednarek

unread,
Mar 23, 2013, 12:25:35 AM3/23/13
to
On Fri, 22 Mar 2013 07:28:56 -0700 (PDT), kerkukisoran@... wrote in microsoft.public.scripting.vbscript:

>Op vrijdag 22 maart 2013 15:23:02 UTC+1 schreef Dave Crash Dummy het volgende:
>> kerkukisoran@... wrote:
>>
>> > Hallo,
>>
>> > I'm looking for a vbscript that reads a text file line by line till
>> > the last line. And within the same script there is a icacls command.
>> > So the script is supposed to read the text file and execute the
>> > command in cmd.
[snip]
>first of all, it's not a homework. I'm just not so good in scripting. I have a network of over 2500 users and i want to change the owner of the sub-folders of the home directories and the admin is the owner of the directory it self.
>
>this is what i want to have, not sure this might work, but i hope this gives you guys a view of what i'm trying to get.
>
>Set objFSO = CreateObject("Scripting.FileSystemObject")
>Set objFile = objFSO.OpenTextFile("C:\userdirlist.txt", 1)
>Do Until objFile.AtEndOfStream
> strCharacters = objFile.Read(1)
> Wscript.Echo strCharacters
>Loop
>
>For i = 1 to (the last line)
> readline 1
> $userfolder = readline 1
> icacls /setown D:\Data\userhome\$userfolder DC01\$userfolder /t
> icacls /setown D:\Data\userhome\$userfolder dc01\administrators
> nextline
> next
>
>End

I'm not sure that VCScript is the right tool for this job. Given that the
ultimate goal is to issue ICACLS commands, I suggest a command line environment
is more appropriate. The simplest tool for this particular task is IMO
Take Command, where it looks like this (1 line):

FOR %userfolder IN (@C:\userdirlist.txt) (icacls /setown "D:\Data\userhome\%userfolder" "DC01\%userfolder" /t %+ icacls /setown D:\Data\userhome\%userfolder dc01\administrators)

I suspect that cmd.exe or PowerShell can do the same thing.

--
Michael Bednarek "ONWARD"

Mayayana

unread,
Mar 23, 2013, 10:06:41 AM3/23/13
to
Mr. Bednarek is suggesting a $100 program to do Windows
admin. tasks. If that's necessary then I guess it's time
to move to Linux. :)

Also, I don't know whether icacls recognizes special
folder paths. If you need to find those paths on other
machines you can use Shell.Application. The method is
slightly convoluted, but it works. It's a wrapper for the
various Windows API methods to get special folders.
Shell.Application is basically the object model for Explorer
and its folder view.

Mr. Wieser had a typo in his post, saying that objFSO.run
can be used to run icacls. That should have been:

Set WS = CreateObject("WScript.Shell")
SH.Run

The Run method can run a command line and optionally
wait for the call to return. I haven't used icacls, but I
assume Run will work for that. Don't you also need to set
permissions after the setowner call?

WMI also has methods for setting ownership and
permissions. But be warned, both permisisons and
WMI are a convoluted mess. For WMI you need to
get a Win32_SecurityDescriptor via the GetSecurityDescriptor
method of Win32_LogicalFileSecuritySetting.

Personally I don't have any experience with network
admin, but my understanding is that WMI is designed
especially for that, so you probably want to familiarze yourself
with it. It's a pain to use but if you search for
something like: WMI Win32_SecurityDescriptor owner
you should find some VBScript samples. (I have sample
in a WMI book but it's too much to transcribe.)

If you don't have the WMI help file you should probably
get a copy. Unfortunately, virtually all Windows
docs now come only in the gigantic Windows SDK. In addition
to downloading that, the help files now come as HXS "help 2"
files, with no standalone viewer. If you want to get CHMs
of these files you can download the SDK, unpack it, and convert
the HXS files to CHM. See here for tools and directions:

http://www.jsware.net/jsware/hxs2chm.php5

The download is a set of scripts and an HTA that I wrote
myself when I discovered that Microsoft had withdrawn
virtually all of the smaller SDKs and doc packages in favor
of a single, large SDK intended to integrate with Visual
Studio.Net. Since most of the docs now are .Net or the
newer WinRT API (for writing TileUI trinkets) there's no
reason for Windows programmers and scripters who are
not using .Net, to install what amounts to multiple GBs of
bloat, just to read a 1 MB help file. If you download and
unpack any recent vintage Windows SDK you can also get
the IE DOM help, SAPI, Shell, WSH, etc. and convert those
to CHMs for future reference.





--
--
<kerkuk...@gmail.com> wrote in message
news:71d87fd2-0d77-4496...@googlegroups.com...

kerkuk...@gmail.com

unread,
Mar 29, 2013, 8:51:26 AM3/29/13
to
Op vrijdag 22 maart 2013 13:51:00 UTC+1 schreef kerkuk...@gmail.com het volgende:
> Hallo,
>
>
>
> I'm looking for a vbscript that reads a text file line by line till the last line. And within the same script there is a icacls command. So the script is supposed to read the text file and execute the command in cmd.
>
>
>
> Please help i really need this.


Hallo guys,

thanks for the responds. I have been trying things out. I got the a script which should work, but I'm not sure the icacls code is correct like this:


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("G:\Userhome\userdirlist.txt")
Set oShell = WScript.CreateObject("WSCript.shell")

Do Until objFile.AtEndOfStream
struserfolder = objFile.ReadLine
oshell.run ("icacls G:\userhome\"+ struserfolder +"\*.* /setowner domain\" + "struserfolder" + "/t")
oshell.run ("icacls G:\userhome\"+ struserfolder +"\*.* /setowner domain\Administrator")
Loop

Todd Vargo

unread,
Mar 29, 2013, 3:36:44 PM3/29/13
to
On 3/29/2013 8:51 AM, kerkuk...@gmail.com wrote:
> Hallo guys,
>
> thanks for the responds. I have been trying things out. I got the a script which should work, but I'm not sure the icacls code is correct like this:
>
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFile = objFSO.OpenTextFile("G:\Userhome\userdirlist.txt")
> Set oShell = WScript.CreateObject("WSCript.shell")
>
> Do Until objFile.AtEndOfStream
> struserfolder = objFile.ReadLine
> oshell.run ("icacls G:\userhome\"+ struserfolder +"\*.* /setowner domain\" + "struserfolder" + "/t")
> oshell.run ("icacls G:\userhome\"+ struserfolder +"\*.* /setowner domain\Administrator")
> Loop
>

FWIW, to verify I didn't slip and miss a quote some place, I typically
create the command in a variable and use MsgBox it to verify it looks
correct before committing it to oshell.run.

cmd = "icacls G:\userhome\" + etc.
MsgBox cmd, vbInformation, "Verify syntax is correct."
'oshell.run (cmd)

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
0 new messages