2016-12-06 81 views
2

我的查詢解決了一個大圖像目錄,有一系列複雜的子目錄,需要執行查找文件列表以及移動所述文件。批處理:使用查找文件搜索目錄樹並移動文件

我發現我在計算器上和其他支持論壇請求幾個可能的解決方案。這是我目前的解決方案:

@echo off 
set Source=C:\users\directory 
set Target=C:\users\target 
set FileList=C:\users\lookup\list.txt 
echo. 

if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit 
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit 
if not exist "%Target%" md "%Target%" 

for /F "delims=" %%a in ('type "%FileList%"') do move "%Source%\%%a" "%Target%" 

:Exit 
echo. 
echo press the Space Bar to close this window. 
pause > nul 

這工作完全正常,但只針對父目錄。我怎樣才能從父目錄中找到這個批處理腳本從上到下搜索並查找查找列表中提供的所有匹配文件?

+1

'爲/ F 「delims =」 %%一個在( '型 「%文件列表%」')做/ F 「delims =」 %% b在('DIR/B/S/A:-D「 %Source%\ %% a「')會移動」%% b「」%Target%「' – aschipfl

回答

0

這裏是您的代碼的改進版本,包括遞歸搜索源目錄的解決方案。爲此,我假定目標目錄是平坦的,所以不要從源目錄複製子目錄。因此,這裏是代碼:

@echo off 
set "Source=C:\users\directory" 
set "Target=C:\users\target" 
set "FileList=C:\users\lookup\list.txt" 
echo/ 

if not exist "%Source%" echo Source folder "%Source%" not found & goto :Quit 
if not exist "%FileList%" echo File list "%FileList%" not found & goto :Quit 
2> nul md "%Target%" 

for /F usebackq^ delims^=^ eol^= %%a in ("%FileList%") do (
    for /F "delims=" %%b in ('dir /B /S /A:-D "%Source%\%%a"') do (
     move "%%b" "%Target%" 
    ) 
) 

:Quit 
echo/ 
echo Press the Space bar to close this window. 
pause > nul 

除了一些小的語法改進,主要的變化是額外的嵌套for /F %%b循環,它解析dir命令的輸出,這在每個文件名依次搜索由%%a也給在源目錄的子目錄中。


如果您想在源目錄中各個子目錄複製到目標目錄,所以目標目錄是遞歸,腳本需要進行調整:

@echo off 
set "Source=C:\users\directory" 
set "Target=C:\users\target" 
set "FileList=C:\users\lookup\list.txt" 
echo/ 

if not exist "%Source%" echo Source folder "%Source%" not found & goto :Quit 
if not exist "%FileList%" echo File list "%FileList%" not found & goto :Quit 
for /D %%d in ("%Source%") do set "Source=%%~fd" 
2> nul md "%Target%" 

for /F usebackq^ delims^=^ eol^= %%a in ("%FileList%") do (
    for /F "delims=" %%b in ('dir /B /S /A:-D "%Source%\%%a"') do (
     set "Item=%%b" 
     set "Parent=%%~dpb." 
     setlocal EnableDelayedExpansion 
     set "Parent=!Parent:*%Source%=.!" 
     2> nul md "!Target!\!Parent!" 
     move "!Item!" "!Target!\!Parent!" 
     endlocal 
    ) 
) 

:Quit 
echo/ 
echo Press the Space bar to close this window. 
pause > nul 

這是我做過什麼:

  • 添加的行for /D %%d in ("%Source%") do set "Source=%%~fd",它什麼也不做,但得到的源目錄的完整絕對和解決路徑從給定的一個;因爲有在路徑上做了一些字符串操作,這是必要的(例如,C:\USERS\.\directory相當於C:\Users\any\..\Directory作爲路徑,儘管它們是不同的字符串;所述for /D命令行解析這樣的路徑到C:\users\directory);
  • for /F %%b環的內部,%%b被存儲在可變Item,從而構成完整的路徑; Parent存儲追加\.的父目錄的完整路徑;例如,如果當前項目是C:\users\directory\subdir\file.ext,則Parent包含C:\users\directory\subdir\.;
  • delayed variable expansion被啓用,因爲變量ItemParent設置爲在相同的代碼塊內讀取;它通常不會啓用,但會在循環中切換,以便在路徑中出現感嘆號時不會造成任何麻煩;
  • set "Parent=!Parent:*%Source%=!"移除路徑存儲在Parent源目錄TROM;例如,Parent路徑C:\users\directory\subdir\.變得.\subdir\.當源目錄是C:\users\directory,所以Parent變得相對;
  • 2> nul md "!Target!\!Parent!"確保Parent目錄中的目標目錄中存在(2> nul抑制了潛在的錯誤消息);
  • 最後,運動完成;例如,當前項目爲C:\users\directory\subdir\file.ext,目標目錄爲C:\users\target時,將其移至目錄C:\users\target\.\subdir\.,相當於C:\users\target\subdir;

注意,源目錄不得包含任何!=字符腳本的工作!

相關問題