I have a set of files in a shared directory (source folder say F:) in the dated folder (e.g. 20180601, 20180602 etc). I have the destination folder which is in my local drive (destination say C:).
I want to copy the selected files from source to destination.
e.g.
F:\Source\20180601\abc1.csv to C:\Destination\20180601\abc1.csv
F:\Source\20180601\abc2.csv to C:\Destination\20180601\abc2.csv
....
Now I am manually creating the folder in destination and copying the selected files manually, I am not able to iterate this for dated folders.
Any help is appreciated. Thanks in advance.
The question is very unclear, but I try nevertheless to answer it.
I assume the files are selected in Windows Explorer and the goal is to copy the selected files with full path from drive
F:
to driveC:
, but with replacing the first directorySource
byDestination
.This could be done using the following batch file:
The batch file can be stored in any directory. For running it with the selected files in Windows Explorer it is necessary to create a shortcut file (*.lnk) in
SendTo
folder of your user account.The
SendTo
folder is on Windows XP the folder%USERPROFILE%\SendTo
and on Windows Vista and later Windows versions the folder%APPDATA%\Microsoft\Windows\SendTo
(as confirmed by Stephan).To create the shortcut file, click with secondary (right) mouse button on the batch file and click in opened context menu in submenu Send to on menu item Desktop (create shortcut).
Switch to your Windows desktop and click with secondary (right) mouse button on the just created shortcut and left click in opened context menu on last item Properties.
First append on Target the string
%*
(space, percent sign, asterisk) which is important to run the batch file later with full qualified file names of all selected files passed as arguments to the batch file by Windows Explorer. Second write in Comment a comment useful for you to know later what this shortcut does on using it. You can also click on button Change Icon and select a better icon from%SystemRoot%\System32\shell32.dll
as the one Windows has automatically chosen for the batch file shortcut. Then close the Properties with button OK.Click again with secondary (right) mouse button on the just created and modified shortcut and click with primary (left) mouse button on menu item Rename. Rename the shortcut file to whatever you want like Copy Selected Files.
Now the shortcut file on Windows desktop is ready for being moved to the
SendTo
folder. So select the shortcut file with a single click on it with primary (left) mouse button , press Ctrl+X to cut it, open in Windows Explorer theSendTo
folder of your user account and paste the shortcut file with Ctrl+V into that folder.The batch file is ready for usage now from within Windows Explorer. You can select one or more files (not folders) in
F:\Source
, click with secondary (right) mouse button on one of the selected files to open the context menu and left click in submenu Send To on Copy Selected Files or whatever name you have given the shortcut file.The batch file runs a FOR loop processing all file names passed to the batch file referenced with
%*
. Each file name is assigned one after the other to loop variableA
.The path of the current file without drive letter
F:
is processed by a second FOR which splits the path string into two substrings using backslash as delimiter. The first substring isSource
which is assigned to specified loop variableB
which is not further used. Everything else of path without drive letter and colon after\Source\
is assigned to next loop variableC
according to ASCII table. So loop variableC
holds the directory path of current file withoutF:\Source\
and ending with a backslash which is important for next command XCOPY.Note: The number after
tokens=
determines the number of directory levels ignored from source file path.The command XCOPY is used to copy the file of which full qualified file name is hold in loop variable
A
to folderC:\Destination
with creating the directory tree as hold in loop variableC
if that is necessary. XCOPY interprets destination argument always as directory if the destination argument ends with a backslash. That's the reason why it is so important here that loop variableC
holds the path ending with a backslash.After all files are copied, the execution of batch file processing is halted by
cmd.exe
because of command PAUSE so that you can verify if everything worked as expected.Please note that the batch file can't be used to copy thousands of files with one single execution. The length of the command line is limited by Windows. So it is not possible to pass from Windows Explorer to batch file thousands of full qualified file names.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains%*
.echo /?
for /?
xcopy /?