I have files in subfolders which are in a folder structure is like this:
D:\Folder_Source
D:\Folder_Source\file1.txt
D:\Folder_Source\FolderA
D:\Folder_Source\FolderA\file1.txt
D:\Folder_Source\FolderA\file2.txt
D:\Folder_Source\FolderB
D:\Folder_Source\FolderB\file3.txt
D:\Folder_Source\FolderB\file4.txt
D:\Folder_Source\FolderB\file8.txt
I want to move some txt
files in each subfolder to C:\Destination\
from a command prompt.
For example, the files I want to move are file1.txt
, file2.txt
, file8.txt
, etc.
If there is a file in the destination folder with the same name of the file from the source folder being copied, I want to keep both files and not overwrite the existing file.
I tried this which fails and does not give me the expected result:
move /s file1.txt file2.txt file8.txt C:\Destination
Is something like this even possible from command line or a batch script?
Best Answer
You can use a batch script and set three variables in it with one being the path to the root source directory which will be traversed for specific files, the second being the destination path where the new files will be copied, and the third being a file list of the file names you wish to copy to the destination directory.
With these three variables you can use a
FOR /R
loop to traverse the source location and use conditionalIF
logic to check whether or not files already exist for whatever copy or delete operation needs to be performed with each file. Furthermore you can use aCALL
and variable substitutions to pass the parts of the file name to another routine and use additionalIF
logic along withSET /A
to increment the duplicate file number, etc.Batch Script
Results
Source Before
Destination After
Source After
Further Resources
CALL
Batch Substitutions (FOR /?)
SET