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
Related
I want to move my files (along with sub-folders) from %LocalAppdata%\A directory to B\App\D directory.
The batch file resides in B\Bat directory. I currently do the following to copy files from
%LocalAppdata%\A to B\App\D --
MOVE /Y "%LocalAppData%\A\*.*" "%~dp0..\App\D"
(Note that the folder B is a variable, so I just can't use it)
The above doesn't seem to move the sub-folders .
MOVE and RENAME can work with multiple files using wild cards in the source mask. They can also work with individual folders. But they cannot work with multiple folders using wild cards in the source mask.
So you can move the folders individually using a loop, and then move all the remaining files directly with a wild card.
for /d %%F in ("%LocalAppData%\A\*") do move /y "%%F" "%~dp0..\App\D"
move /y "%LocalAppData%\A\*" "%~dp0..\App\D"
Or simply move everything individually with one loop. The only draw back with this is it can fail if there is unicode in your file/folder names that does not map to the active code page. It is also a bit less efficient, but I don't think that is significant.
for /f "eol=: delims=" %%F in ('dir /b "%LocalAppData%\A\*"') do move /y "%LocalAppData%\A\%%F" move /y "%%F" "%~dp0..\App\D"
One advantage of this last approach is you can add the /A option to the dir command (do not follow with attributes) and move all files and folders, including those with the hidden and/or system attribute.
Set the working directory to the parent.
pushd "%~dp0.." && MOVE /Y "%LocalAppData%\A\*.*" ".\App\D\"
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.
I want to loop through all subfolders in c:\my_folder, check if specified file exist then create (if not exist) folder in c:\copy (for example c:\copy\project1) and copy mentioned file.
Is it easy to loop through folders in batch file (for /d %variable in ("path") DO command),
but variable contains full path. For creating new folder I need only basename for subfolder.
How I can get it? Should I write another for loop when I remove other characters?
Is there any easier way?
If I understand you correctly you want just the folder name. In that case this is easy:
for /d %%d in (C:\my_folder) do echo %%~nxd
What Joey said. On the other hand, you could try a completely different approach:
XCOPY "C:\my_folder\my_file" "C:\copy" /S /I
This will copy the specified file from all subfolders of C:\my_folder to C:\copy with preserving the structure. Subfolders that do not have my_file will not be created in C:\copy. The /S switch tells XCOPY to search for my_file in subfolders. The /I parameter is there to make sure C:\copy should be a folder, before proceeding.
My requirement is to write a batch script that will compare the files in two folders. If a file exists in both SourceFolder and TargetFolder, then overwrite the file in TargetFolder with the file in SourceFolder.
Using a for-statement and an if-statement I can achieve this:
for /R %Source% %%G in (Prefix.*.ext) do (
if exist %Target%%%~nxG (
del %%G
copy %Target%%%~nxG %Source%
)
)
Although an additional requirement is to only copy files that start with 'prefix.' and end in '.ext' and also to exclude all files that contain the word 'exclude'.
In English: Copy all files from that source folder that start with 'Prefix.', end in '.ext', does not contain the text 'exclude'. and already exists in the target folder.
This is where I get stuck. Does anyone know how to do this in batch?
You can use xcopy for this. First, I am assuming that Prefix and ext are actual strings, to use variables instead you would have to wrap them like %Prefix%.
Second, you will have to make a new text file. Name it excludes.txt and put it in the same directory as your batch file. (If you don't want to make a batch file, then just put it in the directory that is active when you run the command). The only contents of this file should be your EXCLUDE string with no quotes, or other markup.
Ok, the command itself:
xcopy %Source%\Prefix.*.ext %Target% /U /EXCLUDE:excludes.txt
To break it down:
%Source%\Prefix.*.ext Selects the files in the source folder that start with Prefix and end with .ext
%Target% Specifies the destination for the files
/U Only copy files that already exist in the target directory
/EXCLUDE:excludes.txt This will read in excludes.txt and exclude any file that matches any part of the excludes.txt file.
That's it! This is probably easier than writing a FOR statement with a nested IF.
After reading this SO question, I ended up doing it like this. (Before the question got answered)
pushd %Target%
attrib +h *Exclude
for /R %%G in (Prefix.*.ext) do (
if exist %Target%%%~nxG (
del %%G
copy %Target%%%~nxG
)
)
attrib -h *Exclude
popd
The xcopy solution probably looks better although I'd prefer not to have to create (and remove) files if I can help it.
Just wondering the best way to do this in a single batch file. I have a folder called C:\Program\Foo and I want to grab all the folders except for the testing folder inside of foo, and I want to xcopy into D:\ so in D:\ foo will be there but no test folder.
Is there a way I can loop through each folder and check for a certain name not to include?
using /Exclude would mean I would need an extra text file with "Testing" in it
I don't see why you could not create a temporary exclusion file (using a temporary folder, that is):
#ECHO OFF
FOR %%F IN ("%TEMP%\exclude.txt") DO SET tmpf=%%~sF
ECHO Testing>%tmpf%\exclude.txt
XCOPY source destination /EXCLUDE:%tmpf%\exclude.txt other options
Note: XCOPY does not recognise double quotes as path delimiters in the /EXCLUDE option and offers no alternative for specifying paths with spaces, which can be a problem on Windows XP systems. This limitation can be worked around by replacing the original path with its counterpart consisting only of short names. That is what the FOR loop in the above script does for the %TEMP% folder.
Can you use ROBOCOPY?
ROBOCOPY C:\Program\Foo D:\ * /E /XD Test
/E copies subfolders and files
/XD excludes directories
Use the EXCLUDE option and put your exclusions in that file. That will let you exclude entire directories.
http://www.pcreview.co.uk/forums/using-xcopy-backup-can-exclude-some-directories-t489674.html
xcopy "c:\document and settings" "i:\documents and settings\" /s /d /EXCLUDE:c:\a.txt
a.txt contains
\temp\
\temporary internet files\
You may need to use shorter DOS file names.