2010-09-13 42 views
0

更名如何寫一個批處理程序,它可以從一個文件夾(包括子文件夾中的文件)中移動與.txt文件到不同的文件夾,它的形式重新命名folderName_subfolderName_Filename.extension移動和批量

+0

你在Windows或Unix中這樣做? – 2010-09-13 07:32:14

+0

windows xp professional(SP3) – subanki 2010-09-13 07:36:30

回答

1

這下面的片段應該做的伎倆。修改它以滿足您的需求。

@ECHO OFF 
REM Put the source and destination folde names here. 
REM You can use %1 and %2 instead if you want to pass 
REM folders as command line parameters 

SET SOURCE_FOLDER=C:\SRC 
SET TARGET_FOLDER=C:\DST 

REM This is needed for variable modification inside the FOR loop 
SETLOCAL ENABLEDELAYEDEXPANSION 

REM The FOR loop lists all files recursively beginning in 
REM %SOURCE_FOLDER% matching the *.txt pattern. 
REM Filenames can be accessed in th loop via the %%F variable 
FOR /R %SOURCE_FOLDER% %%F IN (*.txt) DO (

    REM Put the path and filename into the FILE_NAME variable 
    SET FILE_NAME=%%~pnxF 

    REM Transform the path to new filename 
    REM (replace '\' with '_' and strip the first '\') 
    SET FILE_NAME=!FILE_NAME:\=_! 
    SET FILE_NAME=!FILE_NAME:~1! 

    REM This is the actual MOVE command creating the 
    REM targest filename from the variables. 
    MOVE "%%F" "%TARGET_FOLDER%\!FILE_NAME!" 
) 
+0

你是天才感謝:)順便說一句,我在他們的工作中有一些疑慮,你可以在代碼旁邊放一個評論,這樣我就可以清楚地理解 – subanki 2010-09-13 08:44:29

0

採用的解決方案:

用法:moveit TargetFolder DestinationFolder NameOfTargetFolder

樣品:moveit C:\MyFolder C:\MySecondFolder MyFolder

moveit.bat:

Set target=%~1 
Set destination=%~2 
Set prefix=%~3 

for /f "tokens=*" %%f in ('dir /b %target%\*.txt') do move "%target%\%%f" "%destination%\%prefix%_%%f" 

for /f "tokens=*" %%s in ('dir /b/ad %target%\*') do call moveit.bat "%target%\%%s" "%destination%" %prefix%_%%s 
+0

非常感謝:) – subanki 2010-09-13 09:08:04

+0

我想命名它根據文件夾層次結構像FolderName_subFolderName1_SubFolderName2_subFolderNAme3 ....._ SubFolderNameN.txt – subanki 2010-09-13 09:09:34

+0

因此,一個文件夾\ Subfolder \ Subfolder2 \ Filename.txt應該是Folder_Subfolder_Subfolder2_Filename.txt? – RoXX 2010-09-13 09:21:13