copy directories only cmd - batch-file

I need to copy an unknown number of directories (including the files inside them) from one location to another with a batch file.
My only problem is that I must not copy the files that are located in the same location as the directories.
For example:
Let say c:\Folder\ contains the directories: Dir1 and Dir2 and the file: f1.
I want to copy c:\Folder\Dir1 and c:\Folder\Dir2 (and the files inside them) to c:\Location directory but not file: f1.
Help please!

Try this:
#echo off
setlocal enabledelayedexpansion
REM Set variable
set _SOURCE="C:\Temp\Test\"
set _DESTINATION="C:\Temp\New\"
REM Change Direction
pushd %_SOURCE%
FOR /D %%a in (*) DO xcopy /S /I %%a %_DESTINATION%%%a
/D : Just directories
/S : Copy subdirectories
/I : Targets are directory (not file)

How about something like...
FOR /F "usebackq tokens=*" %%d IN (`DIR /AD /B C:\FOLDER`) DO (
IF NOT EXIST "C:\LOCATION\%%d" (MKDIR "C:\LOCATION\%%d")
XCOPY /E "%%d" "C:\LOCATION\%%d"
)

Related

Move files with batch and browse sub folders

I created a batch file that allow to move a file from a folder to another.
My issue is that my bat file should also browse source subfolders in order to find files that have a specific pattern.
Actually :
#ECHO ON
SET SourceDir=C:\Users\me\Documents\source
SET CopyDir=C:\Users\me\Documents\repository
SET FilePatterName=*pattern*.pdf
FOR %%A IN ("%SourceDir%\%FilePatterName%") DO (
ECHO F | XCOPY /Y /F "%%~A" "%CopyDir%\"
DEL /Q /F "%%~A"
)
GOTO EOF
For example : in my source folder, if i have sub1, sub2, sub3 folders and a sub1-1 folder in sub1, i would like to check each folders, check the files and move them without creating any folder in the repository
As per my comment, use for /r which will recurse through the directories:
for /R "%SourceDir%" %%A in ("%FilePatterName%") do...
if it feels too ugly then first pushd to the directory, then recursively search from there:
#Echo off
Set "SourceDir=C:\Users\me\Documents\source"
Set "CopyDir=C:\Users\me\Documents\repository"
Set FilePatterName=*pattern*.pdf
Pushd "%SourceDir%"
For /R %%a in ("%FilePatterName%") do (
Echo F | Xcopy /Y /F "%%~a" "%CopyDir%\"
Del /Q /F "%%~a"
)
Popd
Here's an alternative, using the same structure but robocopy instead of xcopy and del:
#Set "SourceDir=%UserProfile%\Documents\source"
#Set "CopyDir=%UserProfile%\Documents\repository"
#Set "FilePatterName=*pattern*.pdf"
#If Exist "%SourceDir%\" For /R "%SourceDir%" %%# In ("%FilePatterName%")Do #"%__AppDir__%Robocopy.exe" "%%~dp#." "%CopyDir%" "%%~nx#" /Mov>NUL 2>&1
If you really needed to see the filenames, I suppose you could include additional RoboCopy options like /FP, /NDL, /NS, /NC, /NJH and /NJS.

Copy a file from one directory and replace the file in multiple directory

I need to copy a jar file from directory(source) and replace the file in the destination. But the problem is my destination directories are different as explained below:
Source=D:\temp\R56A
Target=D:\path\AP\Different_folders\lib\i2
Target folder example:-
D:\path\AP\ABC1\lib\i2
D:\path\AP\XY_C\lib\i2
D:\path\AP\GHS3\lib\i2
I AM NOT ABLE TO FETCH THROUGH DIFFERENT FOLDER NAMES and the script not taking it.
This is for a windows box. Can we copy the folder name in a text file and call that text file as variable in a for loop? Is it possible?
#ECHO OFF
REM SETLOCAL ENABLEDELAYEDEXPANSION
set Source=D:\temp\R56A
set Target=D:\path\AP\<Different_Directory_names>\lib\i2
set file=i2-bam.jar
for /f "delims=" %%f in ('dir /a-d /b /s "%Source%\%file%"') do (
copy /V "%%f" "%Target%\" 2>nul
)
PART 2
#ECHO OFF
for /d "D:\temp\R56A\" %%f in (i2-bam.jar) do copy %%f "D:\path\AP\<Different_Directory_names>\lib\i2"
Is this what you're trying to do?
#Echo Off
Set "Source=D:\temp\R56A"
Set "File=i2-bam.jar"
Set "Target=D:\path\AP"
Set "Sub=lib\i2"
If Not Exist "%Source%\%File%" Exit /B
If Not Exist "%Target%\" Exit /B
For /D %%A In ("%Target%\*")Do If Exist "%%A\%Sub%\" Copy /Y "%Source%\%File%" "%%A\%Sub%">Nul

xcopy batch issue

I want to copy a set of subfolders where name contains items on a list. The list has a set of codes (e.g. ABC1, ABC2) but the folders are named ABC1_revised_2018, etc. My batch file I put together is below. What I am getting a '"Usebackq tokens=^" was unexpected' error.
#ECHO ON
SET FileList=C:\filelist.txt
SET Source=C:\Files
SET Destination=C:\Files-Parsed
FOR /D "USEBACKQ TOKENS=^" %%D IN ("%FileList%") DO XCOPY /E /F /D "%Source%\%%~D" "%Destination%\"
GOTO :EOF
I am attempting to use ^ to denote match beginning of string but that clearly isn't working. Any ideas? I have tried with a batch file and also line by line in cmd.
append
Folder
-ABC1-text-date (this is a subfolder)
-ABC2-text-date
filelist.txt only has values like ABC1, ABC2, etc. not exact matches does this help?
Well, if you want to recurse through directories and copy sub directories as per partial matches inside the file:
#echo off
set "FileList=C:\filelist.txt"
set "Source=C:\Files"
set "Destination=C:\Files-Parsed"
for /f "delims=" %%a in (%filelist%) do (
pushd %source%
for /f "delims=" %%i in ('dir /s /b /ad "%%a*"') do xcopy /E /F /D "%%~fi" "%Destination%"
popd
)
after getting the entry in the file, for /d will do a directory listing of the directory* in the source directory and physically copy the dir as C:\source\*\ABC2018 etc.

copy specific folders and files using a batch file

I'm trying to create a batch file that will do the following:
I have multiple directories and in each one of those directories there is a folder called '06-2015'. I want to create a batch script that will go thru all of those directories and copy the folder '06-2015' and its files and nothing else.
Example:
C:\Files\Accounts\06-2015
C:\Files\Sales\06-2015
C:\Files\IT\06-2015
Is there a way I can create a script that will go something like:
xcopy C:\Files\*\06-2015 C:\Backup\*\06-2015 /s
Or is there a different/better way to do this?
A wildcard characters allowed only in the last path item.
#ECHO OFF
SET "target=06-2015"
IF NOT EXIST c:\backup\%target% MKDIR c:\backup\%target%
FOR /F "delims=" %%G IN ('DIR /B /AD "c:\files"') DO (
if exist "c:\files\%%G\%target%\" (
:: create backup directory if necessary
MKDIR "c:\backup\%%G\%target%\" 2>NUL
XCOPY /S /E /Y "c:\files\%%G\%target%\" "c:\backup\%%G\%target%\"
)
)
Test this: remove the /s and /e if you don't want any folders inside 06-2015 copied.
#echo off
for /d /r "c:\files" %%a in (06-2015?) do (
if /i "%%~nxa"=="06-2015" xcopy "%%a" "C:\Backup\%%~pnxa\" /s/h/e/k/f/c
)

Copy a list (txt) of files

I've seen some scripts examples over SO, but none of them seems to provide examples of how to read filenames from a .txt list.
This example is good, so as to copy all files from A to B folder
xcopy c:\olddir\*.java c:\newdir /D /E /Q /Y
But I need something like the next, where I can fill actually the source and destination folder:
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And instead of src_folder = c:\whatever\*.*, those *.* need to be list of files read from a txt file.
File-list.txt (example)
file1.pds
filex.pbd
blah1.xls
Could someone suggest me how to do it?
Given your list of file names in a file called File-list.txt, the following lines should do what you want:
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
I just tried to use Frank Bollack and sparrowt's answer, but without success because it included a /U switch for xcopy. It's my understanding that /U means that the files will only be copied if they already exist in the destination which wasn't the case for me and doesn't appear to be the case for the original questioner. It may have meant to have been a /V for verify, which would make more sense.
Removing the /U switch fixed the problem.
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
This will do it:
#echo off
set src_folder=c:\batch
set dst_folder=c:\batch\destination
set file_list=c:\batch\file_list.txt
if not exist "%dst_folder%" mkdir "%dst_folder%"
for /f "delims=" %%f in (%file_list%) do (
xcopy "%src_folder%\%%f" "%dst_folder%\"
)
The following will copy files from a list and preserve the directory structure. Useful when you need to compress files which have been changed in a range of Git/SVN commits¹, for example. It will also deal with spaces in the directory/file names, and works with both relative and absolute paths:
(based on this question: How to expand two local variables inside a for loop in a batch file)
#echo off
setlocal enabledelayedexpansion
set "source=input dir"
set "target=output dir"
for /f "tokens=* usebackq" %%A in ("file_list.txt") do (
set "FILE=%%A"
set "dest_file_full=%target%\!FILE:%source%=!"
set "dest_file_filename=%%~nxA"
call set "dest_file_dir=%%dest_file_full:!dest_file_filename!=%%"
if not exist "!dest_file_dir!" (
md "!dest_file_dir!"
)
set "source_file_full=%source%\!FILE:%source%=!"
copy "!source_file_full!" "!dest_file_dir!"
)
pause
Note that if your file list has absolute paths, you must set source as an absolute path as well.
[¹] if using Git, see: Export only modified and added files with folder structure in Git
This will also keep the files original file directory:
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
set file_list=C:\file_list.txt
for /f "tokens=*" %%i in (%file_list%) DO (
echo f | xcopy /E /C /R /Y "%src_folder%\%%i" "%dst_folder%\%%i"
)
Also can use robocopy and Not use for loop with xcopy - can parse list of files in argument.
robocopy Source_folder Destination_folder [files_to_copy] [options]
Files to copy it's string with Space delimiter.
For example:
robocopy . "d:\my folder" *.txt "my file one.cpp" file2.cpp
robocopy "d:\F 15" "d:\backup\F 15" /E

Resources