Avatar of jskfan
jskfan
Flag for Cyprus asked on

Change String with Windows Batch Script

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
Windows BatchWindows OS

Avatar of undefined
Last Comment
jskfan

8/22/2022 - Mon
NVIT

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

ASKER
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.
NVIT

> 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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
jskfan

ASKER
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
NVIT

> 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?
jskfan

ASKER
in the Text file C:\test.txt

aaaaaaaa                  bbbbbbbbbbb        ccccccccccc   -username  username

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Qlemo

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.
jskfan

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

Open in new window


Everything but the username stays the same?
 : Yes
ASKER CERTIFIED SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jskfan

ASKER
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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
jskfan

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

ASKER
I will see if I can get it to work later.

Thank you