There's several ways you could automate this out.
The fastest way would probably be to use the API to log into each device, and set the host name.
If you're not comfortable around the API, but can create one config file per device named after, for example, the only IP address on an interface named 'outside', you could paste something like this:
:local server [:resolve "your.webserver.com"]; :local url ([:pick [/ip address get [/ip address find interface=outside] address] 0 [:find [/ip address get [/ip address find interface=outside] address] "/"]] . ".txt"); :local fileName "hostnamescript"; /tool fetch mode=http address=$server src-path=$url dst-path=$fileName; /import hostnamescript; /file remove hostnamescript
into each device. On a router with 1.1.1.1/24 on interface 'outside', that would attempt to download
http://your.webserver.com/1.1.1.1.txt to "hostnamescript", execute that file and delete it.
1.1.1.1.txt would then be a script file along the lines of
/system identity set "whatever"
Again, the API would be faster and more resilient to errors.