我最近不得不解決這個問題,很多,我想從層級轉向到一個文件夾中的文件具有相同的名稱作爲對方,我想還是扁平化層級沒有他們被覆蓋。 我所做的是寫一個移動文件的腳本,但在名稱與老層級路徑其重命名爲 例如: 源文件:
C:\文件\ somefiles \ file.txt的
C:\文件\ otherfiles \ file.txt的
目的地是C:\ NEWDIR \ 文件被創建爲
C:\ NEWDIR \ somefiles-file.txt的
C:\ NEWDIR \ otherfiles-file.txt的
這裏是代碼,批處理文件1變爲直通文件,批處理文件2重命名和移動它們(也可以直接複製,如果你想保留源:
@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%"
來源
2014-09-12 09:45:35
Max
可能重複[如何從文件夾樹中刪除所有與Robocopy文件夾的文件?](http://stackoverflow.com/questions/1502170/how-to-copy-files-from-folder-tree-dropping -all-the-folders-with-robocopy) – 2011-12-31 19:59:53