Link to home
Create AccountLog in
Avatar of jskfan
jskfanFlag 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
Avatar of NVIT
NVIT
Flag of United States of America image

Do you need to read the c:\test.txt file on multiple remote computers to get the aaaaaaaa string?
Avatar of 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.
> 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
Avatar of 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
> 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?
Avatar of jskfan

ASKER

in the Text file C:\test.txt

aaaaaaaa                  bbbbbbbbbbb        ccccccccccc   -username  username

Open in new window

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.
Avatar of 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
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of 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

SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of jskfan

ASKER

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

ASKER

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

Thank you