递归地将文件夹名称附加到其中的文件并将所有文件移动到基本文件夹 Windows 批处理文件

穆斯塔法·耶尔迪兹

我想递归地将文件夹(和父文件夹)名称附加到该文件夹​​包含的每个 *.txt 文件中。之后我想将所有文件移动到基​​本文件夹并删除所有文件夹。我需要在 Windows BATCH 脚本中实现这一点。例如:

\BaseFolder\A01\B01\EX1.TXT
\BaseFolder\C01\EX2.TXT
\BaseFolder\EX3.TXT

到:

\BaseFolder\A01-B01-EX1.TXT
\BaseFolder\C01-EX2.TXT
\BaseFolder\EX3.TXT

为了做到这一点,我找到了这个解决方案,感谢 JosefZ:

递归地将文件夹名称附加到 Windows 批处理文件中的文件

@echo OFF
SETLOCAL EnableExtensions
for /F "delims=" %%G in ('dir /B /S "C:\Source\*.txt"') do (
    for %%g in ("%%~dpG.") do rename "%%~fG" "%%~nxg_%%~nxG"
)
pause

其中 FOR 循环是:

  • 外部 %%G 循环创建 .txt 文件的静态列表(递归),并且
  • 内部 %%g 循环获取每个特定文件的父文件夹。

但这只是解决了我目标的一部分。任何人都可以帮忙吗?

aschipfl

以下脚本应该完成您想要的操作,即按照预定义移动和重命名文件并删除剩余的空子目录(请参阅所有说明rem):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ROOT=.\BaseFolder" & rem // (target base directory)
set "_MASK=*.txt"        & rem // (file search pattern)
set "_CHAR=-"            & rem // (separator character)

rem // Switch to target directory:
pushd "%_ROOT%" && (
    rem // Loop through list of relative paths of matching files:
    for /F "tokens=1* delims=:" %%E in ('
        xcopy /L /S /I "%_MASK%" "%TEMP%" ^| find ":"
    ') do (
        rem // Store current relative file path, initialise variables:
        set "FILE=%%F" & set "NAME=" & set /A "NUM=0"
        rem // Toggle delayed expansion to avoid trouble with `!` and `^`:
        setlocal EnableDelayedExpansion
        rem // Loop through all individual elements of file relative path:
        for %%I in ("!FILE:\=" "!") do (
            endlocal
            rem // Store current path element and count them:
            set "ITEM=%%~I" & set /A "NUM+=1"
            setlocal EnableDelayedExpansion
            rem // Build new file name and pass it over `endlocal` barrier:
            for /F "delims=" %%N in ("!NAME!%_CHAR%!ITEM!") do (
                endlocal
                set "NAME=%%N"
                setlocal EnableDelayedExpansion
            )
        )
        rem // Finalise new file name:
        if defined _CHAR set "NAME=!NAME:*%_CHAR%=!"
        rem // Actually move and rename the current file:
        > nul move "!FILE!" "!NAME!"
        rem // Switch to parent directory of current file:
        pushd "!FILE!\.." && (
            rem // Loop through parent directory elements:
            for /L %%N in (2,1,!NUM!) do (
                rem // Try to remove parent directory when empty, go one up:
                set "DD=!CD!" & cd ".." & 2> nul rd "!DD!"
            )
            rem // Return to previous working directory:
            popd
        )
        endlocal
     )
     rem // return to original working directory:
     popd
)

endlocal
exit /B

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Windows批处理文件的隐藏功能

Windows批处理文件:.bat与.cmd?

批处理文件中获取Windows Download文件夹的路径(Download shell文件夹)

从GNUMakeFile调用Windows批处理文件

Windows文件中批处理文件中具有国际字符/突变/元音(ä。ö。ü)的文件夹中的FTP文件

Windows curl批处理文件

Windows批处理文件,通过子文件夹循环

从Windows批处理文件调用SUBST

Tesseract-OCR可以在Windows cmd的所有子文件夹中批处理文件?

Windows-如何将子文件夹中的所有文件移动到父文件夹?

Windows批处理文件处理-循环

Windows批处理脚本,以某种平衡的方式将XML文件移动到多个文件夹

在Windows批处理文件中检索文件夹的绝对路径

Windows批处理文件:转换子文件夹中的所有文件

使用Windows控制台或批处理文件更改许多文件夹的名称

多行Windows批处理文件问题

从Windows 7中的任何文件夹运行批处理文件

Windows批处理-匹配文件夹名称中带有点(。)的文件夹

Windows批处理文件Modulo划分

Windows cmd批处理文件,复制源文件夹和内容

Windows批处理文件执行错误

Windows批处理文件-按修改日期和/或创建日期将文件排序到文件夹中

Windows批处理文件对子文件夹进行排序

Windows批处理文件,用于根据文件名将文件移动到子文件夹

从Windows的批处理文件中删除特定文件夹(如果为空)?

Windows 批处理文件:合并当前文件夹中的所有 .docx 文件?

Windows批处理文件将一个文件夹中的文件列出到csv/txt文件中

使用 Windows 批处理脚本将多个文件从子文件夹移动到单个文件夹

.txt 与 Windows 激活(批处理文件)