Environment variables can be tricky to set sometimes
Try naming the filenames explicitly to see if that's where the fault is.
Also don't forget that spaces are not allowed in DOS filenames, so if the first part tries to create a cap file with a space in it, that wont work. So perhaps what's needed is "%filename%".cap