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

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"
)

Related

CMD/BATCH From folders in current location

I believe it should an easy way of doing this, but I could not figure out how.
Current situation:
Customer send zip file to our SFTP folder, by using some easy batch files it copy zip files from SFTP, unzip it and carry on with the rest of the things which has to be done.
What I need:
Inside zip file there is a folder(main problem that it could be named randomly). All what I need is a BATCH file which will copy all files from any existing folders in current(job) location.
Edit:
Current method is a really bad(but it solid and working), I just simply change the name of the folder in batch file and everything run nice and smooth. Method is working fine, but I would like to automate it completely.
You can iterate over the directories in the extracted archive directory using DIR /A:D which only gets directories. This will process more than one subdirectory if it is found.
SETLOCAL ENABLEDELAYEDEXPANSION
SET "EXTRACTED_DIR=C:\path\to\extracted\archive"
FOR /F "usebackq tokens=*" %%d IN (`DIR /A:D /B "%EXTRACTED_DIR%"`) DO (
SET "THE_DIR=%%~d"
ECHO Do something with mystery directory "%EXTRACTED_DIR%\!THE_DIR!"
)

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

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

Need a BAT file to copy & rename all files in specific tree

I need to create a .bat that runs through a multilayered directory... copying certain files that contain the following suffix: '.full.jpg' to save as '.jpg'
What I've tried:
copy /y "C:\Users\myname\Desktop\maindir\*.full.jpg" "C:\Users\myname\Desktop\maindir\*.jpg"
However, I cannot get it to work.
The .bat is located in the 'maindir' directory and ran from the terminal (cmd).
Here's an example scenario that maps closely to mine:
Existing Files:
C:\Users\myname\Desktop\maindir\a\a\picture1.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.full.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.full.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.full.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.full.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.full.jpg
Example Output Wanted:
C:\Users\myname\Desktop\maindir\a\a\picture1.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture1.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.full.jpg
C:\Users\myname\Desktop\maindir\a\a\picture3.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.full.jpg
C:\Users\myname\Desktop\maindir\a\b\picturea.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\a\b\pic1.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.full.jpg
C:\Users\myname\Desktop\maindir\b\a\foto.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.full.jpg
C:\Users\myname\Desktop\maindir\b\a\photo.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.full.jpg
C:\Users\myname\Desktop\maindir\b\b\pic1.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.full.jpg
C:\Users\myname\Desktop\maindir\b\c\pi2.jpg
I'd appreciate any help towards this as I haven't been able to do it yet. I will run across a directory structure whereby the top level directory will contain 15+ directories and each containing 20+ directories with 100+ files in each lowest directory.
Thanks.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:\Users\myname\Desktop\maindir"
FOR /r "%sourcedir%" %%a IN (*.full.jpg) DO (
FOR %%b IN ("%%~dpna") DO ECHO(COPY "%%a" "%%~dpnb.jpg"
)
GOTO :EOF
The inner for examines the drive-path-name only of the complete filename in %%a (ie. it drops the .jpg) and delivers the drive-path-name of that name (ie. drops the .full) to which you add .jpg and job done.
You would need to change the setting of sourcedir to suit your circumstances.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY to COPY to actually copy the files.

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
)

copy list of files whose paths are in semi colon delimited source.txt file to destination folder keeping their souce paths

i have list of files in test.txt which contains list of file paths in format d:\source\www\default.aspx;d:\source\common\common.js I need to write a bat file to copy these files to destination eg.F:\destination\ whose path is also passed as an parameter to the bat file.I have following script for this for /f %%l in (somefile.txt) do (
for %%f in (%%l) do (
copy "%%f" %1
)
) issue is i need to keep the copy source folder's folder structure in destination folder too. ie above d:\source\www\default.aspx need to copy to f:\destination\www\default.aspx not to f:\destination. Will gratefull if some one can give solution to this.
Please try with xcopy /I "%%f" "%~1\%%~pf":
xcopy will create the directory structure for you (without prompting because of the /I switch);
%%~pf is the path-only part of the file to copy (see help for), appended to your destination base path without any surrounding quotes %~1;
the destination path combination is enclosed in quotes.

Resources