Using variables from two text files in the same command string

B

bladeraptor

Hi

I am trying to write a batch for NT / 2000 that does two things. The
first is to make a directory based on variables in a text file that
represent a group and a user. The second is then to allocate the user
permissions for that folder

Once the new set of directories containing a folder for the group and
a sub folder for the user has been created, I need to allocate
permissions for each of the user identities culled from the first file
to the new user subfolder.

I had hoped to use XCACLS.exe to allocate permissions for the user
listed in the text file to the newly created user folder.

Is it possible to append a text file with the result of its action.
I.e the text file provides the names for the new directory structure,
could you then append the original file with the path it creates.

So the original text file containing a group name and a user name
separated by a comma would then be appended on each line by the new
path:

group,user
engineering,tbob

creates directory c:\archive\engineering\bob - this path is then
appended to the text file so that the text file now reads:

group,user,path
engineering,tbob,c:\archive\engineering\bob

I am not sure how to do this so tried another solution - which I
haven't managed to get to work, which is to run the string that
creates the directory structure based on the group and folder in the
text file to create the directory and subfolders in reality the first
time and then to run it again a second time but rather than using
MKDIR used echo to output the resutls to a text file.

I was hoping to use the record of these new directories in this second
text file as a second variable in the XCACLS string to allocate
permissions.

But the question is is it possible to use tokens from two separate
text files?

If not is there any other way to pull either of the variables - the
user name variable from the first text file or the file path variable
from the second path into memory to apply it to the XCACLS string

Any help would be much appreciated as I am a real newbie to this

Regards

Bladeraptor

@echo off & setlocal EnableExtensions DisableDelayedExpansion
FOR /F "tokens=1,2 delims=," %%i IN (test.txt) DO MKDIR
C:\test\Archive\%%i\%%j
FOR /F "tokens=1,2 delims=," %%i IN (test.txt) DO ECHO
C:\test\Archive\%%i\%%j >>test2.txt
FOR /F "tokens=1" %%i IN (test.txt) "tokens=1" %%l IN (test.2.txt) DO
XCACLS %%l /T /E /P %%i:RWD
 
P

Paul R. Sadowski

Why can't you do something like this (in psuedo code), grouping the
operations:

Read in each line of text file
create dir
create a new text file with new entry for each user/dir
apply permisions
loop to next line
When done del the original text file and ren the new text file to the old
name

Without even reviewing your sample code, I mean something like this:
FOR /F "tokens=1,2 delims=," %%i IN (test.txt) DO (
MKDIR C:\test\Archive\%%i\%%j
ECHO C:\test\Archive\%%i\%%j >>test.bak.txt
XCACLS %%l /T /E /P %%i:RWD
)
del test.txt
ren test.bak.txt test.txt

Like I said I haven't checked for errors I'm just illustrating how one might
handle the problem.
 
C

Clyde D'Souza

This seems to work.
FOR /F "tokens=1,2 delims=," %%i IN (testb1.txt) DO MKDIR
C:\test\Archive\%%i\%%j
FOR /F "tokens=1,2 delims=," %%i IN (testb1.txt) DO ECHO %%
i,%%j,C:\test\Archive\%%i\%%j >>testb2.txt
FOR /F "tokens=1,2,3 delims=," %%i IN (testb2.txt) DO
XCACLS %%k /T /E /P %%i:RWD
 
D

David Wang [Msft]

Now bladeraptor, can you explain why are you trying to use two text files
and fabricating language syntax ?


Psuedo Code:
For every group,user
If C:\test\Archive\group\user does not exist, create
C:\test\Archive\group\user
Call XCACLS on C:\test\Archive\group\user giving user credentials
Next

Batch script:
--> translate Pseudo Code logic into syntax

SET DIR_ROOT=C:\test\Archive
FOR /F "tokens=1,2 delims=," %%i IN (test.txt) DO (
IF NOT EXIST %DIR_ROOT%\%%i\%%j MKDIR %DIR_ROOT%\%%i\%%j
XCACLS %DIR_ROOT%\%%i\%%j /T /E /P %%j:RWD
)

--> Another way to do it, per your original description

SET DIR_ROOT=C:\test\Archive
FOR /F "tokens=1,2 delims=," %%i IN (test.txt) DO (
IF NOT EXIST %DIR_ROOT%\%%i\%%j MKDIR %DIR_ROOT%\%%i\%%j
ECHO %%i,%%j,%DIR_ROOT%\%%i\%%j >> test2.txt
)

FOR /F "tokens=2,3 delims=," %%i IN (test2.txt) DO (
XCACLS %%j /T /E /P %%i:RWD
)

IF EXIST test2.txt DEL test2.txt > NUL

--> And there's probably a gazillion other ways to express the equivalent
result.



Now, please forgive me for thinking this thought out-loud, but I've been
observing a couple of threads on this newsgroup lately, and I have to say
that as flattering a self-proclaimed newbie may be, I have to make some
suggestions.

If you are serious, I highly suggest that you take an intro course to
programming logic (don't worry too much about the language/syntax; focus on
learning how to logically express and break-down the steps necessary to do
something, and realize that there may be zero or more solutions).

If this is the first time you've programmed batch scripting, I apologize for
the experience since it is probably not the best first experience due to
arcane syntax/behavior (wait until you work more with the FOR construct...)

It's going to be tough and take a lot of determination on your behalf, but I
really want you to try hard at reading necessary documentation for the
language (feel free to ask for it; even better, try to search for it),
including syntax, experiment a little (this is the best way to learn --
asking questions is the fast way to getting an answer, not necessarily
learn), and please present all the alternatives you've tried and why you
think they failed before asking a question so that you can help people
accelerate your development.

--
//David
IIS
This posting is provided "AS IS" with no warranties, and confers no rights.
//
Hi

I am trying to write a batch for NT / 2000 that does two things. The
first is to make a directory based on variables in a text file that
represent a group and a user. The second is then to allocate the user
permissions for that folder

Once the new set of directories containing a folder for the group and
a sub folder for the user has been created, I need to allocate
permissions for each of the user identities culled from the first file
to the new user subfolder.

I had hoped to use XCACLS.exe to allocate permissions for the user
listed in the text file to the newly created user folder.

Is it possible to append a text file with the result of its action.
I.e the text file provides the names for the new directory structure,
could you then append the original file with the path it creates.

So the original text file containing a group name and a user name
separated by a comma would then be appended on each line by the new
path:

group,user
engineering,tbob

creates directory c:\archive\engineering\bob - this path is then
appended to the text file so that the text file now reads:

group,user,path
engineering,tbob,c:\archive\engineering\bob

I am not sure how to do this so tried another solution - which I
haven't managed to get to work, which is to run the string that
creates the directory structure based on the group and folder in the
text file to create the directory and subfolders in reality the first
time and then to run it again a second time but rather than using
MKDIR used echo to output the resutls to a text file.

I was hoping to use the record of these new directories in this second
text file as a second variable in the XCACLS string to allocate
permissions.

But the question is is it possible to use tokens from two separate
text files?

If not is there any other way to pull either of the variables - the
user name variable from the first text file or the file path variable
from the second path into memory to apply it to the XCACLS string

Any help would be much appreciated as I am a real newbie to this

Regards

Bladeraptor

@echo off & setlocal EnableExtensions DisableDelayedExpansion
FOR /F "tokens=1,2 delims=," %%i IN (test.txt) DO MKDIR
C:\test\Archive\%%i\%%j
FOR /F "tokens=1,2 delims=," %%i IN (test.txt) DO ECHO
C:\test\Archive\%%i\%%j >>test2.txt
FOR /F "tokens=1" %%i IN (test.txt) "tokens=1" %%l IN (test.2.txt) DO
XCACLS %%l /T /E /P %%i:RWD
 
B

bladeraptor

Dear Mr Wang,

Thank you for you reworking of the code to present an elegant and
working alternative solution to my problem.

I also apprecited the time you took to express your views on the
necessary initiation that any new coder should undergo to ensure that
they begin the vital process of learning and not clutter the baord
with elementary and erroneous examples of mongrel code.

I appreciate your perspective. However, in response I would like to
bring your attention to a couple of points. I am not a coder or
programmer. I am a systems asdministrator. I envy and respect
programmers for the knowledge they have to be able to get machines to
automate and simplify alot of the tasks I routinely must carry out.

But these tasks are a subset of my work load and while they make it
easier, I cannot afford to devote all my attention to it. Now that is
not to say that I intend to milk the brains of those who have done the
graft. I am currently spending nights and weekends learning this stuff
and starting from a very low base - but what enlightened elders in the
community often forget is that this is easy for those with a
background in some language as the variables, syntax etc is fairly
transparent and they understand the logic behind it. For people like
me it is complete gobbledegook and there is a steep learning curve
involved and frankly the resources to do that learning in a
progressive and clearly defined way are not really 'easily findable'
out there or plentiful, well written or accessible by the newbie.

I have been studying various bits of eductional material here and
there where I can find it and understand it - so rather than
pontificating on examples of psuedo code - whateve that is - perhaps a
bit more of a relaxed, welcoming attitude to new entrants to the field
accompanied by a general FAQ would be helpful.

Kidnap a code newbie one day and get them to begin the process of
educating themselves without access to the thought pattenrns and
resources you take for granted and maybe you will realise that it is
not that easy to make a start in this area.

The board provides a peerless resource of ideas and help and I could
not have begun to get where I am today without the help, so that is
greatly appreciated, but please take into account that that these
pieces of psueo code were an iteration of that learning process - so
rather than trashing my homework - how about marking it and then
commenting sensibly on how improvements can be achieved.

Regards

Bladeraptor
 
B

bladeraptor

I wrote a post a few hours ago which is perhaps open to a degree of
negative interpretation. If that is the case I apologise. I must admit
to have been slightly frustrated by your earlier comments re learning
and felt compelled to vent my spleen a bit.

I have now been working for several hours with the revoke section of
XCACLS.exe and finally got it working. Maybe I am just stupid but all
the documentation I have seen does not indicate the getting the revoke
(/R) switch to work properly is very difficult unless you use it in
conjunction with the /E switch. My point is what - well just that it
is easy to underestimate the learning curve and thinking that is
required to begin to get a handle on this very simple coding.

With the unstinting help of people like Mathias I have learned a huge
amount in the last couple of weeks and begun to be able to tackle some
real business needs in our user environment. I am sorry if I offend
you with my ignorance or slightly different way of conceptualising the
solution to a problem, but I are learning, and am frankly not the sort
of person who learns by reading a book but by doing something in
actuality and then figuring out why something worked.

I greatly appreciate your solution to my problem it is very elegant
but again points to the difference between you and me. You 'speak' VB
like a native and say what you mean in the shortest most correct way.
I am like a foreigner learning a new language and yes my questions may
seem asinine and my grammar appalling but that is because I do not
have the vocabularly to express what I mean. Furthermore - you can
seamlessly translate a real business requirement intantly into code in
one stage - for people at my stage we first have to abstract it - i.e.
psuedo code before we can pick it apart and build it up from the
individual components into some form of code syntax. I was thinking of
using two documents because i did not have the vocabularly to know
that i could hold the directory as a variable and then apply the other
components from the text file in the same process, without needing to
output first.

I hope you continue to feel happy to offer your expertise and advice,
because despite what you may believe, I for one, certainly do not just
copy your code into a batch files and get on with my day job - I work
my tail off to understand it and tweak it.

Regards

Bladeraptor
 
B

bladeraptor

Hi Clyde

Thanks for your suggestion - I admit to being a bit embarrassed for
not seeing the simple next step of logic - you took

Regards

Bladweraptor
 
A

Al Dunbar [MS-MVP]

bladeraptor said:
I wrote a post a few hours ago which is perhaps open to a degree of
negative interpretation. If that is the case I apologise. I must admit
to have been slightly frustrated by your earlier comments re learning
and felt compelled to vent my spleen a bit.

And this post makes it two such that are open to a negative interpretation.

You have successfully intuited that there is a foundation of common
experience and understanding behind the language used by the non-newbie.
While frustrating for you, it is similarly frustrating for those that are
trying to help you. Some of us do a better job than others in bridging the
"language gap", so to speak. But please realize that there are precious few
shortcuts to the knowledge and ability you seek.

David's last reply gave a number of very polite and insightful suggestions
that were, IMHO, intended to give you some useful directions to follow.
Perhaps his language sounded condescending to you, but it did not seem so to
me.

/Al
 
D

David Wang [Msft]

I must apologize for getting you upset. My brain can be a bit of a loose
cannon at times.

I will absolutely continue to offer any useful advice (unless someone beats
me to it...). I understand how it can seem so non-obvious, stressful, and
frustrating -- we've all been there at one point. It's a part of the reason
why I answer questions -- I want to help people with their problems,
especially if I have some knowledge that can bridge the gap.
Batch/commandline scripting can be quite non-trivial since the "script
language" is rather arcane, and frequently, the "useful" commands are
non-intuitive. It's not the Garden of Eden, shall we say... ;-)

That said, I am the sort of person that prefers to teach a man to fish
instead of handing him a fish -- because the former is an human advancement
while the latter is just social welfare. I admire your resiliancy and
determination, and I hope that you will continue to cast the fishing line...

--
//David
IIS
This posting is provided "AS IS" with no warranties, and confers no rights.
//
I wrote a post a few hours ago which is perhaps open to a degree of
negative interpretation. If that is the case I apologise. I must admit
to have been slightly frustrated by your earlier comments re learning
and felt compelled to vent my spleen a bit.

I have now been working for several hours with the revoke section of
XCACLS.exe and finally got it working. Maybe I am just stupid but all
the documentation I have seen does not indicate the getting the revoke
(/R) switch to work properly is very difficult unless you use it in
conjunction with the /E switch. My point is what - well just that it
is easy to underestimate the learning curve and thinking that is
required to begin to get a handle on this very simple coding.

With the unstinting help of people like Mathias I have learned a huge
amount in the last couple of weeks and begun to be able to tackle some
real business needs in our user environment. I am sorry if I offend
you with my ignorance or slightly different way of conceptualising the
solution to a problem, but I are learning, and am frankly not the sort
of person who learns by reading a book but by doing something in
actuality and then figuring out why something worked.

I greatly appreciate your solution to my problem it is very elegant
but again points to the difference between you and me. You 'speak' VB
like a native and say what you mean in the shortest most correct way.
I am like a foreigner learning a new language and yes my questions may
seem asinine and my grammar appalling but that is because I do not
have the vocabularly to express what I mean. Furthermore - you can
seamlessly translate a real business requirement intantly into code in
one stage - for people at my stage we first have to abstract it - i.e.
psuedo code before we can pick it apart and build it up from the
individual components into some form of code syntax. I was thinking of
using two documents because i did not have the vocabularly to know
that i could hold the directory as a variable and then apply the other
components from the text file in the same process, without needing to
output first.

I hope you continue to feel happy to offer your expertise and advice,
because despite what you may believe, I for one, certainly do not just
copy your code into a batch files and get on with my day job - I work
my tail off to understand it and tweak it.

Regards

Bladeraptor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top