Reputation: 83
I want to make a batch file who is doing the following things, search all file of a type from all my computer, but exclude some directories from searching, like Windows or Program Files and copy only files în one folder, without the parent/parents directory/directories.
I tried in 3 ways:
First with ROBOCOPY command
ROBOCOPY C:\ %location% *.jpg *.png /S /XD "Windows" "Program Files" /XJ /W:0 / R:0
It's working perfect but i don't need the parents directory of the files, i want all files who was find to copy în one folder.
Second way was with XCOPY
XCOPY /C /H /S /EXCLUDE: excludefiles.txt "%source%" "%destination%"
But the same problem, copy files with their parents directories.
And third way with For Loop, where i used same xcopy.
FOR "eol=| delims=" %%f IN (*.jpg,*.png) DO ( XCOPY /C /H /S /EXCLUDE:excludefiles.txt "%source%\%%~nxf" "%destination%"
But same problem, i tried in for loop and with copy, but copy doesn't have options to search in subdirectories, what for me it's also a problem.
So if you can help me with this problem I will thank you.
Upvotes: 1
Views: 1206
Reputation: 34909
I messed around a bit and came up with the following script (see all the explanatory rem
remarks):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "SOURCE=D:\" & rem // (root directory of source location)
set "TARGET=H:\copydata\" & rem // (target location)
set PATTERNS="*.jpg","*.png" & rem // (list of file patterns)
set EXCLUSIONS="D:\dontcopy\","\ignoreit\" & rem // (list of exclusions)
set "EXCLFILE=exclusions.lst" & rem /* (name of temporary exclusions file;
rem this must not contain spaces!) */
set "EXCLFDIR=%~dp0" & rem /* (location of temporary exclusions file;
rem `%~dp0` is the parent dir. of this script) */
rem // Create temporary exclusions file:
cd /D "%EXCLFDIR%"
if defined EXCLUSIONS (
> "%EXCLFILE%" (
for %%E in (%EXCLUSIONS%) do (
echo(%%~E
)
)
rem // Build exclusions option for `xcopy`:
set "EXCLUSIONS=/EXCLUDE:%EXCLFILE%"
)
rem /* Create target directory to ensure it exists
rem (if the target does not exist, `xcopy` prompts for
rem whether it is a File or a Directory; if it exists,
rem no prompt appears; `2> nul` surpresses error message
rem of `md` in case the target already exists): */
2> nul md "%TARGET%"
rem // Enumerate all files in source recursively:
for /F "eol=| delims=" %%F in ('
cd /D "%SOURCE%" ^& dir /B /S /A:-D %PATTERNS%
') do (
rem // Do the actual flat copying here:
(
rem /* Surpress output (also `1 File(s)`) by `> nul`
rem (`xcopy` outputs the copied file plus the summary
rem `1 File(s)`; to avoid that summary after each file,
rem the entire output of `xcopy` is surpressed): */
> nul xcopy /L /Y /C /H /K %EXCLUSIONS% "%%~F" "%TARGET%"
rem /* (after testing, remove `/L` to do actual copying) */
) && (
rem /* Return every copied file to see what has been copied;
rem `&&` lets `echo` execute inly if `xcopy` succeeded: */
echo(%%~F
)
)
rem // Delete temporary exclusions file:
del "%EXCLFILE%"
endlocal
exit /B
You do not need to create an exclusions file for xcopy
(as in your similar previous question), because that file is created automatically by the script, temporarily. You simply need to define all the file and directory locations, file patterns and exclusions in the section with the Define constants here:
remark.
After having tested whether the output lists all files you want to copy, you need to remove the /L
switch from the xcopy
command in order to actually copy and files.
Upvotes: 1
Reputation: 4506
I'd use the last command with a minor change:
FOR /F "delims=" %%f IN ('dir /s/b *.jpg,*.png') DO ( XCOPY /H /S "%%~f" "%destination%")
This will loop over all files listed by dir /s/b *.jpg,*.png
there are additional attributes for hidden / system files etc.
Then it simply copies everything into the flat destination folder. Note: the destination folder must end in a \
----- edit - repeating for another partition ------
set destination=c:\temp\batches\dir3\
FOR /F "delims=" %%f IN ('dir /s/b c:\*.jpg') DO call :copyWithExcludes %%f
FOR /F "delims=" %%f IN ('dir /s/b d:\*.png') DO call :copyWithExcludes %%f
pause
goto:eof
:copyWithExcludes
XCOPY /H /S /Y "%~1" "%destination%"
goto:eof
Upvotes: 0