2012-02-03 110 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

相关问题