【问题标题】:DOS Batch file - Copy file based on filename into folderDOS批处理文件 - 根据文件名将文件复制到文件夹中
【发布时间】:2013-06-06 09:43:24
【问题描述】:

我想使用批处理文件将它们放入默认文件夹,但帐户名称在文件夹中间。有可以在 dos 命令提示符下使用的脚本吗?

888123AA.pdf  
888123BB.pdf  
888123CC.pdf  
777456AA.pdf  
777456BB.pdf  
777456CC.pdf 

默认文件夹:

999-888123-03
666-777456-01

【问题讨论】:

    标签: batch-file dos filenames special-folders


    【解决方案1】:
    @echo off
    setlocal EnableDelayedExpansion
    
    rem Process all .pdf files
    for %%a in (*.pdf) do (
       rem Get just the file name, ie: "888123AA"
       set fileName=%%~Na
       rem Using the file name minus two last chars, ie: "888123"
       rem get the default folder with that name
       for /D %%b in (*-!fileName:~0,-2!-*) do (
          rem And copy the file to that folder
          copy "%%a" "%%b"
       )
    )
    

    【讨论】:

    • 以下任何想法和建议:在批处理参数替换中路径运算符的以下用法无效:%~Na 对于有效格式,请键入 CALL /?或为/?该命令的语法不正确。
    【解决方案2】:

    除了 UNIX shell 之外,我不记得有任何明显的方法可以做到这一点...也许获取 MSYS 并使用那个(过时的)bash 来提供帮助?

    这是一个 bash 脚本,可以在您从 MSYS 安装 bash 后使用(或者您可以使用 Linux 机器对其进行排序 - Ubuntu 不大于 800MB,并且可以作为 LiveCD 运行而不会干扰您当前的 Windows 系统,并且 LiveCD 可以在需要时兼作系统保护程序。:-)

    #!/bin/bash
    for each in ./*; do
        if [ -d $each ]; then # Only folders are minded.
            # Extract the second part of the folder name.
            ACCOUNT_NAME=`echo $each | sed "s/\\-/\n/" | head -n 2 | tail -n 1`
            cp -v ./$ACCOUNT_NAME*.pdf $each
        fi
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多