批处理文件循环文本文件的内容,创建变量并移动具有部分名称的文件

时间:2013-06-11 15:44:19

标签: windows batch-file

我有一个文件夹区域,每个文件夹中有数百个文档,结构如下:

C:\myLibrary\Accident Investigation Report
C:\myLibrary\Address Change
C:\myLibrary\Medical Certificate
C:\myLibrary\New Starter

这些文件夹中的文件名称如下例所示(基于日期戳,时间戳,部门编号,报告类型,员工姓名)。

2013-06-06 16-28-59 165 Accident Investigation Report - J Bloggs.xml
2013-06-06 16-28-59 165 Accident Investigation Report - J Bloggs.pdf

2013-06-11 15-38-07 147 Address Change - L Test.xml
2013-06-11 15-38-07 147 Address Change - L Test.pdf

(每个都在他们自己明显的标题文件夹中)

识别文件来自哪个部门号码(重要位)是日期戳后面的数字&时间戳 - 在上面的例子中是165&分别为147。

我想根据他们的部门号码将所有文件移动到另一个设置文件夹结构(已经创建了)(每个部门都有另一个类似的结构如下所示);

C:\Dept Structure\165man\Accident Investigation Report
C:\Dept Structure\165man\Address Change

因此,正如批处理文件运行后的上述示例文件一样,我应该最终得到;

C:\Dept Structure\165man\Accident Investigation Report\2013-06-06 16-28-59 165 Accident Investigation Report - J Bloggs.pdf
C:\Dept Structure\165man\Accident Investigation Report\2013-06-06 16-28-59 165 Accident Investigation Report - J Bloggs.xml
C:\Dept Structure\165man\Address Change\2013-06-06 16-28-59 165 Address Change - J Bloggs.pdf
C:\Dept Structure\165man\Address Change\2013-06-06 16-28-59 165 Address Change - J Bloggs.xml

C:\Dept Structure\147man\Accident Investigation Report\2013-06-11 15-38-07 147 Accident Investigation Report - L Test.pdf
C:\Dept Structure\147man\Accident Investigation Report\2013-06-11 15-38-07 147 Accident Investigation Report - L Test.xml
C:\Dept Structure\147man\Address Change\2013-06-11 15-38-07 147 Address Change - L Test.pdf
C:\Dept Structure\147man\Address Change\2013-06-11 15-38-07 147 Address Change - L Test.xml

我有一个包含所有部门经理编号的文本文件,例如

003man
004man
005man
006man
007man
008man
009man
etc
etc
410man

由此我想循环遍历文本文件,根据前3位数创建变量,添加' Accident'或者到这个变量的末尾,检查第一个C:\ myLibrary \事故调查报告'在文件夹中,找到标题中包含该变量的任何文件,并将其移至“结构”部分'上面命名的目录 - phew !!

我设法找到一些几乎'完成这看起来非常简单;

`cd /D "C:\Test"`

`for /F %%i in (C:\Dept.txt) do set str1=%%i`

`set _dept=%str1%`
`set _dept=%_dept:~0,3%`
`set "str2=%_dept%`

`move "????????????????????%str2%*.*" "C:\Dept Structure\%str1%\Accident Investigation Report"`

它确实会将一些文件移动到正确的位置,但是,这似乎贯穿整个文本文件并且只处理最后一行!因此,所有其他行都保持不变,剩下的文件也没有移动。

2 个答案:

答案 0 :(得分:4)

测试 - 顶部创建测试文件和文件夹。然后它会移动文件,因为您似乎希望它们被移动。

@echo off
md "c:\mylibrary2\Accident Investigation Report\" 2>nul
md "c:\mylibrary2\Address Change\" 2>nul
type nul > "c:\mylibrary2\Accident Investigation Report\2013-06-06 16-28-59 165 Accident Investigation Report - J Bloggs.pdf"
type nul > "c:\mylibrary2\Accident Investigation Report\2013-06-06 16-28-59 165 Accident Investigation Report - J Bloggs.xml"
type nul > "c:\mylibrary2\Address Change\2013-06-06 16-28-59 165 Address Change - J Bloggs.pdf"
type nul > "c:\mylibrary2\Address Change\2013-06-06 16-28-59 165 Address Change - J Bloggs.xml"
type nul > "c:\mylibrary2\Accident Investigation Report\2013-06-11 15-38-07 147 Accident Investigation Report - L Test.pdf"
type nul > "c:\mylibrary2\Accident Investigation Report\2013-06-11 15-38-07 147 Accident Investigation Report - L Test.xml"
type nul > "c:\mylibrary2\Address Change\2013-06-11 15-38-07 147 Address Change - L Test.pdf"
type nul > "c:\mylibrary2\Address Change\2013-06-11 15-38-07 147 Address Change - L Test.xml"



for /f "delims=" %%a in ('dir "c:\mylibrary2" /a-d /b /s') do (
for /f "tokens=3" %%b in ("%%~nxa") do (
for /f "delims=" %%c in ("%%~dpa\.") do (
md "C:\Dept Structure2\%%bman\%%~nxc\" 2>nul
move "%%a" "C:\Dept Structure2\%%bman\%%~nxc\" >nul
)
)
)

答案 1 :(得分:2)

对不起。我不明白dept管理器数字文件用于什么。如果没有所有行的文件,则处理此列表中的所有行是浪费时间。如果文件不在此列表中并且您想省略它们,则此文件将非常有用,但您没有提及此点。

有几种方法可以解决这个问题。下面的批处理文件假定您在问题中解释的所有名称都没有错误。

@echo off
setlocal EnableDelayedExpansion

rem Process all folders in:
cd "C:\Dept Structure"
for /D %%a in (*) do (
   set "dept=%%a"                         // For example: "165man"
   set "dept=!dept:~0,3!"                 // For example: "165"
   pushd %%a
   rem Process all folders here, ie: "Accident Investigation Report" "Address Change"
   for /D %%b in (*) do (
      rem Move all existent files from source folder to this folder
      move "C:\myLibrary\%%b\*!dept! %%b - *.*" "%%b" 2> NUL
   )
   popd
)