Copy files already exists and ignore source folder structure - batch-file

I need to replace files in destination with ignore source structure.
Basicly source folder have many subfolders, destination is only one folder without the subfolders so I tried Xcopy but the result is not good.
I have another folder with same issue I beleive the solution for the upper question will solve both.
Drop folder from TFS get everytime different name, I need to be able to copy from buildoutput folder(parent folder for all builds) only existing files to destination, destination doesn't include folders.

As best as I can tell from your question:
#echo off
set "src=C:\Your\Source\Directory"
set "des=C:\Your\Destination\Folder"
for /r "%src%" %%A in (*) do (
if exist "%des%\%%~nxA" copy /y "%%A" "%des%\%%~nxA"
)
Will loop through all files in your source directory, check to see if those files exist in your destination folder, and if they do it will copy/overwrite them.
Reference: For, Copy.
If you're trying to accomplish something more specific you'll need to edit your question.

Related

Delete duplicate files of different extensions recursively

I'm trying to create a batch file that can delete .xmlb files is there is a .engb file of the same name in the same folder. I want it to work so that it can check the folder where the batch file is placed as well as all of the sub-folders. I found this question where someone pointed out how to do this in a single folder, but I'm trying to do it in every sub folder. Here's what I have so far:
#echo off
for /r %%I in (*.xmlb) do (
if exist %%~pnI.engb del %%I
)
pause
It gives me a "the system cannot find the path specified" error. I tried changing del with echo, and when I do that it does manage to display the names of the .xmlb files (with .engb extensions), so I'm assuming it can actually find them and my syntax is off somewhere. If I try to use %%~nI like the original code, it won't do anything (because the .engb files are in the same folders as the .xmlb files). I'm sure I'm probably missing some small thing that's preventing it from deleting the right files. Any help is appreciated! I'm attaching two screenshots: one with the batch file in a folder, and then one of the sub-folders that contains .xmlb and .engb files. So in the jungle1, jungle2, and jungle3 folders I want to keep the .engb files and remove any .xmlb files with the same names as .engb files

Move a folder if a file exist, the check should perform in multiple directories

I want a batch file to check for a certain files in a folder and all of its subdirectories and if that file is found I want to copy the folder in which the file sits.
How can I achieve it? Can anyone help?
You can try something like this:
#echo off
for /R "full_path_to_folder_with_subfolders" %%A IN (filename_you_want_to_check) do (
copy %%~FA "full_path_you_want_file_to_be_copied"
)
NOTES:
full_path_to_folder_with_subfolders should be replaced with the FULL PATH of folder which contains the subfolders in which you want to search the certain files.
filename_you_want_to_check is only ONE certain file you want EXCEPT if your files have a common suffix, for example you have files a1.txt and a2.txt (ONLY IF these files will be copied in the same location) replace filename_you_want_to_check with a*.txt.
full_path_you_want_file_to_be_copied is the FULL PATH where your certain files (filename_you_want_to_check) should be copied.
Hope this helps!

Command Promp to copy ONLY images from FOLDERS and SUBFOLDERS

I'm wondering if it is possible to set up a batch command to perform this action.
Once .bat file is executed, ALL images from folders and sub-folders would be copied to my location on the desktop.
Example:
Original folder is located:
\intranet\file_location\PP Complete Images (in this folder will be loads of other folders and in those folders there will be .jpg images)
Destination file would be based on the desktop.
So I need to extract .jpg images from all folders and sub-folders.
If image already exists in original folder, skip the image or overwrite as script will be executed every morning.
Or should I look for a software to do this for me?
Existing code:
cd c:
cd\
copy "\\intranet\PP Complete Images\Master Image Folder*.jpg" "C:\Users\username\Desktop\Master Image Folder"
copy "\\intranet\PP Complete Images*.jpg"
exit
How do you want it?
It isn't quite clear to me, how the result exactly should be -- should it be flattened or should it be hierarchical as well?
Look at this for example:
source
folder-1
folder-1-1
image1.jpg
folder-1-2
image2.jpg
cheese.jpg
image3.jpg
some_text.txt
folder-2
folder-2-1
image3.jpg
some_music.mp3
cheese.jpg
target
Should the result be basically a copy of the shown hierarchy (without any other file than the jpgs), or should it be a flattened result like this one:
source
... (see above)
target
image1.jpg
image2.jpg
cheese.jpg
image3.jpg
image3.jpg
How can you do it?
Flattened
You can use DOS' for command to walk directories1 and make a custom function2 to handle the files:
#ECHO OFF
for /r %%f in (*.jpg) do call:copyFile %%f
GOTO END
:copyFile
copy /V /-Y %~1 ..\target
GOTO:EOF
:END
Meaning: for every %%f in the listing of *.jpg from the current working dir, execute function copyFile. The /r switch makes the listing recursing (walk through all subdirectories).
In the function, the argument passed to it (now known as %~1) is passed to the copy function: Copy the file to the target directory which is ..\target in this case. /V lets copy verify the result, /-Y lets it ask for permission to overwrite files. See copy /?!
Very big problem: If you have one or more files in different subfolders of your source directory, which have the same name (like the two cheese.jpgs in my example), you will loose data!
So, I wouldn't recommend this approach, as you risk loosing data (digital cameras are not very creative in naming pictures!).
Hierarchical
Just use robocopy:
robocopy /S <sourcedir> <targetdir> *.jpg
/S creates and copys subfolders as well. You can also use /DCOPY:T to make the directories have the same timestamp than the original ones or /L to preview the actions of robocopy.
Small problem: The /E switch handles subfolders as well, even if they are empty. /S handles subfolders as well, but not, if they are empty. But it handles them, if they are not empty, but have no JPG inside -- so, subfolders without JPGs will result in empty folders in the target folder.
Robocopy has loads of parameters, so check out robocopy /?.
Hope this helps! :-)
1Found here: How to traverse folder tree/subtrees in a windows batch file?
2Found here: http://www.dostips.com/DtTutoFunctions.php
Your existing code:
the cd c: is incorrect. To switch the current drive to c: use
c:
The cd \ is redundant. Your remaining code specifies the directories, so the current directory is irrelevant.
Your first copy command has three problems. Master Image Folder*.jpg means all filenames beginning Master Image Folder and ending .jpg. You probably meant Master Image Folder\*.jpg meaning all files ending .jpg in ...\Master Image Folder\
C:\Users\username\Deskto... is probably an error. It is a literal path, so the actual directory would be C:\Users\username\Deskto... You would probably need C:\Users\%username%\Deskto... to substitute-in the current username.
And then the job would stop on a filename-match, so either you'd be pressing A to overwrite all or you'd be pressing y or n for each name-match.
Your final copy command has no specified destination directory.
You can edit-in your actual code by using the edit button under the original text window, cutting-and-pasting your actual code - censoring if necessary, selecting the resultant code block and pressing the {} button above the edit box which indents each line with the effect of formatting and hilighting the code.
The simplest solution is probably to use
xcopy /d /y /s "\\intranet\PP Complete Images\Master Image Folder\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"
which will copy updated files (/d) with automatic overwrite (/y) and scanning subdirectories (/s) from-name/mask to-directory.
This would create an identical directory-hierarchy to the original subtree under the destop's Master Image Folder directory.
You could extend this to
for %%a in (
"\\intranet\PP Complete Images\Master Image Folder"
"\\intranet\wherever\somewhere"
) do xcopy /d /y /s "%~a\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"
to perform the same action on multiple directory-subtrees; but you need to ensure that the destination directory is not within any subtree selected for inclusion in the list within the parentheses.
I'd advise against "flattening" the output because if you do that, the latest whatever.jpg from each of the subtrees will end up in your destination directory, without notification that there are many possibly different whatever.jpg versions.
I do believe the solution to your problem would be Robocopy.
Robocopy is just plain awesome!
Here is the syntax of robocopy-
robocopy [Source] [Destination] [File] [...] [options]
Source
Specifies the source folder. Where you want to take the files from.
Destination
Destination directory/folder.
File
Here we are! This is what will help you. Here you can specify an extension you want to move. So in your case, your code would look somewhat like this.
robocopy *.jpg c:\destinationdir /S /MAX:1048576
*To execute this .bat every morning go to a program called task scheduler, dont worry, its built into windows. http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7
*Then Click on Create basic task, and set your task to whenever you like!
Thanks guys for your help!!!
I got it solved and there is a code below if someone would ever need something similar:
pushd Z:\intranet\PP Complete Images\
for /r %%a in (*.jpg) do (
XCOPY /Y "%%a" "C:\Users\username\Desktop\Master Image Folder"
)
popd

.bat file for copying folder to folder leaving out certain folders

Hi I need help to copy folders from one folder to another.
Suppose there are five folders named
forward1
forward2
forward3
Backward
Forward directions
and i need to copy only forward1,forward2,forward3 into Forward directions leaving out Backward repetitively.
Is there any way to do this with batch files?
Robocopy has an exclude feature and has similar functions to xcopy
I'm not sure if that's what you are looking for. This copies every file of the specified folders into the "Forward directions" folder.
Be carefull with spaces in path-names.
#echo off
set sources=forward1 forward2 forward3 forward4
set "destination=Forward directions"
for %%a in (%sources%) do (
copy "%%a" "%destination%"
)
If you want to copy the folders, this would copy your folders into the "Forward directions" folder using xcopy.
#echo off
set sources=forward1 forward2 forward3 forward4
set "destination=Forward directions"
for %%a in (%sources%) do (
xcopy "%%a" "%destination%/%%a" /IE
)
If you want to always overwrite directories/files, change /IE to /IEY.
Here is a full list of other usefull Xcopy Options.
Either you try to copy the folder by creating the exclude list and the other way is to copy by providing the command like
%xcopy% %source%\forward1 %destination%\forward1
%xcopy% %source%\forward2 %destination%\forward2
%xcopy% %source%\forward3 %destination%\forward3

Update existing files in a folder and it's subfolder from another folder

I have two folders, A and B. A contains files I want to copy over to B, provided they already exist in B.
Both have a similar structure but A has more files and directories than B has, but I cannot figure out how to keep only the path relative to the inside of each directory intact.
So if I had
C:\FolderA\file1.txt
C:\FolderA\file2.txt
C:\FolderA\subfolder\text1.txt
C:\FolderA\subfolder\text2.txt
C:\FolderB\file2.txt
C:\FolderB\file3.txt
C:\FolderB\subfolder\text2.txt
It would only copy file2.txt and subfolder\text2.txt. A sort of "only update" command.
I've come to this, executed from FolderB:
FOR /R %%i IN (*) DO COPY /Y "%%~nxi" "%%i"
Unfortunately %%~nxi only gets me the file, none of the internal directory structure.
You want to use xcopy with the switch /u.
Copy only files that already exist in destination.

Resources