Windows 批处理命令将文件从子目录移动到新目录
我正在尝试使用下面的批处理文件将文件从一个文件夹移动到另一个文件夹。批处理命令将根据源文件夹中每个文件的创建日期在目标文件夹中创建子文件夹 问题是源文件夹包含子文件夹,批处理命令无法递归到子文件夹中 请建议如何修改批处理文件以允许递归到源文件夹上的子文件夹中 谢谢 里亚莱酒店Windows 批处理命令将文件从子目录移动到新目录,windows,batch-file,cmd,Windows,Batch File,Cmd,我正在尝试使用下面的批处理文件将文件从一个文件夹移动到另一个文件夹。批处理命令将根据源文件夹中每个文件的创建日期在目标文件夹中创建子文件夹 问题是源文件夹包含子文件夹,批处理命令无法递归到子文件夹中 请建议如何修改批处理文件以允许递归到源文件夹上的子文件夹中 谢谢 里亚莱酒店 echo %1 "-" %2 If [%1]==[] ECHO "Source Directory parameter required"&GOTO :EOF If [%2]==[] ECHO "Target Di
echo %1 "-" %2
If [%1]==[] ECHO "Source Directory parameter required"&GOTO :EOF
If [%2]==[] ECHO "Target Directory parameter required"&GOTO :EOF
SET TrimQuote=%2
for /f "useback tokens=*" %%T in ('%2') do set TrimQuote=%%~T
REM echo %TrimQuote%
::loop through files only
For /F "TOKENS=1 DELIMS=%_TabSpace%" %%B In ('dir %1 /a-d /B /OD') DO (
REM echo "%%B - " %%B
For /F "TOKENS=1 DELIMS=%_TabSpace%" %%D In ('dir %1\"%%B" /a-d /OD ^| findstr /B [0-9][0-9]/[0-9]') DO (
REM echo "%%D - " %%D
for /F "tokens=1,2,3,4 delims=/ " %%b in ("%%D") do (
REM echo "b = " %2\%%c%%a\%%b
REM echo %2\%%d%%c\%%b
if NOT exist %2\%%d%%c\%%b md %2\%%d%%c\%%b
move %1\"%%B" %2\%%d%%c\%%b\
)
)
)
试用
For /F "TOKENS=1 DELIMS=%_TabSpace%" %%B In ('dir %1 /a-d /S /B /OD') DO (
/s
将导致递归。缺点是,dir
命令的输出是d:\path\file.ext
——这可能与您的“TOKENS=1 DELIMS=%\u TabSpace%”不匹配。您可能需要使用“delims=“
”(即,没有分隔符,因此在token1中使用整行)
然后,您可以检索完整文件名的各个部分,如%%~dB
、%%~pB
、%%~nB
和%%~xB
(驱动器、路径、naem和扩展名-如果愿意,您可以使用%%~nxB
作为名称+扩展名来组合这些部分
补充信息-批评论
@ECHO OFF
SETLOCAL
:: The above two lines are a traditional batch introduction.
:: The first turns `ECHO`ing of the command to the console OFF
:: The second makes all changes to the environment 'local'
:: which means that any variable changes made during the batch
:: will be undone at the end, restoring the original environment.
:: Note that the official remarks/comments method is
REM This is a remark
:: But the double-colon method is commonly used as :: is less intrusive
:: Echo the two parameters given to the batch (%1 and %2)
echo %1 "-" %2
:: The original parameter-present detection is weak. This is a better method
SET target=%~1
If not defined target ECHO "Source Directory parameter required"&GOTO :EOF
SET target=%~2
If not defined target ECHO "Target Directory parameter required"&GOTO :EOF
:: Note `trimquote` (batch is largely case-insensitive) is a meaningless name
:: New name TARGET is better. Setting to %~2 removes enclosing quotes from
:: string assigned to variable.
::loop through files only
:: `"delims="` means there are no delimiters, so the entire line is assigned to
:: the variable `%%B` (FOR loop variablenames ("metavariables") ARE case-sensitive!)
:: The line being assigned comes from the output of the `DIR` command
:: which is filenames only (/a-d) in subdirectories (/s) in basic form (/b)
:: (ie name only, no dates, sizes, headers or summary) and in order of date (/od)
For /F "DELIMS=" %%B In ('dir "%~1" /a-d /S /B /OD') DO (
REM echo "%%B - " %%B
REM within a FOR loop, better to use REM remarks than :: remarks (version-dependent)
REM I believe the intention of the original here was to pick up the filedate
REM It wouldn't work since FINDSTR is looking for lines that begin (/B) with
REM 2 digits, a slash and one digit, but the date format about to be processed...
REM For /F "TOKENS=1 DELIMS=%_TabSpace%" %%D In ('dir %1\"%%B" /a-d /OD ^| findstr /B [0-9][0-9]/[0-9]') DO (
REM echo "%%D - " %%D
REM Process date - 4 elements separated by space or /. Pick the last three
REM so implictly format is DAYNAME xx/yy/zz BUT the elements would be applied
REM to %%b, %%c, %%d, %%e
REM for /F "tokens=1,2,3,4 delims=/ " %%b in ("%%D") do (
for /F "tokens=1,2,3,4 delims=/ " %%a in ("%%~tB") do (
REM echo "b = " %2\%%c%%a\%%b
REM echo %2\%%d%%c\%%b
REM Make a new directory. 2>nul suppresses error message if already exists
md "%TARGET%\%%d%%c\%%b" 2>nul
move "%%B" "%TARGET%\%%d%%c\%%b\"
)
)
这真是一场噩梦-不知道你使用的是什么格式的日期,也不知道你想要什么格式的目标目录结构。这应该“平坦化”结构,因此任何文件filename.ext
都将被放置在%target%\xx\yy\zz
中,而不管该文件最初位于源结构中的何处。对于具有相同日期的同一filename.ext
的多个实例,也没有任何保护措施。N需要对整个场景进行更多的澄清才能更加确定。实际上,只需对现有(假定工作但明显有故障)批次进行注释和更改…尝试使用
For /F "TOKENS=1 DELIMS=%_TabSpace%" %%B In ('dir %1 /a-d /S /B /OD') DO (
/s
将导致递归。缺点是dir
命令的输出是d:\path\file.ext
-这可能与您的“TOKENS=1 DELIMS=%\u TabSpace%”
不匹配。您可能需要使用“DELIMS=“
”(即,没有分隔符,因此token1中的整行)
然后,您可以检索完整文件名的各个部分,如%%~dB
、%%~pB
、%%~nB
和%%~xB
(驱动器、路径、naem和扩展名-如果愿意,您可以使用%%~nxB
作为名称+扩展名来组合这些部分
补充信息-批评论
@ECHO OFF
SETLOCAL
:: The above two lines are a traditional batch introduction.
:: The first turns `ECHO`ing of the command to the console OFF
:: The second makes all changes to the environment 'local'
:: which means that any variable changes made during the batch
:: will be undone at the end, restoring the original environment.
:: Note that the official remarks/comments method is
REM This is a remark
:: But the double-colon method is commonly used as :: is less intrusive
:: Echo the two parameters given to the batch (%1 and %2)
echo %1 "-" %2
:: The original parameter-present detection is weak. This is a better method
SET target=%~1
If not defined target ECHO "Source Directory parameter required"&GOTO :EOF
SET target=%~2
If not defined target ECHO "Target Directory parameter required"&GOTO :EOF
:: Note `trimquote` (batch is largely case-insensitive) is a meaningless name
:: New name TARGET is better. Setting to %~2 removes enclosing quotes from
:: string assigned to variable.
::loop through files only
:: `"delims="` means there are no delimiters, so the entire line is assigned to
:: the variable `%%B` (FOR loop variablenames ("metavariables") ARE case-sensitive!)
:: The line being assigned comes from the output of the `DIR` command
:: which is filenames only (/a-d) in subdirectories (/s) in basic form (/b)
:: (ie name only, no dates, sizes, headers or summary) and in order of date (/od)
For /F "DELIMS=" %%B In ('dir "%~1" /a-d /S /B /OD') DO (
REM echo "%%B - " %%B
REM within a FOR loop, better to use REM remarks than :: remarks (version-dependent)
REM I believe the intention of the original here was to pick up the filedate
REM It wouldn't work since FINDSTR is looking for lines that begin (/B) with
REM 2 digits, a slash and one digit, but the date format about to be processed...
REM For /F "TOKENS=1 DELIMS=%_TabSpace%" %%D In ('dir %1\"%%B" /a-d /OD ^| findstr /B [0-9][0-9]/[0-9]') DO (
REM echo "%%D - " %%D
REM Process date - 4 elements separated by space or /. Pick the last three
REM so implictly format is DAYNAME xx/yy/zz BUT the elements would be applied
REM to %%b, %%c, %%d, %%e
REM for /F "tokens=1,2,3,4 delims=/ " %%b in ("%%D") do (
for /F "tokens=1,2,3,4 delims=/ " %%a in ("%%~tB") do (
REM echo "b = " %2\%%c%%a\%%b
REM echo %2\%%d%%c\%%b
REM Make a new directory. 2>nul suppresses error message if already exists
md "%TARGET%\%%d%%c\%%b" 2>nul
move "%%B" "%TARGET%\%%d%%c\%%b\"
)
)
这真是一场噩梦-不知道你使用的是什么格式的日期,也不知道你想要什么格式的目标目录结构。这应该“平坦化”结构,因此任何文件filename.ext
都将被放置在%target%\xx\yy\zz
中,而不管该文件最初位于源结构中的何处。对于具有相同日期的同一filename.ext
的多个实例,也没有任何保护措施。N需要对整个场景进行更多的澄清才能更加确定。实际上只是对现有场景进行评论和更改(假定可以正常工作,但显然有缺陷)批处理…您还可以使用XCOPY函数复制父文件夹和子文件夹。您还可以使用XCOPY函数复制父文件夹和子文件夹。什么是%\u TabSpace%
?什么是%\u TabSpace%
?请原谅我的无知,我对批处理脚本不太熟悉。您能解释一下DELIMS=\u TabS是什么吗速度%"表示。您能否提供一个示例,说明您在上面建议的更改中代码的外观。谢谢,Rialet。呵呵-离开几天,回到混乱中来。让我们面对现实吧-您根本不熟悉批处理脚本。没问题-每个人都必须从某个地方开始。我的印象是,从如果您发布了原始代码,您可能会熟悉它的含义。在任何语言中都有与其使用或内容相关的变量名,这是一种传统做法。仅从其名称来看,%\u TabSpace%
可能会包含一个制表符和一个空格,这似乎很奇怪,因为这是一些默认的分隔符,所以不需要使用指定。我将发布一个修订。请原谅我的无知,我对批处理脚本不太熟悉。您能解释一下DELIMS=%\u TabSpace%是什么吗"表示。您能否提供一个示例,说明您在上面建议的更改中代码的外观。谢谢,Rialet。呵呵-离开几天,回到混乱中来。让我们面对现实吧-您根本不熟悉批处理脚本。没问题-每个人都必须从某个地方开始。我的印象是,从如果您发布了原始代码,您可能会熟悉它的含义。在任何语言中都有与其使用或内容相关的变量名,这是一种传统做法。仅从其名称来看,%\u TabSpace%
可能会包含一个制表符和一个空格,这似乎很奇怪,因为这是一些默认的分隔符,所以不需要使用我会发布一个修正案。