首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >批量移动和重命名

批量移动和重命名
EN

Stack Overflow用户
提问于 2010-09-13 15:28:24
回答 2查看 292关注 0票数 0

如何编写一个批处理程序,可以将带有.txt的文件从一个文件夹(包含子文件夹中的文件)移动到另一个文件夹中,并以folderName_subfolderName_Filename.extension的形式重命名

EN

回答 2

Stack Overflow用户

发布于 2010-09-13 16:26:39

下面这段代码应该可以做到这一点。根据需要对其进行修改。

代码语言:javascript
复制
@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
EN

Stack Overflow用户

发布于 2010-09-13 17:05:57

采用的解决方案:

用法:moveit TargetFolder DestinationFolder NameOfTargetFolder

示例:moveit C:\MyFolder C:\MySecondFolder MyFolder

moveit.bat:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3698511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档