2012-02-03 41 views
0

我想重命名上傳到另一臺服務器的文件,從擴展名.txt到.txt_mvd,並移到不同的目錄以便在Windows批處理模式下進行歸檔。任何人都可以幫助什麼Windows批處理腳本應該是什麼?如何重命名文件並將文件移動到新目錄

謝謝。

+0

已嘗試使用Windows PowerShell。這應該會讓你輕鬆。 – jake 2012-02-04 10:17:55

回答

2

下面是代碼

FOR /R C:\your_folder %%d IN (*.txt) DO (
    ren %%d %%~nd.txt_mvd 
) 

%% d是完整的文件名+路徑
%%〜ND不帶擴展
使用/ R參數僅返回的文件名,它會掃描文件夾和子文件

UPDATE 1

根據需要下面的代碼應該工作。
我已經添加了一個忽略子文件夾的IF。

FOR /R E:\your_folder\ %%d IN (*.*) DO (
    IF %%~dpd==E:\your_folder\ (
     ren %%d %%~nd.txt_mvd 
    ) 
) 

UPDATE 2

固定碼

FOR /R E:\your_folder\ %%d IN (*.txt) DO (
    IF %%~dpd==E:\your_folder\ (
     ren %%d %%~nd.txt_mvd 
    ) 
) 

UPDATE 3
這裏是腳本的一個更一般化的和參數化的版本。
將開始參數更改爲您的需要(前4行代碼)。
此腳本首先將您選擇的文件(第一個參數)重命名爲您的起始文件夾(第三個參數),將擴展名更改爲新文件(第二個參數),然後移動您選擇的文件夾中的重命名文件(第四個參數)。

set Extension_of_file_you_want_to_renamne_and_move=txt 
set New_extension_of_moved_files=txt_mvd 

set Folder_that_contain_your_files=C:\Your_starting_folder\ 
set Folder_where_to_move_your_files=C:\Your_destnation_folder\ 

FOR /R %Folder_that_contain_your_files% %%d IN (*.%Extension_of_file_you_want_to_renamne_and_move%) DO (
    IF %%~dpd==%Folder_that_contain_your_files% (
    IF %%~xd==.%Extension_of_file_you_want_to_renamne_and_move% (
     ren "%%~d" "%%~nd.%New_extension_of_moved_files%" 
     move "%%~dpnd.%New_extension_of_moved_files%" "%Folder_where_to_move_your_files%" 
     ) 
    ) 
) 

當您更改參數時請勿添加任何空格。
所以不改變這樣的參數:

set Folder_that_contain_your_files = c:\myFolder  <--- WRONG, WON'T WORK, there are unneeded space 

而是寫參數,而不需要的空間:

set Folder_that_contain_your_files=c:\myFolder  <--- OK, THIS WILL WORK, there are no extra spaces 

UPDATE 4
固定代碼,我加入了一些引號,沒有他們的代碼將不會工作,如果文件夾名稱包含空格。

set Extension_of_file_you_want_to_renamne_and_move=txt 
set New_extension_of_moved_files=txt_mvd 

set Folder_that_contain_your_files=C:\Your_starting_folder\ 
set Folder_where_to_move_your_files=C:\Your_destnation_folder\ 

FOR /R "%Folder_that_contain_your_files%" %%d IN (*.%Extension_of_file_you_want_to_renamne_and_move%) DO (
    IF "%%~dpd"=="%Folder_that_contain_your_files%" (
    IF %%~xd==.%Extension_of_file_you_want_to_renamne_and_move% (
     ren "%%~d" "%%~nd.%New_extension_of_moved_files%" 
     move "%%~dpnd.%New_extension_of_moved_files%" "%Folder_where_to_move_your_files%" 
     ) 
    ) 
) 
+0

謝謝Max,代碼。我能夠重命名這些文件,但它將當前和所有子目錄中的所有帶有ext .txt的文件重命名爲.txt_mvd。我只想更改進入當前目錄的新文件並更改擴展名,然後將其移至存檔目錄。這可以改變嗎?再次感謝。 – Jerry 2012-02-03 23:13:31

+0

對不起,但代碼無法正常工作。它將包括.exe在內的其他文件的擴展名更改爲.txt_mvd。我假設在你的代碼中,E:\ your_folder \是我當前的文件夾?我在哪裏提到移動已更改的擴展文件的文件夾?你能解釋一下邏輯和我需要把我的當前目錄放在哪裏以及我需要放置檔案目錄的位置嗎?謝謝! – Jerry 2012-02-05 02:41:04

+0

@richard:對不起,有一個錯誤。我已經修復了它 – Max 2012-02-05 11:24:03

相關問題