[英]Make subfolder names from part of file name and copy files with Robocopy
Is it posible to copy and make directories automatically from file name substrings using Robocopy? 是否可以使用Robocopy从文件名子字符串自动复制和建立目录?
I mean i have files like these. 我的意思是我有这样的文件。 LAJ00306130201004626.rc
the first 8 chararacters are control number ( LAJ00306
=control number) this would be the name of the folder and the rest are the date and time (Date=130201) (time=004626). LAJ00306130201004626.rc
的前8个字符是控制编号 ( LAJ00306
=控制编号),这是文件夹的名称,其余是日期和时间(日期= 130201)(时间= 004626)。
LAJ00306130201004626.rc LAJ00306130202004626.rc LAJ00306130203004626.rc LAJ00307130201004626.rc LAJ00307130202004626.rc
and i would like to copy and create folders from the file name like under and copy the files mentioned before in the new folders. 我想从下面的文件名复制和创建文件夹,然后在新文件夹中复制前面提到的文件。
LAJ00306
LAJ00307
I hope to be clear if necessary ask me for more information 如果有必要,我希望弄清楚,请问我更多信息
try this, look at the output and remove the echo
s before MD
and ROBOCOPY
, if it looks good: 试试看,看一下输出并删除MD
和ROBOCOPY
之前的echo
,如果看起来不错:
@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcefolder=."
SET "targetfolder=X:\data"
CD /d "%sourcefolder%"
FOR %%a IN (*.rc) DO (
SET "fname=%%a"
SET "folder=!fname:~0,8!"
SET "$!folder!=1"
)
FOR /f "delims=$=" %%a IN ('set "$"') DO (
ECHO MD "%targetfolder%\%%a" 2>nul
ECHO ROBOCOPY "%sourcefolder%" "%targetfolder%\%%a" "%%a*.rc"
)
Set sourcefolder
and targetfolder
for your folder tree. 为您的文件夹树设置sourcefolder
和targetfolder
。
Try this: 尝试这个:
@echo off
pushd "c:\source folder"
setlocal enabledelayedexpansion
for %%a in (*.rc) do (
set "name=%%a"
robocopy "%cd%" "%%a" "D:\target directory\!name:~0,8!"
)
popd
Answers to your questions are: 您的问题的答案是:
pushd "drive:\\path"
makes the location the current working directory. pushd "drive:\\path"
将位置pushd "drive:\\path"
当前工作目录。
popd
restores the last working directory popd
恢复上一个工作目录
setlocal enabledelayedexpansion
allows you to change and use variables within a loop, using the !variable! setlocal enabledelayedexpansion
允许您使用!variable!在循环中更改和使用变量。 syntax. 句法。
If your 2000 files are in a single folder then it should work - but test it on some sample files first so that you can see how it will work. 如果您的2000个文件位于单个文件夹中,那么它应该可以工作-但首先在一些示例文件上对其进行测试,以便您可以看到它如何工作。
@ECHO OFF
SETLOCAL
SET "sourcedir=."
SET "destdir=c:\destdir"
FOR /f "tokens=1*delims=_" %%i IN (
'dir /b /a-d "%sourcedir%\*_*."'
) DO XCOPY /b "%sourcedir%\%%i_%%j" "%destdir%\%%i\"
GOTO :EOF
This should accomplish the task described. 这样就可以完成所描述的任务。 You'd need to set up the source and destination directories to suit, of course. 当然,您需要设置适合的源目录和目标目录。 Add >nul
to the end of the XCOPY
line to suppress 'copied' messages. 在XCOPY
行的末尾添加>nul
以禁止显示“复制”消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.