如何编写批处理程序,该程序可以将.txt文件从文件夹(包括子文件夹中的文件)移动到另一个文件夹,并以表格folderName_subfolderName_Filename.extension
重命名答案 0 :(得分: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!"
)
答案 1 :(得分: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