2

I want to copy more than 1000 files from a source folder like

sourcefolder\prod_de_7290022.xlsx
sourcefolder\prod_de_1652899.xlsx
sourcefolder\prod_de_6272899.xlsx
sourcefolder\prod_de_6189020.xlsx
sourcefolder\prod_de_7290022.wav
sourcefolder\prod_de_1652899.wav
sourcefolder\prod_de_6272899.wav
sourcefolder\prod_de_6189020.wav
sourcefolder\prod_de_7290022_mark.xlsx
sourcefolder\prod_de_1652899_mark.xlsx
sourcefolder\prod_de_6272899_mark.xlsx
sourcefolder\prod_de_6189020_mark.xlsx

to the right destination folder. The folder names are - based on another routine - long and only the first 15 characters are identical with the first 15 characters of each file name, like:

destination\prod_de_1652899_tool_big\
destination\prod_de_6272899_bike_red\
destination\prod_de_6189020_bike-green\
destination\prod_de_7290022_camera_good\

I am looking for a routine to copy the files into the folder, like sourcefolder\prod_de_1652899.xlsx into destination\prod_de_1652899_tool_big\.

Is here anyone with a good idea for a batch/script?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 1
    Possible duplicate of [Batch file put folders into other folders depending on name](http://stackoverflow.com/questions/38970982/batch-file-put-folders-into-other-folders-depending-on-name) – SomethingDark Aug 16 '16 at 20:15
  • Check out this link http://www.coviantsoftware.com/blog/2014/12/copy-files-multiple-destinations/ Perhaps you can modify that code to suit your needs. – Jonas Aug 17 '16 at 06:50

1 Answers1

0

I suggest to use this commented batch code for this task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFolder=sourcefolder"
set "TargetFolder=destination"

rem Call subroutine CopyFile for each non hidden and
rem non system file found in specified source folder
rem and then exit processing of this batch file.

for %%I in ("%SourceFolder%\*") do call :CopyFile "%%~fI"

endlocal
exit /B 2>nul


rem This is a subroutine called for each file in source folder.
rem It takes the first 15 characters from each file name passed
rem to this subroutine via first parameter and search for a
rem folder in target folder starting with same 15 characters.
rem If such a folder is found, the file is copied to this folder
rem and the subroutine is exited.
rem Otherwise a new folder is created for the file and if
rem this is indeed successful, the file is copied into the
rem newly created folder with an appropriate message.

:CopyFile
set "FileName=%~n1"
set "DirectoryName=%FileName:~0,15%"
for /D %%D in ("%TargetFolder%\%DirectoryName%*") do (
    copy /B /Y %1 /B "%%~D\" >nul
    goto :EOF
)

set "NewFolder=%TargetFolder%\%DirectoryName%_new"
md "%NewFolder%"
if exist "%NewFolder%\" (
    echo Created new folder: %NewFolder%
    copy /B /Y %1 /B "%NewFolder%\" >nul
    goto :EOF
)

echo Failed to create folder: %NewFolder%
echo Could not copy file: %1
goto :EOF

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • copy /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • rem /?
  • set /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143