如何根据文件名和文件夹名的前15个字符将文件复制到文件夹中? [英] How to copy files into folders based on first 15 characters of file and folder name?
问题描述
我想从源文件夹中复制1000多个文件
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
到正确的目标文件夹.文件夹名称-基于另一个例程-长,并且只有前15个字符与每个文件名的前15个字符相同,例如:
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\
我正在寻找将文件复制到文件夹的例程,例如sourcefolder\prod_de_1652899.xlsx
到destination\prod_de_1652899_tool_big\
.
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?
推荐答案
我建议将此注释批处理代码用于此任务:
I suggest to use this commented batch code for this task:
@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
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
-
call /?
-
copy /?
-
echo /?
-
endlocal /?
-
for /?
-
goto /?
-
if /?
-
md /?
-
rem /?
-
set /?
-
setlocal /?
call /?
copy /?
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
rem /?
set /?
setlocal /?
这篇关于如何根据文件名和文件夹名的前15个字符将文件复制到文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!