2011-04-10 115 views
4

可能重複:
Windows batch (files)打印文件夾和文件遞歸使用Windows批量

你好。我試圖做一個批處理腳本,從命令行爲每個文件名打印文件夾的所有子文件夾,其中找到文件(我有作爲參數文件夾和文件名列表)。子文件夾名稱應按文件大小的降序排列(文件在不同子文件夾中可以有不同的大小)。 我找不到打印路徑的腳本。誰能幫忙?

@echo off 
REM check the numbers of parameters 
if "%2"=="" goto err1 
REM check: is first parameter a directory? 
if NOT EXIST %1\NUL goto err2 
set d=%1 
shift 
REM iterate the rest of the parametersa 


for %%i in %dir% (
find %dir /name %i > temp 

if EXIST du /b temp | cut /f 1 goto err3 
    myvar=TYPE temp 
    echo "file " %i "is in: " 

    for %%j in %myvar do 
    echo %j 

    echo after sort 
    du /b %myvar | sort /nr  
) 

:err1 
echo Two parameters are necessary 
goto end 
:err2 
echo First parameter must be a directory. 
:err3 
echo file does not exist. 
:end 
+0

這並不完全清楚你的意思。我知道給你一個文件夾名稱,後跟一個文件名列表,但我不確定你的意思是「文件夾的所有子文件夾,其中找到了文件」。每個文件的文件名應該在1個或多個子文件夾中,還是僅僅意味着您需要查找包含每個文件的*子文件夾(每個文件)? – 2011-04-10 17:23:40

+0

如果我有文件夾1和a.txt,b.txt,c.txt作爲參數,並在folder1我有a.txt和folder2(包含b.txt和c.txt)和folder3(包含b.txt和a.txt ),我必須打印文件a.txt:folder1/a.txt .......... folder1/folder2/a.txt b.txt:folder1/folder2/b.txt .... folder1/folder3/b.txt c.txt:folder1 folder2.c.txt .... folder1/folder3/c.txt – 2011-04-10 17:28:43

+0

「我必須打印」聽起來像作業。你爲什麼試圖用批處理文件來做到這一點? – Khez 2011-04-10 18:09:55

回答

1

這是一個腳本,有點做你想做的事,對於長時間的延遲抱歉。

@echo off 
REM application logic. askForDir. [askForFile => findFile]{infinity} 
:askForDir 
cls 
set basePath= 
echo Not supplying a directory will exit. Trailing slash please 
set /p basePath=Search in directory: 
if x%basePath%==x goto endApp 

REM Gets a file name, sets the curDir 
:askForFile 
cls 
set fileName= 
echo Currently searching in: '%basePath%' 
echo Not supplying a filename will return you to directory selection 
set /p fileName=Search for file: 
if x%fileName%==x goto askForDir 

REM We know what we're searching for and where 
set file=%basePath%%fileName% 
if exist %file% (
    echo %file% 
) 
REM check recursively for the fileName in each subfolder 
FOR /R %basePath% %%f IN (%fileName%) DO (
    echo %%f 
) 

REM Finished dir and all subs, askForFile again 
echo Done. 
pause>nul 
goto askForFile 

REM Finished 
:endApp 
cls 
echo We hope you enjoyed this presentation. 
pause>nul 

雖然作爲傑布指出,我建議你學習一些基礎知識,考慮到這是家庭作業,它不會幫助你,如果多你不實際學習如何做到這一點。試圖評論我的代碼儘可能多爲你理解,但嘗試閱讀更多關於using batch files

+0

謝謝。我會盡我所能。 – 2011-04-10 21:23:16

+0

+1,這是一個很好的樣本來找到這些文件,但不是一個完整的解決方案,因爲這是Radu的作業:-) – jeb 2011-04-10 21:24:48

+0

@jeb呵呵,我一直在爲自己掙扎,不給他一個完整的解決方案。最難的部分(至少按我的標準)我省略了。排序子文件夾將是殺手:D – Khez 2011-04-10 21:27:47

相關問題