如何编写一个批处理程序,可以将带有.txt的文件从一个文件夹(包含子文件夹中的文件)移动到另一个文件夹中,并以folderName_subfolderName_Filename.extension的形式重命名
发布于 2010-09-13 16:26:39
下面这段代码应该可以做到这一点。根据需要对其进行修改。
@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!"
)发布于 2010-09-13 17:05:57
采用的解决方案:
用法: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%_%%shttps://stackoverflow.com/questions/3698511
复制相似问题