Gần đây tôi đã phải giải quyết vấn đề này, và nhiều tác phẩm mà tôi muốn chuyển sang từ hệ thống phân cấp vào một thư mục duy nhất có cùng tên với nhau, và tôi muốn vẫn san bằng hệ thống cấp bậc mà không cần họ bị ghi đè. Những gì tôi đã làm là viết một kịch bản mà di chuyển các tập tin, nhưng đổi tên nó với đường dẫn hệ thống cấp bậc cũ trong tên ví dụ: nguồn tập tin:
C: \ file \ somefiles \ file.txt
C: \ file \ otherfiles \ file.txt
đích là C: \ newdir \ tập tin được tạo ra như
C: \ newdir \ somefiles-file.txt
C: \ newdir \ otherfiles-file.txt
đây là mã, hàng loạt tập tin 1 đi qua các tập tin, tập tin thực thi 2 đổi tên và di chuyển chúng (cũng có thể sao chép thay vào đó, nếu bạn muốn giữ nguồn:
@echo off
for /r %%f in (*.*pr) do @renameandmovefilespart2.bat "%%f" "%%~ff" "%%~xf"
renameandmovefilespart2.bat
@echo off
Setlocal EnableDelayedExpansion
rem set the whole file path
set origWhole=%1
set origPathOnly=%2
set extension=%3
rem here you can set where the directory to hold the flattened hierarchy is
set destDir=c:\destinationDir\
rem set the directory to do a string replace
rem make this the starting directory, that you dont want in the newly renamed files
set startingDir=C:\starting\directory\
set nothing=
set slash=\
rem here you can set what the character to represent the directory indicator \ in the new files
set reaplcementDirectoryCharacter=--
set quote="
rem cut out the starting part of the directory
call set newname=%%origWhole:!startingDir!=!nothing!%%
rem replace slashes with new character
call set newname=%%newname:!slash!=!reaplcementDirectoryCharacter!%%
rem remove quotes
call set newname=%%newname:!quote!=!nothing!%%
rem @echo shortened: %newname%
rem @echo source path: %origPathOnly% newPath: %startingDir%
rem @echo extension: %extension%
rem rename the files
ren %origWhole% %newname%
rem prepare to move the file, clean up the source path
call set origPathOnly=%%origPathOnly:!quote!=!nothing!%%
move "%origPathOnly%%newname%" "%destDir%"
bản sao có thể có của [Cách sao chép tệp từ cây thư mục thả tất cả các thư mục bằng Robocopy?] (Http://stackoverflow.com/questions/1502170/how-to-copy-files-from-folder-tree-dropping -all-the-folders-with-robocopy) –