Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows批处理文件传递变量(.bat)时出错_Windows_Batch File - Fatal编程技术网

Windows批处理文件传递变量(.bat)时出错

Windows批处理文件传递变量(.bat)时出错,windows,batch-file,Windows,Batch File,我试图递归地遍历文件夹中的所有文件。 我需要读取文件名,其中一个是文件名模式。我需要创建一个名为somethingone的文件夹(如果该文件夹不存在),并将文件移动到该文件夹中。我已经开始在.bat中编写以下代码 FOR /R D:\MOE\MRs\batchfiles\01\ %%F in (*.*) do ( echo %%~nF set newloc=%%~nF echo %newloc% ) 第1行正确打印文件夹中的文件名。但当我将其读取到新变量newloc时,第4行总是只打印最后

我试图递归地遍历文件夹中的所有文件。 我需要读取文件名,其中一个是文件名模式。我需要创建一个名为somethingone的文件夹(如果该文件夹不存在),并将文件移动到该文件夹中。我已经开始在.bat中编写以下代码

FOR /R D:\MOE\MRs\batchfiles\01\ %%F in (*.*) do ( 
echo %%~nF
set newloc=%%~nF
echo %newloc%
) 

第1行正确打印文件夹中的文件名。但当我将其读取到新变量newloc时,第4行总是只打印最后一个文件的名称。任何人都能意识到这里出了什么问题,或者给我一个方法来解决这个问题。

编辑:完全改变了我的答案,并试图完全解决你的整体问题,而不仅仅是你一直坚持的问题

您必须将target_dir更新到您希望文件到达的任何位置

@echo off
setlocal enabledelayedexpansion

set source_dir=D:\MOE\MRs\batchfiles\01\
set target_dir=D:\something\

:: Recursively search the source directory for files
for %%A in (somethingone somethingtwo somethingthree) do (
    for /F "delims=" %%B in ('dir /a:-d /s /b %source_dir%*%%A*') do (
        REM if the directory does not exist, make it
        if not exist %target_dir%%%A mkdir %target_dir%%%A

        move %%B %target_dir%%%A
    )
)

您需要启用延迟扩展,以便
for
循环中的变量值能够正确传播

setlocal enabledelayedexpansion
for /R D:\MOE\MRs\batchfiles\01\ %%F in (*.*) do (
    echo %%~nF
    set newloc=%%~nF
    echo !newloc!
) 

黑暗的东西,谢谢你的回答…,但现在第4行只打印!纽洛克!为了我。。不知道出了什么问题。如果你能帮忙的话,我还需要知道如何用分隔符“u”(下划线)分割文件名字符串,正如我在上面的问题中提到的。。谢谢你…@nadh-在我的脑海里,我会使用并设置
“delims=.
。你想让所有
somethingone
文件都放在同一个文件夹中,还是每个目录级别都有一个文件夹?我需要为每个不同的“somethingone”创建一个单独的文件夹。例如,可以有像abcd_somethingone_pqrs.csv、abab_somethingone_pqpq.csv、abcb_something two_pqrr.csv这样的文件。所以我需要将所有somethingone文件分类到一个名为somethingone的文件夹中。所有的somethingtwo名称包含文件到一个名为somethingtwo的文件夹中…要么我读错了,要么你的前两句话互相矛盾。我知道需要有不同的
something\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu?