批处理文件以将文件从文件夹内的特定子文件夹复制到目标文件夹 [英] Batch file to copy file from specific sub folder inside a folder to destination folder

查看:186
本文介绍了批处理文件以将文件从文件夹内的特定子文件夹复制到目标文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像他一样的文件夹结构

I have a folder structure like his

folder1-------|
              |123456------123.txt
                          abc.txt
              |234567------fgt.txt
                           234.txt
              |abc---------ytr.txt
                           1234.txt

如果子文件夹的长度为6(仅数字),则需要从主文件夹子目录复制文件.因此,将复制123456,234567中的.txt文件,而不会复制abc.

I need to copy files from Main folders sub directories if the sub folder has a length of 6(only numbers) . So .txt files from 123456,234567 will be copied and abc will not be copied.

我尝试使用通配符,但尚未成功.预先感谢.

I have tried using wildcards but not succeed yet. Thanks in advance.

>xcopy /s "C:\Users\xxx.xxx\Desktop\folder1\*\*.txt" "C:\Users\xxx.xxx\Documents\New folder" /l /D:09-09-2019

推荐答案

好的,所以这是一个示例:

Ok, so here is an example:

@echo off
setlocal enabledelayedexpansion

for /f %%i in ('dir "C:\Users\xxx.xxx\Desktop\folder1\" /b /ad') do (
  set "str=#%%i"
  set "len=0"
  for %%a in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if "!str:~%%a,1!" neq "" (
       set /a "len+=%%a"
       set "str=!str:~%%a!"
       if !len! equ 6 if 1%%i equ +1%%i echo %%i is !len! characters   
     )
   )
)

这将对您提供的路径内的目录执行dir.然后它将测试字符串的长度,如果长度为6个字符,它将测试字符串是否为数字,如果为true,它将echo字符串(文件夹名称)及其长度.然后,应按原样进行测试,如果它提供了所需的输出,则可以用复制字符串替换echo部分.

This will do a dir of the directories inside the path you give it. It will then test the length of the string, if the length is 6 characters, it will then test if the string is numeric, if true, it will echo the string (folder name) and the length of it. You should then test it as is and if it gives you the required output, you can replace echo portion with your copy string.

所以您的最终解决方案将是:

So your final solution will be something like:

@echo off
setlocal enabledelayedexpansion
set /p "mydate=Enter Date (format dd-mm-yyyy): "
for /f %%i in ('dir "C:\Users\xxx.xxx\Desktop\folder1\" /b /ad') do (
  set "str=#%%i"
  set "len=0"
  for %%a in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if "!str:~%%a,1!" neq "" (
       set /a "len+=%%a"
       set "str=!str:~%%a!"
       if !len! equ 6 if 1%%i equ +1%%i xcopy "C:\Users\xxx.xxx\Desktop\folder1\%%i\*.txt" "C:\Users\xxx.xxx\Documents\New folder" /D:!mydate! 
     )
   )
)

这篇关于批处理文件以将文件从文件夹内的特定子文件夹复制到目标文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆