使用嵌套循环中的文件列表将文件复制到文件夹folderlist,批处理 [英] Use nested loop to copy files in filelist to folders in folderlist, with batch

查看:220
本文介绍了使用嵌套循环中的文件列表将文件复制到文件夹folderlist,批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要同时从文件列表及文件夹列表读取每个文件复制到每个文件夹,一个文件重命名。

下面是我原来的code。外循环不会增加。我试图运用我所了解的循环和延迟扩展,但没有奏效各种方法。

有人可以告诉我如何修复code或改用什么?

编辑澄清的问题:每次迭代,回声SRC相呼应第一行Filelist.txt中。永远不会改变。在另一方面,回声DEST确实经过folderlist.txt预期(需要)。

 关闭@echo
SETLOCAL enabledelayedexpansion
:: VAR在调用程序设置
SETNEWNAME =%VAR%_filename_0.jpg
    FOR / F %% G IN(Filelist.txt中)DO(
    FOR / F %%中的H(Folderlist.txt)DO(
        SET SRC = %%摹
        SET DEST = %% ^ h
            回声src的!SRC!
            回声dest为!DEST!
            REM在这里,每次迭代做复制和重命名
            ::复制!SRC! !DEST \\%NEWNAME%1> NUL
  )


解决方案

在您的code的主要问题是没有语法,但概念。您code读取的文件列表中的行,然后,对于这一行,读取文件夹列表中的所有行。

如果您有嵌套循环,为内环外所有迭代的每一次迭代都执行

如果你需要从两个文件读取同步线,你需要像

 关闭@echo
    SETLOCAL ENABLEEXTENSIONS disabledelayedexpansion    <文件(
        FOR / Fdelims =%%一中(文件夹)做(
            集/ PMYFILE =
            SETLOCAL enabledelayedexpansion
            FOR / Fdelims =%%的B(!MYFILE!)做(
                ENDLOCAL
                呼应%%一个%% b
            )
        )
    )

也就是说,在文件夹列表读取使用命令,文件列表,从以一套标准输入读取/ p 。由于我们重定向文件列表作为输入,设置/ P 将会从中获取数据。

I want to simultaneously read from a file list and a folder list and copy each file into each folder, with a file rename.

Below is my original code. The outside loop doesn't increment. I've tried every which way to apply what I've read about loops and delayed expansion but nothing has worked.

Can someone please tell me how to fix the code or what to use instead?

EDIT to clarify problem: With each iteration, "echo src" echoes 1st line in Filelist.txt. Never changes. On the other hand, "echo dest" does go through folderlist.txt as expected (desired).

@echo off
setlocal enabledelayedexpansion
:: var is set in calling routine
SET "newname=%var%_filename_0.jpg"   
    FOR /F  %%G IN (Filelist.txt) DO (
    FOR /F %%H IN (Folderlist.txt) DO (
        SET src=%%G
        SET dest=%%H
            echo src is !src!
            echo dest is !dest!
            REM Here with each iteration do a copy and rename
            :: copy "!src!" "!dest!\%newname%"  1>nul
  )
)

解决方案

The main problem in your code is not the syntax, but the concept. Your code reads a line from list of files and then, for this line, reads all the lines in the folder list.

If you have nested for loops, for each iteration of the outer for all iterations of the inner loop are executed.

If you need a synchronized line read from the two files, you need something like

@echo off
    setlocal enableextensions disabledelayedexpansion

    < files (
        for /f "delims=" %%a in (folders) do (
            set /p "myFile="
            setlocal enabledelayedexpansion
            for /f "delims=" %%b in ("!myFile!") do (
                endlocal
                echo %%a %%b
            )
        )
    )

That is, the folder list is read with the for command, and the file list, is read from the standard input with a set /p. Since we are redirecting the file list as input, the set /p will get its data from it.

这篇关于使用嵌套循环中的文件列表将文件复制到文件夹folderlist,批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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