如何根据文件和文件夹名称的前15个字符将文件复制到文件夹中?
问题描述:
我要超过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\
。
在这里任何人都有一个好主意批/脚本?
答
我建议使用这种评论批次代码完成这个任务:
@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 /?
的可能的复制([批处理文件夹到其他文件夹取决于名字放] http://stackoverflow.com/questions/38970982/batch-file-把文件夹放入其他文件夹 - 取决于名称) – SomethingDark
看看这个链接http://www.coviantsoftware.com/blog/2014/12/copy-files-multiple-destinations/也许你可以修改代码来满足您的需求。 – Jonas