Question Locked

Return any character positions within a string

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a .bat file that has the following links with network passwords.

net use u: \\Myserver\Folder45 Cyrus24 /User:NA\cyrus.t.andrews
net use v: \\Myserver\Folder3645 Cyrus24 /User:NA\cyrus.t.andrews
net use x: \\Myserver\Folder32645 Cyrus24 /User:NA\cyrus.t.andrews
net use y: \\Myserver\Folder995 Cyrus24 /User:NA\cyrus.t.andrews

I want the code to pull the password "Cyrus24" so that it can be replaced. I want to be able to do this every 30 days when the password expires. Problem is I have to define the string exactly in order to change the characters I want to change. How can I get the code to pull any 8 characters that proceed "^/User:NA\cyrus.t.andrews"?

Here is what I have tried:

'--get text to replace and new text.
s1 = inputbox ("Enter text to be replaced.", "What to replace?", Left(Cyrus24 /User:NA\cyrus.t.andrews ",8))

5 replies

by grassiap

that hurts to see a password listed plain text in a file.

anyway since you're already doing so why don't you just start you bat file with an variable and call this variable in your net uses ?
when you want you'll just have to discard the first line and replace it, once.

the bat file would look like :

set MONTHLYPWD Cyrus24
net use u: \\Myserver\Folder45 %MONTHLYPWD% /User:NA\cyrus.t.andrews
net use v: \\Myserver\Folder3645 %MONTHLYPWD% /User:NA\cyrus.t.andrews
net use x: \\Myserver\Folder32645 %MONTHLYPWD% /User:NA\cyrus.t.andrews
net use y: \\Myserver\Folder995 %MONTHLYPWD% /User:NA\cyrus.t.andrews

one (only a little bit) better level would be to have the variable an environment variable stored in the registry, therefore you do not have to change the bat file, just change the registry value.

HTH
Philippe

by kaptandrews

OK...new to VB. Ihave the following script that i will use to change the password.I want the first inbox to pull the password from the .bat, the second inbox will prompt the user to change it.

'--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")
set MONTHLYPWD

'--get filename and open it. read all text into a string var.
r = inputbox("NonReg.bat", "Change NA Domain Password", "C:\java\New Folder\Text Ops\TestNonReg.bat")
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("Old Password", "Change NA Domain Password", MONTHLYPWD)
s2 = inputbox("New Password?", "Change NA Domain Password")

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

Having problems with the S1 line and getting it to return , just one instance of the, the password in the box. I can do it once but once the string is defined in the S1 line, I am unable to change the password. It retains the old password from the defined string and the program is a one time deal. I don't want the user to have to remember the password is what I am getting at.

by danlm

You can define variables within your .bat file, which is what I beleive he was showing you. The variable being defined at the very top and then being referenced elsewhere in the bat file where it is needed.

If you still want to use your vb script, the coding would become alot easier if you used this approach. You could tell your script to only look at the first line, and your string extraction/replacement wouldn't change. It would be in one fixed place. I don't know vb, but I have string manipulation in perl and other languages...

If you don't like this solution, regular expresions could be used for the replacement of the password.

If it was perl, the syntax for a regular expresion would be something like:

Read the full bat file into a string.
substitue the password with:
$stringvariable ~= s/Cyrus24// g

Write out the string to the bat file.

The first posters response though is the fastest, easiest, and probably the cleanest way though. No external applications would be needed.

Dan

Dan

by kaptandrews

I was not sure how to make the first messagebox pull the last six characters of a line. (Regardless what the characters are.)

Here is what I was doing?.

s1 = inputbox("Old Password", "Change NA Domain Password", Left("set MONTHLYPWD Cyrus24", 8)

but when the password was changed from the first run of the script, the string stayed the same in the program. When you run is a second time, it would pull Cyrus24 (whats defined in the string) and not the new password within the BAT.

Don't know enough about VB to get it to pull any character?from any line? be it at the beginning, middle or end w/o defining a string. I am sure this can be done. Just not smart enough ? yet!

This discussion is closed to new replies.