繁体   English   中英

使用批处理编程在文件路径中重命名具有特定文件夹名称的文件

[英]Renaming files with specific folder name in a file path with batch programming

我有一个文件夹路径:

 C:\form\Regression\Measurments\scene1\a\file.txt C:\form\Regression\Measurments\scene2\a\file.txt

我希望 output 是:

 C:\form\Regression\Measurments\scene1\a\scene1.txt C:\form\Regression\Measurments\scene2\a\scene2.txt

我尝试使用以下代码,无法完成它:

@echo OFF

SET Dir=C:\form\Regression\Measurments

for /D /r %Dir% %%a in (*) do (
    ECHO Processing folder: %%a
    for %%b in (%%a\*.txt) do (
        setlocal EnableDelayedExpansion
        rem create here your new name of file
        set FileName=%%~nxb 
        echo File: %%b - !FileName!
        ren %%b !FileName!
    )
)

PAUSE

仅基于您提供的信息,以下示例应按您的要求进行。

请确保您根据需要修改第2行的基本目录, (特别注意您的拼写,因为正确的拼写是“测量”)

@Echo Off
Set "baseDir=C:\form\Regression\Measurments"
For /D %%G In ("%baseDir%\*") Do For /D %%H In ("%%G\*"
) Do For /F "Skip=6 Tokens=3" %%I In (
    '%__AppDir__%robocopy.exe /L /NFL /NDL /NJH "%%H" Null *.txt 2^> NUL'
) Do If "%%I"=="1" (Echo Processing %%H & Ren "%%H\*.txt" "%%~nxG.txt")
Pause

要了解它是如何工作的,请阅读所使用的每个命令的使用信息。

您已经获得了特定层次结构深度的子目录和文件,因此我建议不要使用for /R :而是使用 for /D to first get the sub-directories scene* , then build the complete path to the file 。 txt` 并重命名它,因为它存在。

这是我的意思的一个例子:

@echo off
rem // Enumerate the desired directory hierarchy depth:
for /D %%I in ("C:\Form\Regression\Measurements\scene*") do (
    rem // Built the full path to the file and rename when found:
    if exist "%%~I\a\file.txt" ren "%%~I\a\file.txt" "%%~nI.*"
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM