search - 批处理文件为: move files into a sub folder if they contain the filename of another file

标签 search batch-file filenames move

这段代码非常适合我将具有相同文件名但不同扩展名的文件 move 到子文件夹中。

所以当前的逻辑是:如果 NC1 文件与 PDF 文件具有相同的文件名,则将该 NC1 move 到其各自的子文件夹。

但是我的文件没有相同的文件名。

以下是 2 个示例文件:

  • f100.nc1”
  • “999-P-f100 - 板 - Rev 0 - 287x200.pdf”

如何更改此代码以遵循以下逻辑:如果 PDF 文件名包含 NC1 文件的文件名,则将该 NC1 move 到其各自的子文件夹。

    for %%j in ("..\2PDF_Outsourced\1PDF_Heavy\*.pdf") do (
        if exist "%%~nj.nc1" (
            move /-y "%%~nj.nc1" "\2NC1_Outsourced\1NC1_Heavy"
        )
    )

    for %%j in ("..\2PDF_Outsourced\1PDF_Light\*.pdf") do (
        if exist "%%~nj.nc1" (
            move /-y "%%~nj.nc1" "\2NC1_Outsourced\1NC1_Light"
        )
    )

预先感谢您的帮助。我在这个阶段已经停留了一段时间,并且正在努力理解分隔符、字符串和通配符。

最佳答案

诀窍是扭转你的逻辑。迭代 .nc1 文件,然后查看是否存在匹配的 .pdf(带有通配符)。为 Heavy 和 Light 添加第二个循环可以避免代码复制。

for %%F in ("*.nc1") do for %%P in (Heavy Light) do (
  if exist "..\2PDF_Outsourced\1PDF_%%P\*%%~nF*.pdf" (
    if exist "%%F" move /-y "%%F" "\2NC1_Outsourced\1NC1_%%P"
  )
)


编辑

我在上面的代码中添加了第二个 IF EXIST,以防名称与 Heavy 和 Light pdf 文件匹配。

如果 Endoro 关于忽略与较大单词的子字符串匹配的名称的担忧是有效的,则可以扩展上述内容:

for %%F in ("*.nc1") do (
  set "name=%%~nF"
  setlocal enableDelayedExpansion
  for %%C in (. [ ^^) do set "name=!name:%%C=\%%C!"
  for %%N in (!name!) do (
    endlocal
    for %%P in (Heavy Light) do for /f "eol=: delims=" %%A in (
      'dir /b /a-d "..\2PDF_Outsourced\1PDF_%%P\*%%~nF*.pdf"^|findsdr /i "\<%%N\>"'
    ) do if exist "%%F" move /-y "%%F" "\2NC1_Outsourced\1NC1_%%P"
  )
)

关于search - 批处理文件为: move files into a sub folder if they contain the filename of another file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18696158/

相关文章:

regex - FOR/F 不适用于批处理文件中的逗号分隔双引号字符串

bash - 如何使用 bash 脚本递归地从文件中删除特定扩展名

javascript - 一个网站的多个搜索框

ios - 搜索功能解析 IOS

windows - 如何在 Rust cargo 构建脚本上调用批处理构建器脚本?

c# - 替换C#中所有目录和文件的名称

python - 在 Python 中构建完整路径文件名

android - 像 Youtube 应用程序一样搜索 View

php - 包含在常量中的 Laravel 查询模型

batch-file - batch - 当前批量处理的文件的目录