批量创建文件夹,然后使用文本文件重新定位现有文件夹和子文件夹

时间:2013-02-12 06:18:50

标签: batch-file

我在......的结构中有一堆文件夹(和子文件夹)。

Test/Student001/ABC,
Test/Student001/DEF,
Test/Student002/ABC,
Test/Student002/DEF, etc...

我需要做的是将这些文件夹(及其子文件夹和文件)重新定位到另一个位置,以便它就像......

Test/Class01/ArronAmos(Student001)/ABC
Test/Class01/ArronAmos(Student001)/DEF
Test/Class02/BrettBesty(Student002)/ABC
Test/Class02/BrettBesty(Student002)/DEF

我的文本文件包含所有文件夹(和子文件夹)的原始名称和新名称(如此保存)..

studentdata.txt

A (studentcode), B (studentnewname), C (Class)

Student001, ArronAmos (Student001), Class01

Student002, BrettBesty (Student002), Class02

有没有办法让批次基本上像这样(使用上面的文本文件中的A,B和C - 如果可能的话,最好是一个txt文件)......

md 'C' ::which will skip past if folder exists

rename folder 'A' to 'B' ::only rename of root folder and leave subfolders intact

move folder 'B' to 'C' ::move new named folder (B) and all subfolders and contents to new root folder (C)

新目录和子文件夹(适用于新学生和未来学生)的创建就像这样并且工作得很好(除非有一种方法可以为子文件夹创建调用第二个文本文件而不是编码,这将是非常棒的 - 但我觉得没什么大不了的......)

创建批次

 cd /d %~dp0 pause

FOR /F "delims=~" %%f in (dirlist.txt) DO md "%%f"

:: This code creates directories referenced from a .txt file: - :: FOR /F "delims=~" %%f in (dirlist.txt) DO MD "%%f"

pause

FOR /D %%x in (*) do mkdir "%%x\Individual Behaviour Plans" "%%x\Individual Learning Plans" "%%x\Student Reports" "%%x\Student Support Group Meetings"

:: This code creates a new dir within every folder in batch location: - :: FOR /D %%x in (*) do mkdir "%%x\value"

pause

这是我从其他技术人员那里收到的重命名批处理并且不太了解或知道修改它以使其正常工作..

*rename_users.bat** :: Script to Rename folders - prefixing from a text file

setlocal ENABLEDELAYEDEXPANSION

Set Rootfolder=Test Set Names=names.txt

:: Goto Root Folder cd /d %~dp0

:: Start Line Counter Set LineCount=0

           :: For Every folder in the directory
           For /d /r %%g in (*) DO (
                           :: Increment the line counter by 1 (see the use of "!" >instead of "%" due to delayed expansion)
                           Set /a LineCount=!Linecount!+1
                           :: Call the Rename Folder sub - passing through the >variables of folder name and line counter
                           Call:RenameFolder %%g !LineCount!)
:RenameFolder :: For all of the tokens in the findstr on the names file for /f "Tokens=1* delims=:" %%a in ('findstr /n "^" "%Names%"') DO ( :: If the line counter matches the line number If %%a==%~2 ( :: Rename the Folder Move "%~1" "%~1 %%b") ) ::Return to the Primary 
Goto:EOF

Set Rootfolder= Set Names= Set linecount= Set Drive=

Endlocal

诀窍是我们不能只使用创建目录(和子目录)批处理文件,因为原始格式中存在一些文件夹,其中包含我们需要位于新结构子文件夹中的数据...和手动移动它们是一种选择......如果没有900多个学生文件夹也可以这样做......

我希望能有某种形式的感觉......谢谢你们!

1 个答案:

答案 0 :(得分:0)

下面的批处理文件可以执行您想要的操作:

@echo off
setlocal EnableDelayedExpansion

rem Enter into working directory
cd C:/Test

rem Process the file with original and new names
for /F "skip=1 tokens=1-3 delims=," %%a in (studentData.txt) do (

   rem Get studentNewName end eliminate spaces
   set "studentNewName=%%b"
   set "studentNewName=!studentNewName: =!"

   rem If the new folder not exist, create it
   if not exist "%%c/!studentNewName!" md "%%c/!studentNewName!"

   rem Move files from old folder 
   move "%%a/*.*" "%%c/!studentNewName!"

   rem And delete old empty folder
   rd "%%a"

)

安东尼奥