0

thanks in advance for any help offered.

I have a batch file from which name i want to use parts and assign them as variables. Using the for /f with delims i was able to do something like it but i have to rename the file to a different scheme than what i need. Se here is what i have:

-Current filename: WHID-PRT-MFP-3, NORTH EAST OFFICE.bat

SET getname=%~n0

for /f "tokens=1 delims=-" %%G IN ("%getname%") DO (SET siteid=%%G)

for /f "tokens=1 delims=," %%E IN ("%getname%") DO (SET name=%%E) 

SET printername=%name%

SET servername=Server-%siteid%-01

OK, so that seems to work ok and im able to get the parts i need from the file name and assign them to variable so that %name%=WHID-PRT-MFP-3 and %siteid%=WHID

Now my dilema is that i need the file name to be: NORTH EAST OFFICE, WHID-PRT-MFP-3.bat So I've been trying to get the entire line behind the comma(WHID-PRT-MFP-3) to a variable and the first item in dashes(WHID) to another variable, same as the example above, but cant figure it out. This will be used with several names so not only as the example shown i could have shorter or longer names for both parts, say: WHID-PRT-1, OFFICE 1

Any ideas are welcome.

Thanks

2 Answers 2

1

NORTH EAST OFFICE, WHID-PRT-MFP-3.bat:

SET getname=%~n0

rem split at comma
for /f "tokens=2 delims=," %%E IN ("%getname%") DO (

    rem trim space after comma
    for /f "tokens=*" %%S IN ("%%E") DO SET name=%%S

    rem get word before hyphen and strip leading space
    for /f "tokens=1 delims=- " %%S IN ("%%E") DO set siteid=%%S
)

SET printername=%name%
SET servername=Server-%siteid%-01
Sign up to request clarification or add additional context in comments.

1 Comment

You sir/mam rule!!! Great answer exactly what i needed. 2 birds 1 stone actually... i didnt know i could nest for /f statements to keep splitting :)
0
for /f  "tokens=1,2 delims=,." %%i in ("WHID-PRT-MFP-3, NORTH EAST OFFICE.bat") do set newname=%%j,%%i.bat

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.