Coping files from multiple directories into one folder - batch-file

I have many pictures in multiple directories and want to copy them to one folder, but my script is interrupted when a file with the same name already exists in the destination folder. I tried
for /R d:\dups %f in (*.jpg) do copy "%f" d:\pictures\.
What can i add to the code to append the name of the newer file before it's copied to the the destination folder? My source directory is "d:\dups" and my destination folder is "d:\pictures". thanks!

example:
#echo off
for /R "d:\dups" %%f in (*.jpg) do copy "%%f" "d:\pictures\New-%%f"

Related

Batch Copy Files from Inside Folder to Matching Folder in Destination

I have files in a folder structure on one network drive and need to copy them via batch to another folder structure on a separate network drive.
I have a batch script that copies them from the source to the destination but it only copies them into the top-level, not into the folders.
Ex:
SOURCE
newsletters
|-2016
|-2017
|-2018
DESTINATION
newsletters
|-2016
|-2017
|-2018
My script copies the files inside the SOURCE\newsletters\.. folders but only puts them into DESTINATION\newsletters, not inside the year folders:
#echo off
setlocal EnableDelayedExpansion
for /R %G IN (*.pdf) DO xcopy "%G" "DESTINATION\newsletters"
Figured it out with ROBOCOPY:
robocopy ./ DESTINATION\newsletters\ /E *.pdf

Batch copy files from variable source folder and its subfolders to a variable destination folder

I'm trying a very simple batch script, but I must be missing something obvious.
The idea is to copy all files from a source folder and from its subfolders to one destination folder.
Both source and destination folder can be set manually.
The code below works, but it only copies the files of the source folder and not the files from the source subfolders. I guess it's because I use the src variable that it only looks into that folder, and not its subfolders.
set /p src=Enter source folder:
set /p dst=Enter destination folder:
md %dst%
for /r %%i in (%src%) do copy "%%i" %dst%
pause
I based it on the code below that I found on this forum. This one working, including copying the subfolders, but as I mentioned before, I can't define the (main) source folder, or I have to put the batch script in the folder I want to use as source folder:
for /r %%i in (*) do copy "%%i" %dst%
Thanks a lot!
for /r "%src%" %%i in (*) do copy "%%i" "%dst%"
Here's more detailed help for FOR /R

How to copy file with fixed name from a folder on which last folder name in folder path is unknown?

I have a problem on copying a file because of name of one directory in directory tree varies.
The directory tree is: D:\folder\Unknown Folder\myfile.rar
I want to copy the RAR file inside D:\folder\ containing only one folder.
The name of this folder varies and is therefore unknown for me.
I want that the batch script opens D:\folder\, then find and open first subfolder and finally copies the RAR file myfile.rar.
Something like this:
copy "D:\folder\*\myfile.rar" "D:\a.rar"
For each folder under d:\folder, if the searched file exists, copy to target folder
for /d %%a in ("d:\folder\*") do if exist "%%a\myfile.rar" copy "%%a\myfile.rar" d:\a.rar
To use it from command line, replace all %% with %
copy does not support wildcards in the path.
MC ND's answer is good enough , but you can try also this:
for /f "delims=" %%a in ('dir /b /s /a:-d "D:/folder/" ^|findstr /i /e /c:"/myfile.rar"') do (
copy "%%a" d:\a.rar
)

How do I copy all files in the source folder and in the subfolders in batch

I have a folder with many subfolders inside them, the subfolders have random numbers/characters as their name, and they all do have an important file in them. But how do i copy all the files in the source folder and in the subfolders to one folder without copy the folders, in a batchfile?
I use Windows7
This should be all you need.
#echo off
for /r "d:\base\folder" %%a in (*) do copy "%%a" "c:\destination\folder"

copy all files recursively into a single folder (without recreating folders)

With a batch (.bat), I want to copy all mp3 files that are in 1 subdirectory of D:\TEMP
D:\TEMP\\(anyfolder)\\(anyfile.mp3)
to
E:\MYFOLDER\
I tried with xcopy but
I don't know how to tell "just recurse subfolders of D:\TEMP and not subsubfolders, subsubsubfolders, etc."
When using xcopy, folders are created in the destination (in order to replicate source's folder tree), I don't want this : files should be copied in just 1 single folder.
for command is your friend. Read help for and then try this in the command prompt
for /d %a in (*) do #echo %a
as you see, it follows all subfolders in the current directory.
thus,
for /d %a in (*) do #copy %a\*.mp3 e:\myfolder
will copy all your mp3 to the destination folder.

Resources