windowsbatch-filecopydrive-letter

Is there a way to copy files with a batch file to the folder that batch file is in?


So I am trying to make a batch file to sync an external hard drive. My hard drive is not always connected to the same letter, so i cannot just make a batch file with xcopy F:\Folder1 G:\backupharddrive because G:\backupharddrivewould be variable

Is there a way to just copy to the folder the batch file is in?


Solution

  • Just use relative addressing, where . represents the current directory.

    xcopy F:\Folder1 .
    

    For future reference, you can also access the parent of the current folder (IOW, 1 level higher up in the directory tree) using ... This makes it handy to move up a level, or even over one. For instance, to copy files from C:\Temp\One to C:\Temp\Two, you can use

    xcopy *.* ..\Two\
    

    which means copy the files from this folder up one level and then over to the Two folder.

    If you use dir from a command prompt, you'll see that in any level below the drive root you'll have items for . and .. in the list. (The root only has ., because it has no parent folder.