We help IT Professionals succeed at work.

Change String with Windows Batch  Script

jskfan
jskfan used Ask the Experts™
on
Change String with Windows Batch  Script

I have a CSV file that has computer names, usernames
example

computername, Newusername 
comp1,  John
CompX, Alton
....

Open in new window



in each computer C: drive there is a text file (C:\test.txt) that has  a line like this :
aaaaaaaa                  bbbbbbbbbbb        ccccccccccc   -username    username

Open in new window


I want the second username string to be replaced by a string read from the CSV file under usernames ... Note the first username has a dash in front of it and it should stay as it is -username, I need just the second username to be changed.

for instance:
 on comp1 , the line will be
aaaaaaaa                  bbbbbbbbbbb        ccccccccccc   -username    John

Open in new window


 on compX , the line will be
aaaaaaaa                  bbbbbbbbbbb        ccccccccccc   -username    Alton

Open in new window




Thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NVITEnd-user support

Commented:
Do you need to read the c:\test.txt file on multiple remote computers to get the aaaaaaaa string?

Author

Commented:
No ...each computer c:\test.txt has the same string :
aaaaaaaa                  bbbbbbbbbbb        ccccccccccc   -username  username

Open in new window


I want the second username to be replaced by the Newusername specified in the CSV file.
NVITEnd-user support

Commented:
> I want the second username to be replaced by the Newusername specified in the CSV file

Where is this 'second username'? I don't see it in your post

Author

Commented:
each computer has in its C:\test.txt, a line as follows:

aaaaaaaa                  bbbbbbbbbbb        ccccccccccc   -username    username

Open in new window



you can see username twice the first one is preceded by a dash "-" that one stays as is. The second username needs to be replaced
NVITEnd-user support

Commented:
> you can see username twice the first one is preceded by a dash "-" that one stays as is. The second username needs to be replaced

I don't see it. Is it in the csv or txt file?

Author

Commented:
in the Text file C:\test.txt

aaaaaaaa                  bbbbbbbbbbb        ccccccccccc   -username  username

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
To clarify - there is one parameter (literal string) "-username" and a uersname (which is different on each PC). The non-literal username needs to be replaced.

I cannot see it on mobile: Is the CSV file tab delimited?
Everything but the username stays the same? Because it is much more simple to just write a string to remote than to replace a value.

Author

Commented:
CSV is comma delimited ..
computername, Newusername 
comp1,  John
CompX, Alton

Open in new window


Everything but the username stays the same?
 : Yes
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
With CSV file I meant C:\test.txt ;-).
@echo off
for /f "skip=1 tokens=1,2 delims=, " %%A in ('C:\Temp\EE\usernames.csv') do (
  > \\%%A\c$\test.txt echo aaaaaaaa                  bbbbbbbbbbb        ccccccccccc   -username  %%B
)

Open in new window

(assuming you run this with an admin account to allow to log in from network for all those PCs)

Author

Commented:
Qlemo,


Sorry you can name it whatever name you want. the CSV will be like this:
computername, Newusername 
comp1,  John
CompX, Alton
....

Open in new window


I do not think you need csv and txt files at the same time
CSV already has computer name that the script will access and the username that will be changed.
example : the script will access Comp1 and changethe second "username" string to John ..

aaaaaaaa                  bbbbbbbbbbb        ccccccccccc   -username    username

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Did You try my last suggestion?

Author

Commented:
I just tried it, the script opens up the CSV file, it does not replace the username on the targeted computers.

Author

Commented:
I will see if I can get it to work later.

Thank you