When popular support to your army is zero and everybody in the country feel invaded the only way to achieve "victory" is total genocide. And this isn't possible in this situtation.
The mistake of Napoleon (and most French of his time) was to think that the Spaniards would be happy to be free from the stupid Bourbons, but Spain in 1808 wasn't the same that France in 1789. The only way to achieve victory in the Peninsular War was just to avoid the war itself. Napoleon should have avoided the occupation of the Spanish cities, kept Carlos IV on the throne or gave the crown to Fernando as a French puppet, and then he would have both the French and Spanish armys free to invade Portugal, completing the isolation of Britain.
Mmm.
I think we're mapping the problems of guerilla warfare in the modern era onto that of Spain.
Napoleon had seen guerillas before; in the Vendee, in Sicily, etc. The French had beaten them, why would Spain be different? And he had support from liberals (and some conservatives in Spain) at the beginning.
The real problem was that the British army was in Spain, and that Spain was too large. Remove the British, and Spain, while still a quagmire, isn't the fiasco it was in OTL.
I agree that keeping Carlos or Ferdinand on the throne would've been the best idea.