Copy files matching string(s) in a text file out of many subfolders with folder structure to another destination folder - windows

I have a lot of image files in a folder structure like this:
/Foldername1 (hohe Auflösung)/PictureRANDOMNAME1-FB.jpg
/Foldername1 (hohe Auflösung)/PictureRANDOMNAME2-FB.jpg
[...]
/Foldername2 (hohe Auflösung)/PictureRANDOMNAME1-SW.jpg
/Foldername2 (hohe Auflösung)/PictureRANDOMNAME2-SW.jpg
[...]
/Foldername3 (hohe Auflösung)/PictureRANDOMNAME1-SP.jpg
/Foldername3 (hohe Auflösung)/PictureRANDOMNAME2-SP.jpg
[...]
Now I have a filelist.txt with a list of some of these image files, like this:
PictureRANDOMNAME1, PictureRANDOMNAME3, [...]
I would like to copy all images matching the string(s) in the text file list out of all subfolders to a new destination (not moving them) while keeping the folder structure.
Maybe I need a batch file that I just copy into the main folder with all the subfolders, together with a filelist.txt, execute it and get the same folder structure but only with the wanted files on another destination.
Special about this might be the spaces in the paths and the umlauts.
Sorry for my bad english. I honestly tried my best. Maybe some native speaker could help editing to be more understandable.

#echo off
setlocal
:: This copies the tree structure from sourcedir to destdir
xcopy /t /e sourcedir destdir
:: This reads the filenames and copies the required files
for /f "delims=" %%a in (filelist.txt) do (
for %%b in (%%a) do xcopy /s /e sourcedir\%%b* destdir
)
%%a acquires the lines from the file. Since each line is comma-separated, %%b will execute xcopy on each name on the line.
You could add >nul to the end of the xcopy lines to suppress reporting if you like.
Please note that / indicates a switch in winbatch - \ is the directory-separator.
This procedure is untested. I suggest you try it against a small dummy subdirectory tree to verify it before relying on it for your live data.

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

Bat script to find and replace files in multiple subfolders - replace .java files with .class files from specific folder

I am new to windows batch scripting .. please help on this scenario
I have file structures as below ::
dir1:
c:\workspace\changeset\com\folder
subfolder1
one.java
subfolder-2
two.java
dir2:
c:\workspace\target\class\com\folder
subfolder1
one.class
subfolder2
two.class
Subfolder3
three.class
Need to find and replace dir1 files in respective subfolders i.e one.java and two.java from dir2 files i.e one.class and two.class ( need to replace certain .java files with .class files from specific folder )
much appreciated for your help.
Thanks
Arjun
for /f "delims=" %%a in ('dir /b /a-d "c:\workspace\changeset\com\folder\*.java"') do (
if exist "c:\workspace\target\class\com\folder\%%~na.class" (
echo copy "c:\workspace\target\class\com\folder\%%~na.class" "c:\workspace\changeset\com\folder\%%a")
)
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. Append >nul to suppress report messages (eg. 1 file copied)
Note that execution directly from the prompt and as lines with a batch file are different. The metavariable (loop-control variable) %%x must be referenced as %%x for a batch line and %x if executed from the command prompt. Since it makes little sense to repeatedly execute a line containing a for from the prompt (it's easier to create a batch file), I post the batch-file version. User's responsibility to adjust for direct-from-prompt if desired.
Read each filename in /b basic form /a-d without directories and assign the filename+extension to %%a.
If a file in the other directory called thenamepartofthefile.class exists, then copy that file to the first directory.
Please post the entire problem to be solved. The approach can change radically, as in this case.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir1=U:\sourcedir\changeset"
SET "sourcedir2=U:\sourcedir\target"
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%sourcedir1%\*.java" '
) DO (
SET "javadir=%%~dpa"
SET "classfile=!javadir:%sourcedir1%=%sourcedir2%!%%~na.class"
IF EXIST !classfile! ECHO COPY "!classfile!" "%%a"
)
GOTO :EOF
You would need to change the settings of sourcedir1 and sourcedir2 to suit your circumstances.
Essentially, the same approach and the same comments re messages. The difference is that this procedure uses files and subdirectories in the dir list and substitutes the first part of the directoryname in deriving the expected name of the .class file.

How to Move or Copy Folders using directory Wildcards within Batch File?

I need to set up a batch file that will copy files from a preset directory, several layers down, organized into a folder in the current directory using batch files.
The file structure looks something like this,
AAA
--0001
----textures
------file1.tga
------file2.tga
------file3.tga
--0002
----materials ...
--0003
----textures ...
--0004
----scripts ... etc
--0005 ...
--0006 ...
BBB
--0001 ... etc
---textures ... etc
CCC ...
DDD ... etc
I want to move each textures, materials, models (etc.) folders into a single folder beneath the parent item (eg. AAA) so that the folder structure then looks like this, with all sub-files in tact,
AAA
--textures
--materials
--models
--scripts
I've been trying to use a code similar to this, with no success as of yet,
for /d %%a in ('dir /b .\AAA*\textures') do copy "%%a" ".\AAA"
Whereas the wildcard would go between 'AAA' and 'textures' folders. The same would apply for every other 2nd-generation sub-folder.
I just tried another version of the command which moved the files, but not the folder. I'm launching the batch file from within the "AAA" folder.
for /d %%a in (0001) do copy "%%a" "."
I think the issue is not knowing how to properly input directory wildcards into the "for" command, which could be a very simple fix.
Any help would be greatly appreciated beyond measure.
Put this code in a .bat file, and run it inside your AAA directory
for /f "tokens=*" %%a in ('dir /b') do robocopy %%a . /s /move
for /f "tokens=*" means that all the returned values by 'dir /b' will be processed
dir \b will list the directory in bare format
robocopy is the main command, that will do the copy job.
source %%a is the variable containing the iterated subfolder name {0001\textures, 0002\materials, ...}
destination . means copy to the working directory, which is AAA in our case
option /s to search subdirectories
option /move to move instead of copy

How to replicate a directory structure using xcopy in windows

I have a text file containing a list of folders.My text file looks like this:
"D:\old\FOLDER1"
"D:\old\FOLDER2"
"D:\old\FOLDER3"
"D:\old\FOLDER4"
"D:\old\FOLDER5"
all these folders have subfolders and files under it
what I want to do is use xcopy to copy FOLDER1, FOLDER2 , FOLDER3 FOLDER4 and FOLDER5 replicate folders ,replicating the structure of those folders
so in output , I want to get
D:\output\bkup\FOLDER1\............Including all subfolders and files D:\output\bkup\FOLDER2\............Including all subfolders and files D:\output\bkup\FOLDER3\.......... Including all subfolders and files
D:\output\bkup\FOLDER4\............Including all subfolders and files
D:\output\bkup\FOLDER5\............ Including all subfolders and files
I have written below script which works fine for one folder
set sourceFolder="D:\old\FOLDER5"
set destinationFolder=%sourceFolder:~7,-1%
echo %destinationFolder%
xcopy /s /e /i /h /r /y %sourceFolder% "D:\output\bkup%destinationFolder%"
but since # of directories to copy is 100+ ,I like to use a for loop or pass the list of directories to copy in a text file , that's what I don't know how to handle it.
please help me ,I'm not expert in batch file writing.
You can use for /f to read (parse if needed) a textfile line by line. Use "delims=" to read the line as a whole. Don't forget to add quotes to prevent having multiple arguments and strip the quotes in the subprocedure
for /f "delims=" %%a in (yourtextfile.txt) do call :docopy "%%a"
goto :eof
:docopy
set sourceFolder=%~1
set destinationFolder=%sourceFolder:~7,-1%
echo %destinationFolder%
xcopy /s /e /i /h /r /y %sourceFolder% "D:\output\bkup%destinationFolder%"
goto :eof
try robocopy, it is more powerfull for your needs, available for XP Prof. or newer:
set "sourceFolder=D:\old\FOLDER5"
set "destinationFolder=%sourceFolder:~7,-1%"
robocopy "%sourceFolder%" "%destinationFolder%" /MIR
This makes a complete MIRror at "%destinationFolder%".
If you want to copy folders from a text file with xcopy, use the following code:
set "sourceFolder=D:\old\FOLDER5"
set "destinationFolder=%sourceFolder:~7,-1%"
for /f "usebackqdelims=" %%i in ("My File With Source Folders.txt") do xcopy /seihry "%%i" "D:\output\bkup%destinationFolder%"
Text file with source folders is "My File With Source Folders.txt".
Taking the solution from this Microsoft forum thread:
To copy folder structure of another without copying files.
Here "ProjectsX" folder is replaced into "ProjectsY" - within SVN directory
/E - include empty directories
/XF * - all files are excluded
/XD ".svn" "obj" - specified directories are excluded
D:\SVN>robocopy ProjectsX ProjectsY /E /XF * /XD ".svn" "obj"
Thanks to the hint to robocopy in the previous answer by Endoro. But you don't need mirroring for this really.

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