2016-08-16 99 views
2

我要超過1000個文件從源文件夾類似如何根據文件和文件夾名稱的前15個字符將文件複製到文件夾中?

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 

複製到正確的目的地文件夾。該文件夾名稱 - 基於另一種常規 - 長,僅前15個字符是與每個文件名的前15個字符相同,如:

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

我找了一個程序來將文件複製到文件夾,如sourcefolder\prod_de_1652899.xlsx分成destination\prod_de_1652899_tool_big\

在這裏任何人都有一個好主意批/腳本?

+1

的可能的複製([批處理文件夾到其他文件夾取決於名字放] http://stackoverflow.com/questions/38970982/batch-file-把文件夾放入其他文件夾 - 取決於名稱) – SomethingDark

+0

看看這個鏈接http://www.coviantsoftware.com/blog/2014/12/copy-files-multiple-destinations/也許你可以修改代碼來滿足您的需求。 – Jonas

回答

0

我建議使用這種評論批次代碼完成這個任務:

@echo off 
setlocal EnableExtensions 
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 
goto :EOF 


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 

對於理解使用的命令以及它們如何工作,打開命令提示符窗口中,執行有下面的命令,並完全閱讀所有幫助非常仔細地顯示每個命令的頁面。

  • call /?
  • copy /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • rem /?
  • set /?
  • setlocal /?
相關問題