问题
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
回答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
回答2:
for /f "tokens=1,2 delims=,." %%i in ("WHID-PRT-MFP-3, NORTH EAST OFFICE.bat") do set newname=%%j,%%i.bat
来源:https://stackoverflow.com/questions/31654312/split-own-filename-and-assign-specific-parts-to-variables-in-a-batch-script