2010-09-13 13 views
0

Comment écrire un programme batch qui peut déplacer des fichiers .TXT d'un dossier (y compris les fichiers dans le sous-dossier) dans un autre dossier et renommez sous la forme folderName_subfolderName_Filename.extensionDéplacer et renommer en batch

+0

Êtes-vous faire cela dans Windows ou Unix? –

+0

windows xp professional (SP3) – subanki

Répondre

1

Cet extrait suivant devrait faire l'affaire. Modifiez-le selon vos besoins.

@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

Vous êtes Merci :), en passant, j'ai quelques doutes dans leur travail pouvez-vous mettre un commentaire à côté du code afin que je puisse comprendre clairement – subanki

0

solution adoptée:

utilisation: moveit TargetFolder DestinationFolder NameOfTargetFolder

Exemple: 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

Merci beaucoup :) – subanki

+0

Je veux le nommer en fonction de la hiérarchie des dossiers, par exemple FolderName_subFolderName1_SubFolderName2_subFolderNAme3 ....._ SubFolderNameN.txt – subanki

+0

Donc, un dossier de fichiers \ sous-dossier \ Subfolder2 \ Filename.txt serait Folder_Subfolder_Subfolder2_Filename.txt? – RoXX

Questions connexes