Batch script to copy your backups from cd/dvd optical disks to a hard disk/nas




4/14/2010. I recently decided to drop the habit of backing up all data to optical disks and use a 2 terabyte external hard disk instead. Not exactly cheaper, but what you lose in money (just once) you gain tenfolds in speed, comfort and ease of use, and physical space needed for storage. So I got a 2TB hard drive and fitted it in a USB external box.
Backing up all the new stuff there is no big deal, but it surely was to move the contents of the hundreds of “old” CD/DVD’s in there; doing it manually by dragging files onto the drive was going to be the most cumbersome thing, so I thought that if I had a utility that would let me put a disk inside the cd/dvd drive, and did it all by itself, that would be great. Also this utility had to eject the disk when done, so all I had to do was change it with the next disk in the serie, and after reinserting the tray the program would go on copying files.
In other words, all I wanted to do was to take out the just copied CD/DVD and put inside the next one without even touching a file manager but keep doing whatever else it was that I was doing (writing this howto for example, while every few minutes I reach to the disks stockpile and exchange the disk in the drive). Obvously, abiding by Murphy’s law, there was no such thing, or at least a hour worth of search didn’t bring up anything. Either I went full-manual or I made something like it myself: the time I was going to spend on the development would surely have been way less than the time I would have needed to select files, drag them to the drive, then wait for it to finish to change disks.

My choice fell onto AutoHotKey as the scripting tool (I already knew a little of it, plus it had just the functions I needed), so I invested some more than an hour into studying the language and building up from scratch the code to do exactly what I needed.
And in short, here it is (do not copy it yet, you need to read explanation below):

while 1
{
	empty = 1

	while empty
	{
		DriveGet, status, StatusCD, E:

		if (status = "stopped" OR status = "not ready")
			empty = 0
		
		Sleep, 500
	}


	SetWorkingDir, E:\

	Loop, *.*, 2, 1
		FileCreateDir, M:\%A_LoopFileFullPath%
	
	Loop, *.*, , 1
	{
		if (A_LoopFileName != "filename1.ext"
		&& A_LoopFileName != "filename2.ext"
		&& A_LoopFileName != "filename3.ext")
		{
			docopy = 0
			IfExist, M:\%A_LoopFileFullPath%
			{
				MsgBox, 4, Overwrite file?, Overwrite file %A_LoopFileFullPath%?
				IfMsgBox, Yes
					docopy = 1
			}
			else
				docopy = 1
			if docopy = 1
				FileCopy, %A_LoopFileLongPath%, M:\%A_LoopFileFullPath%, 1
			if ErrorLevel
				MsgBox, Could not copy "%A_LoopFileLongPath%" to "M:\%A_LoopFileFullPath%" with ErrorLevel = %ErrorLevel%.
			ErrorLevel = 0
		}
	}

	Drive, Eject, E:
	
	SetWorkingDir, C:\
}
	

As I said before, do not put it in action yet, you’re very likely to need to edit it to your needs.
Replace EVERY occurence of “E:” with the correct drive letter of the cd/dvd drive you’re going to use.
Replace EVERY occurence of “M:” with the correct drive letter of the hard drive you want to copy the files to.
The part with the “filenameX.ext” is for my convenience, and probably yours as well: I have some files on most optical disks that do not need to be copied over to the hard disk, mostly being a catalog file, or maybe the exe of the player for the videos, or whatever. So in place of “filenameX.ext” you need to put the name of those files (if any) that you want the program to ignore (if you need more of them, just duplicate the central line as needed, if you need less, or none at all, either edit that part to your needs, or if you’re not good at it, you can probably leave it as it is, since I do not think there are any files in your collection that are actually named “filenameX.ext”). The “SetWorkingDir, C:\” command is just a temporary buffer for when the disk is ejected, you can leave it as long as you have a C: drive!

Save the code in text format with .ahk extension, install AutoHotKey, and double-click on the .ahk file you just created; if I receive enough requests, I’ll upload an exe version of the utility so you can skip installing autohotkey. This is how the script works: when you launch it, unless it detects the presence of a disk inside the CDrom/DVDrom drive, it does nothing; as soon as you put a cd inside, it proceeds to copy all the files that have names different from the ignored ones, to the chosen destination, respecting the folder structure. If there are any files that already exist on the destination drive, it asks you if you want to overwrite them; there’s a little problem here, I have set the filecopy to do an overwrite, but it won’t work, if I try and overwrite it will just return an error. It’s not a big deal for me as I can just press OK and the program will go on by itself without copying the file, but if you really need to do an overwrite (for example a newer version of the same file) first delete the existing file, then press OK and it should work. As soon as the whole optical disk has been processed, the script will eject the tray of your drive, and pause until it detects again a disk inside, so you have all the time you want to switch disks. As soon as you put a new disk and close the tray, the program will continue the file copy process, in a batch fashion. To close it right click in the green “H” icon that appears in the system tray and press Exit.

I tested it on my pc alone, so I won’t be giving support if it doesn’t work for you… no time for that! I just thought it would have been nice to share something like it in case someone else needed to do the same thing.
On a side note, already a handful of disks failed on me, probably we’re talking about 4%; I didn’t reach yet the point where I switched to DVDs; as far as statistics are concerned, first place for unreadable cd’s are Verbatim Datalife (not plus), second (this is a surprise) TDK Reflex, and last, Traxdata tx; still surprisingly, a green die unknown brand of CDr’s, “CDV”, the very first ones I bought in my life, hence the oldest and the ones burned the longest time ago (about 9 years), were all perfectly readable; perfect results, at least until now, and with top reading speeds, are TDK metalAZO with white printable surface, and Verbatim DataLifePlus with silver surface, both of them (guess what) with blue die; special mention goes also to the TDK d-view’s, and the Verbatim Pastel-Disc, no failures there as well.

5 thoughts on “Batch script to copy your backups from cd/dvd optical disks to a hard disk/nas”

  1. Incredibile, stavo cercando un programmino che faceva la stessa identica cosa, gia’ pronto (visto che sai quanto sono pigro), e immagina la sorpresa di trovane uno proprio sul tuo sito 🙂
    Ovviamente, visto che la mia pigrizia è asintotica, l’ho modifcato in modo che non mostri nessun messagbox se il file è gia’ presente e nel caso non riesca a copiarlo (sto eliminando un centinaio di cd di mp3 vecchissimi, se perdo qualcosa non è un poi un dramma…)

  2. non mi funziona correttamente..
    quando inserisco il secondo dvd mi esce un errore e nell’ hd mi si creano un sacco di file diversi.

    1. Mi dispiace che non ti sia andata bene, esistono delle variabili con autohotkey per quanto riguarda il check dello stato del lettore dvd in base al tipo di disco presente, e forse anche per la creazione dell’albero delle cartelle sul disco fisso (permessi vari ecc?). Forse hai solo bisogno di eseguire lo script come amministratore.
      In ogni caso a me il tutto è servito una sola volta, ovviamente, una volta passati i file non ne ho più bisogno, e come dico non fornisco supporto in caso di problemi, dopotutto mi sono anche dimenticato la sintassi AHK nel frattempo 😉
      Ti consiglio di usare lo script fornito come base di partenza, documentarti sul tipo di errore che ricevi, e correggere il codice in base alle informazioni che troverai su internet.

Leave a Reply

Your email address will not be published. Required fields are marked *