将文件夹中的一个文件移动到另一个文件夹并重命名??Cmd?

将文件夹中的一个文件移动到另一个文件夹并重命名??Cmd?

我希望得到一些帮助,我正在考虑使用命令行执行以下操作:

  • 移动文件夹中的文件(包含数百个文件)复制到新文件夹(新文件夹是此文件夹所在的文件夹,因此向上一个目录)

  • 重命名文件(基本上所有文件都有852在文件名中,我想删除852。TXT

非常感谢您的帮助!!

谢谢!!

答案1

下列评论批处理脚本955137.bat可以完成这项工作-如果我理解你的(稍微模糊地表述)问题的话:

@ECHO OFF
SETLOCAL EnableExtensions
:: production
:: store folder (that has hundreds of files) name to a variable
    ::       ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓  change to match your conditions
set "_folder=D:\test\955137\sub955137"
:: Change the current directory/folder and store the previous folder/path
pushd "%_folder%"
:: get (any) one file, store its name to the _fileOld variable 
for %%G in (*.txt) do set "_fileOld=%%G"
::  
   :: To delete everything after the string '852'  
   :: first delete '852' and everything before it
   SET "_fileAux=%_fileOld:*852=%"

   ::Now remove this from the original string
   CALL SET "_fileNew=%%_fileOld:%_fileAux%=%%.TXT"
::
   :: debug: show renaming rule
   echo %_fileOld% - %_fileAux% = %_fileNew%  
:: silently copy file up one directory (renaming the target) 
>NUL copy /B "%_fileOld%" ..\"%_fileNew%"
:: debug: show copied file
tree /F %CD%\.. | findstr /I /V "volume"

:: Change directory back to the path most recently stored by the PUSHD command.
popd

输出显示所选文件的重命名规则(实际上,上述代码总是_fileOld使用循环将变量设置为按字母顺序排列的最后一个文件for %%G …)。

d:\bat> d:\bat\SF\955137.bat
ONExy852ONEab.txt - ONEab.txt = ONExy852.TXT
D:\TEST\955137
│   ONExy852.TXT
└───sub955137
        axy852aab.txt
        filexy852fileab.txt
        folderxy852folderab.txt
        inxy852inab.txt
        Movexy852Moveab.txt
        ONExy852ONEab.txt

请注意,以上输出来自以下临时创建的测试环境:

@ECHO OFF
SETLOCAL EnableExtensions
:: build testing environment - start
::   store folder (that has hundreds of files) name to a variable
set "_folder=D:\test\955137\sub955137"
::   create a folder
2>NUL MD "%_folder%"
pushd "%_folder%"
::   create some files there in %_folder%
for %%G in (Move ONE file in a folder) do >"%_folder%\%%Gxy852%%Gab.txt" echo %%G
::   remove all files from previous testing
2>NUL erase %CD%\..\*852*.txt
::   built testing environment: list files
tree /F %CD%\.. | findstr /I /V "volume"
::   build testing environment - done
popd
ENDLOCAL

输出如下:

d:\bat> d:\bat\SF\955137create.bat
D:\TEST\955137
└───sub955137
        axy852aab.txt
        filexy852fileab.txt
        folderxy852folderab.txt
        inxy852inab.txt
        Movexy852Moveab.txt
        ONExy852ONEab.txt

相关内容