How to copy files in specific location within multiple folders to a shared drive using windows command? - windows

I need to copy several backup files to a shared folder
The Source to copy from looks like this below (there is other files inbetween but are not interested in copying them
C:\BackupLocation\BCK1\Logs\myLog1.log
C:\BackupLocation\BCK1\Logs\myLog2.log
C:\BackupLocation\BCK1\Logs\myLog3.log
C:\BackupLocation\BCK2\Logs\myLog1.log
C:\BackupLocation\BCK2\Logs\myLog2.log
C:\BackupLocation\BCK2\Logs\myLog3.log
C:\BackupLocation\BCK3\Logs\myLog1.log
C:\BackupLocation\BCK3\Logs\myLog2.log
I need to be able to copy any log file with the words "myLog" in the file name.
I thought about doing a For look do get the directory and then using robocopy to copy the files to my shared location
FOR /d /r %ROOT_SRC_BACKUP_LOCATION% %%G in (*logs*) DO (
Pushd %%G
Robocopy %%G "mybackup location directory" *mylog* /S
Popd )
But the problem is that the logs directory detects other logs directory... for example I have another logs directory in here
C:\BackupLocation\BCK3\server\Logs\IDontCareAboutTheLogsIntheServerDirectory.log
What is the most effective way to copy all the myLog's in the C:\BackupLocation\BCKXX\Logs\ folder?

Test this:
#echo off
Pushd "C:\BackupLocation"
for /d %%a in (bck*) do (
if exist "%%a\logs\" Robocopy "%%a\logs" "d:\mybackup location directory\%%a\logs" "*mylog*.log"
)
Popd

Related

How to batch copy files based on a list (txt) in to another folder with same directory structure?

I have a root directory with over 25,000 files in it. These files are in loads of different subdirectories.
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory. Like below,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.
#echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure. So using the power of the FOR command modifiers you can get the file path only from the file name in the file list. You will use that modifier to create the destination directory and also use it as the destination for the XCOPY command.
I have taken the liberty of providing best practices for all the code you are using.
#echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)

copy directory and its contents without using xcopy or robocopy

xcopy and robocopy are not working when I try to transfer files to a remote drive, so i'm restricted to using del and copy until I can figure that out.
Here is what I have so far:
del /q y:\OPENcontrol\targetDir
for /d %%x in (y:\OPENcontrol\targetDir\*) do #rd /s /q "%%x"
copy c:\Users\CNC\share y:\OPENcontrol\targetDir
How can I copy folders and their contents without using xcopy or robocopy?
Edit: This is on a CNC machine that is transferring files to its OPENcontrol module. The code needs to work within the limitations of the OSAI controller. A good example is that do (mkdir "destination\%%i" copy "%%i\*" "destination\%%i") had to execute in two separate loop commands, one for mkdir and one for copy
As per my comment (not being 100% working at the time). a Simple for loop will be able to run through each folder, create it and copy its contents. There are better ways, but yes, you said you wanted to use copy specifically. So as per your ammended comment:
for /d /r "c:\Users\Nil\share" %%i in (*) do (
mkdir "c:\Users\Nil\targetDir\%%~nxi"
copy "%%i\*" "c:\Users\Nil\targetDir\%%~nxi"
)

How can I create new empty folders in a directory using names of subfolders of another directory using batch script?

My main goal is to backup SVN repositories in REPOS folder. Since "svnadmin hotcopy" has to have both source and target folders I need to create new folders in different directory with name of folderName_backup and then copy them using "svnadmin hotcopy". Btw this has to be done in windows batch file. My code for this portion is the following:
for /d %%X in (%source%\*) do (
md %destination%\%%X_backup
svnadmin hotcopy %%X %destination%\%%X_backup
)
After running this code I get error : The filename, directory name, or volume label syntax is incorrect.
set "source=c:\where\the\folders\are"
set "destination=c:\where\the\backup\will\be"
for /d %%a in ("%source%\*") do for %%b in ("%destination%\%%~nxa_backup") do (
if not exist "%%~fb\" md "%%~fb"
if exist "%%~fb\" svnadmin hotcopy "%%~fa" "%%~fb"
)
%%~fb is the full path to the file/folder referenced in %%b. The equivalent for %%~fa. %%~nxa is the name and extension of the file/folder referenced in %%a.
The code encloses all paths in quotes to avoid problems with spaces, and test for existence of the folder before creation and backup
You can remove the inner for %%b and use the composed target folder in the md and svnadmin commands (as in your code), but this aditional for allows to write only once how the target folder is defined.
for /f "tokens=*" %%G in ('dir /b /a:d "%Source%"') do (
md %destination%\%%G
svnadmin hotcopy %Source%\%%G %Destination%\%%G
)
The problem with the old solution was instead of getting folder names in directory it gets subdirectory of the directory. This is why the result was something like C:\where\the\backup\will\be\C:\where\the\folders\are
That was the reason that I got error:The filename, directory name, or volume label syntax is incorrect. The script part (works fine) that I have shared with you above in answer tokenize the subdirectory to get the folder name and use it.

Batch copy command doesn't reach subfolder

I have a batch command to copy and move file with a new name to another directory. When I added one more level of subfolder it does not copy but when I remove the added subfolder and move the file to the previous level it does copy. Here is it is:
cd /d dir "U:\Sourcing\Vendor Demand Planning\Customer CPFR\BBM\"
for /f "delims=" %%I in ('dir /b /o:-d "BBM Tool *.xlsx"') do (
copy "%%I" "..\Pricing Project\BBM Tool.xlsx"
exit /b
)
If I removed the BBM level and move the file into the Customer CPFR level it works. I edited this batch from another one that had the target file in the CPFR folder. The file I need to copy is in the BBM folder.
Change COPY to XCOPY and use /S
See XCOPY /?
I'd say that the "Pricing Project" directory is on the same level as "Customer CPFR" hence the target of the copy should be "..\..\Pricing Project...
(later edit)
Also dir in the CD line should be removed.

To copy entire subfiles to other (only one) folder without path in windows platform

I did lots of search however i couldnt achieve what i want to do :
I have a folder which contains lots of folders and files under it. I just want to copy entire JPEG files in this folder to another folder without copying previous path tree.
I used this batch command like that :
for /r "C:\source" %%f in ("*.JPG") do #copy %%f "C:\dest" /Y
It works some files which dont contain any space in the name of files. However, it doesnt work files which contains space in file name like that :
it can find and copy "aabb.JPEG"
it can not copy "aa bb.JPEG"
any solutions?
The following works great for me
#ECHO OFF
FOR /R "c:\source" %%F IN (*.jpg) DO (
copy "%%F" "c:\dest\" /Y
)

Resources