DOS批处理文件 - 将基于文件名的文件复制到文件夹中

时间:2013-06-06 09:43:24

标签: batch-file dos filenames special-folders

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

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

默认文件夹:

999-888123-03
666-777456-01

2 个答案:

答案 0 :(得分: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"
   )
)

答案 1 :(得分:-1)

我不记得除了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