Home All Groups Group Topic Archive Search About

Importing variables into Batch script from text file



Author
26 Feb 2007 8:48 PM
mrbowes
Hi,

I'm trying to be able to source in variables into my batch script from
a text file.  I know how to do this in a shell script, but I'm not
sure how to do it in a Windows batch script.  Is this even possible?
The work-around I thought of would be to have a batch file (instead of
text file) set the variables to environment variables such that other
scripts could use them.

Ideally, I'd like to be able to have a simple text file to be used to
hold the configuration data and then source that into my batch script.

Any help would be greatly appreciated. Thank you.

Regards,

Josh

Author
27 Feb 2007 1:43 PM
Tom Lavedas
On Feb 26, 3:48 pm, mrbo***@gmail.com wrote:
Show quote
> Hi,
>
> I'm trying to be able to source in variables into my batch script from
> a text file.  I know how to do this in a shell script, but I'm not
> sure how to do it in a Windows batch script.  Is this even possible?
> The work-around I thought of would be to have a batch file (instead of
> text file) set the variables to environment variables such that other
> scripts could use them.
>
> Ideally, I'd like to be able to have a simple text file to be used to
> hold the configuration data and then source that into my batch script.
>
> Any help would be greatly appreciated. Thank you.
>
> Regards,
>
> Josh

If you were to use a simple file with two bits of information on each
line, thus ...

varname=Sometext to define variable content

Then this would do the job ...

  for /f "tokens=1,2 delims==" %%a in (yourfilespec) do call :sub %%a
"%%b"
  goto :EOF

:sub
  set %1=%~2

The variables would only be set for that particular console session.

Type FOR/? at a command prompt for more information.

Tom Lavedas
=============
http://members.cox.net/tglbatch/wsh/
Author
1 Mar 2007 4:55 PM
MMC-Michelle
I would like to do this but with a VBS script for ADAM rather than a batch
file.  I have a working script for deleting a single users.    How would I
modify that to have it call a list of users to delete?  My script looks just
like the Delete Users example listed here:
http://www.microsoft.com/technet/scriptcenter/scripts/ds/adam/modify/default.mspx?mfr=true only modified for my system.

Thanks.



Show quote
"Tom Lavedas" wrote:

> On Feb 26, 3:48 pm, mrbo***@gmail.com wrote:
> > Hi,
> >
> > I'm trying to be able to source in variables into my batch script from
> > a text file.  I know how to do this in a shell script, but I'm not
> > sure how to do it in a Windows batch script.  Is this even possible?
> > The work-around I thought of would be to have a batch file (instead of
> > text file) set the variables to environment variables such that other
> > scripts could use them.
> >
> > Ideally, I'd like to be able to have a simple text file to be used to
> > hold the configuration data and then source that into my batch script.
> >
> > Any help would be greatly appreciated. Thank you.
> >
> > Regards,
> >
> > Josh
>
> If you were to use a simple file with two bits of information on each
> line, thus ...
>
> varname=Sometext to define variable content
>
> Then this would do the job ...
>
>   for /f "tokens=1,2 delims==" %%a in (yourfilespec) do call :sub %%a
> "%%b"
>   goto :EOF
>
>  :sub
>   set %1=%~2
>
> The variables would only be set for that particular console session.
>
> Type FOR/? at a command prompt for more information.
>
> Tom Lavedas
> =============
> http://members.cox.net/tglbatch/wsh/
>
>
Author
2 Mar 2007 1:56 AM
Tom Lavedas
On Mar 1, 11:55 am, MMC-Michelle
<MMCMiche***@discussions.microsoft.com> wrote:
Show quote
> I would like to do this but with a VBS script for ADAM rather than a batch
> file.  I have a working script for deleting a single users.    How would I
> modify that to have it call a list of users to delete?  My script looks just
> like the Delete Users example listed here:http://www.microsoft.com/technet/scriptcenter/scripts/ds/adam/modify/...only modified for my system.
>
> Thanks.
>
> "Tom Lavedas" wrote:
> > On Feb 26, 3:48 pm, mrbo***@gmail.com wrote:
> > > Hi,
>
> > > I'm trying to be able to source in variables into my batch script from
> > > a text file.  I know how to do this in a shell script, but I'm not
> > > sure how to do it in a Windows batch script.  Is this even possible?
> > > The work-around I thought of would be to have a batch file (instead of
> > > text file) set the variables to environment variables such that other
> > > scripts could use them.
>
> > > Ideally, I'd like to be able to have a simple text file to be used to
> > > hold the configuration data and then source that into my batch script.
>
> > > Any help would be greatly appreciated. Thank you.
>
> > > Regards,
>
> > > Josh
>
> > If you were to use a simple file with two bits of information on each
> > line, thus ...
>
> > varname=Sometext to define variable content
>
> > Then this would do the job ...
>
> >   for /f "tokens=1,2 delims==" %%a in (yourfilespec) do call :sub %%a
> > "%%b"
> >   goto :EOF
>
> >  :sub
> >   set %1=%~2
>
> > The variables would only be set for that particular console session.
>
> > Type FOR/? at a command prompt for more information.
>
> > Tom Lavedas
> > =============
> >http://members.cox.net/tglbatch/wsh/

You'd do that something like this ...

sArguments ="LDAP://localhost:389/ou=Accounting,dc=fabrikam,dc=com"
Set objOU = GetObject(sArguments)
With Createobject("Scripting.FileSystemObject")
  aNames = split(.opentextfile(sFilename,1).readall, vbnewline)
end with
for each sName in aNames
  objOU.Delete "user", "cn=" & sName
next

where the names were one per line.

Tom Lavedas
=============
http://members.cox.net/tglbatch/wsh/
Author
2 Mar 2007 5:36 PM
MMC-Michelle
Thank you for your help.  I'm getting an "invalid procedure call or argument"
on the 4th line (aNames = split(.opentextfiles....).  I've been trying to
figure out how to change it to make it work by doing a lot of Internet
searches, but my knowledge of VBS is extremely limited.  Where, or how, do I
call the text file it will be getting values from?  Do I put the name of it
somewhere in that code?  Or is it called via command line when I execute the
script?



Show quote
"Tom Lavedas" wrote:

> On Mar 1, 11:55 am, MMC-Michelle
> <MMCMiche***@discussions.microsoft.com> wrote:
> > I would like to do this but with a VBS script for ADAM rather than a batch
> > file.  I have a working script for deleting a single users.    How would I
> > modify that to have it call a list of users to delete?  My script looks just
> > like the Delete Users example listed here:http://www.microsoft.com/technet/scriptcenter/scripts/ds/adam/modify/...only modified for my system.
> >
> > Thanks.
> >
> > "Tom Lavedas" wrote:
> > > On Feb 26, 3:48 pm, mrbo***@gmail.com wrote:
> > > > Hi,
> >
> > > > I'm trying to be able to source in variables into my batch script from
> > > > a text file.  I know how to do this in a shell script, but I'm not
> > > > sure how to do it in a Windows batch script.  Is this even possible?
> > > > The work-around I thought of would be to have a batch file (instead of
> > > > text file) set the variables to environment variables such that other
> > > > scripts could use them.
> >
> > > > Ideally, I'd like to be able to have a simple text file to be used to
> > > > hold the configuration data and then source that into my batch script.
> >
> > > > Any help would be greatly appreciated. Thank you.
> >
> > > > Regards,
> >
> > > > Josh
> >
> > > If you were to use a simple file with two bits of information on each
> > > line, thus ...
> >
> > > varname=Sometext to define variable content
> >
> > > Then this would do the job ...
> >
> > >   for /f "tokens=1,2 delims==" %%a in (yourfilespec) do call :sub %%a
> > > "%%b"
> > >   goto :EOF
> >
> > >  :sub
> > >   set %1=%~2
> >
> > > The variables would only be set for that particular console session.
> >
> > > Type FOR/? at a command prompt for more information.
> >
> > > Tom Lavedas
> > > =============
> > >http://members.cox.net/tglbatch/wsh/
>
> You'd do that something like this ...
>
> sArguments ="LDAP://localhost:389/ou=Accounting,dc=fabrikam,dc=com"
> Set objOU = GetObject(sArguments)
> With Createobject("Scripting.FileSystemObject")
>   aNames = split(.opentextfile(sFilename,1).readall, vbnewline)
> end with
> for each sName in aNames
>   objOU.Delete "user", "cn=" & sName
> next
>
> where the names were one per line.
>
> Tom Lavedas
> =============
> http://members.cox.net/tglbatch/wsh/
>
>
Author
2 Mar 2007 11:46 PM
Tom Lavedas
On Mar 2, 12:36 pm, MMC-Michelle
<MMCMiche***@discussions.microsoft.com> wrote:
> Thank you for your help.  I'm getting an "invalid procedure call or argument"
> on the 4th line (aNames = split(.opentextfiles....).  I've been trying to
> figure out how to change it to make it work by doing a lot of Internet
> searches, but my knowledge of VBS is extremely limited.  Where, or how, do I
> call the text file it will be getting values from?  Do I put the name of it
> somewhere in that code?  Or is it called via command line when I execute the
> script?

I suspect you failed to realize the meaning of the sFilename variable
I used in the offending line.  That is, you will need to add a line at
the beginning of the script to define that variable's contents.  For
example, ...

sFilename = "C:\SomeFolder\Somefilename.txt"

sArguments ="LDAP://localhost:389/ou=Accounting,dc=fabrikam,dc=com"
Set objOU = GetObject(sArguments)
With Createobject("Scripting.FileSystemObject")
  aNames = split(.opentextfile(sFilename,1).readall, vbnewline)
end with
for each sName in aNames
  objOU.Delete "user", "cn=" & sName
next

Tom Lavedas
=============
http://members.cox.net/tglbatch/wsh/
Author
5 Mar 2007 4:35 PM
MMC-Michelle
Perfect!  Thank you so much!

Michelle


Show quote
"Tom Lavedas" wrote:

> On Mar 2, 12:36 pm, MMC-Michelle
> <MMCMiche***@discussions.microsoft.com> wrote:
> > Thank you for your help.  I'm getting an "invalid procedure call or argument"
> > on the 4th line (aNames = split(.opentextfiles....).  I've been trying to
> > figure out how to change it to make it work by doing a lot of Internet
> > searches, but my knowledge of VBS is extremely limited.  Where, or how, do I
> > call the text file it will be getting values from?  Do I put the name of it
> > somewhere in that code?  Or is it called via command line when I execute the
> > script?
>
> I suspect you failed to realize the meaning of the sFilename variable
> I used in the offending line.  That is, you will need to add a line at
> the beginning of the script to define that variable's contents.  For
> example, ...
>
> sFilename = "C:\SomeFolder\Somefilename.txt"
>
> sArguments ="LDAP://localhost:389/ou=Accounting,dc=fabrikam,dc=com"
> Set objOU = GetObject(sArguments)
> With Createobject("Scripting.FileSystemObject")
>   aNames = split(.opentextfile(sFilename,1).readall, vbnewline)
> end with
> for each sName in aNames
>   objOU.Delete "user", "cn=" & sName
> next
>
> Tom Lavedas
> =============
> http://members.cox.net/tglbatch/wsh/
>
>
>

AddThis Social Bookmark Button