2012-07-18 150 views
2

我有一個主文件(File1.txt),其中一些名稱保留在內容中。我必須找到文件夾中具有這些名稱(通配符)的所有文件,並使用批處理文件程序將它們移動到不同的文件夾。從文件內容複製文件的批處理文件

如:File1.txt有內容

abcd 
    efgh 

現在的文件夾中說c:\temp\Source我有文件,就像

12abcd34.asc 
56efgh78.asc 
testing.asc 

我要搬到只有2個文件到文件夾說C:\ TEMP \目標。

這是我的代碼,但它給出錯誤說我* * *在這個時候是意想不到的。你能幫忙嗎?

@Echo Off 
title Test move files 
set dir1=C:\temp\Source 
dir %dir1% 
Echo Directory Changed 
FOR /f "eol=; delims=, " %i in (file1.txt) do move /y "*%i*.*" Target 
+0

在批處理文件中,'%i'必須有%加倍,如'%% i'。 – lavinio 2012-07-18 23:12:27

回答

1

在這裏你去....

這是目錄結構是什麼,當我開始......

C:\Temp>tree /f 
Folder PATH listing for volume OS 
Volume serial number is XXXX-XXXX 
C:. 
│ file1.txt 
│ run.bat 
│ 
├───Source 
│  12abcd34.asc 
│  56efgh78.asc 
│  testing.asc 
│ 
└───Target 

這是我將在以後運行的run.bat。 。包括bug修復...

C:\Temp>copy run.bat con 
@Echo Off 

title Test move files 

set dir1=Source 

dir %dir1% 

Echo Directory Changed 

FOR /f "eol=; delims=, " %%i in (file1.txt) do move /y "%dir1%\*%%i*.*" Target 
     1 file(s) copied. 

現在我運行該批處理文件...

C:\Temp>run.bat 
Volume in drive C is OS 
Volume Serial Number is XXXX-XXXX 

Directory of C:\Temp 

19/07/2012 00:03 <DIR>   . 
19/07/2012 00:03 <DIR>   .. 
18/07/2012 23:59     0 12abcd34.asc 
18/07/2012 23:59     0 56efgh78.asc 
18/07/2012 23:59     0 testing.asc 
       3 File(s)    0 bytes 
       2 Dir(s) 41,653,194,752 bytes free 
Directory Changed 
C:\Temp\Source\12abcd34.asc 
     1 file(s) moved. 
C:\Temp\Source\56efgh78.asc 
     1 file(s) moved. 

現在,這是最終的目錄結構...所以你可以看到,這是工作......

C:\Temp>tree /f 
Folder PATH listing for volume OS 
Volume serial number is XXXX-XXXX 
C:. 
│ file1.txt 
│ run.bat 
│ 
├───Source 
│  testing.asc 
│ 
└───Target 
     12abcd34.asc 
     56efgh78.asc 

下面是for循環,你需要...

FOR /f "eol=; delims=, " %%i in (file1.txt) do move /y "%dir1%\*%%i*.*" Target 

變化:

[1] within FOR you use %%i not %i. 
[2] You need this format: 

%dir1% <-- Where 
\  <-- path delimiter 
*  <-- starts with anything 
%%i  <-- contains what you want to search 
*.*  <-- ends with anything 

希望這會有所幫助。

+0

哇!感謝您的快速響應和幫助。其實我的第一個錯誤是file1.txt不在批處理文件所在的位置。他們在2個不同的地方,當我嘗試%時,它說沒有找到文件。 – kissac 2012-07-18 23:35:24

+0

感謝Prashant Gupta,這是非常有幫助! – kissac 2012-07-18 23:41:37

+0

很高興成爲服務kissac – chkdsk 2012-07-18 23:58:42