批处理脚本的循环中修改变量 [英] Modify variable within loop of batch script

查看:566
本文介绍了批处理脚本的循环中修改变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是根据自己的名字,以preSET文件夹移动文件。我不想做出新的文件夹。因此,如果对应的文件夹中已有文件应该仅被移动。

I am moving files based on their names to preset folders. I don't want to make new folders. So files should only be moved if the corresponding folder is existing already.

文件名都遵循相同的模式: 1234_123456_AA _ *********** 商务部 / PDF

The file names all follow the same pattern: 1234_123456_AA_***********.(doc/pdf)

我有低于下面的脚本作品:

I have the following script below which works:

@echo on
for /r %%f in (*.*) do (
    echo processing "%%f"
    for /f "tokens=1-3 delims=_" %%a in ("%%~nxf") do (
        move "%%f" C:\Users\xxxxxxxxx\Desktop\MOVEFILES\%%a_%%b_%%c\
    )
)
pause

但我遇到的问题是,一些文件名的循环变量有一个'0'占位符 %% b ,例如 1234_0123456_AA 。但是,这个文件名应该是像PTED 1234_123456_AA 间$ P $,我想这个文件移动到相应的文件夹。

But the issue I am running into is that some of the files names have a '0' place holder in loop variable %%b, for example 1234_0123456_AA. But this file name should be interpreted like 1234_123456_AA and I want this file moved into the appropriate folder.

我写了这个:

@echo on
SETLOCAL ENABLEDELAYEDEXPANSION
for /r %%f in (*.*) do (
    for /f "tokens=1-3 delims=_" %%a in ("%%~nxf") do (
        set z=%%b%
        echo !z:~-6!
        move "%%f" C:\Users\xxxxxxxxx\Desktop\MOVEFILES\%%a_%%z_%%c\
    )
)
pause

我得到的回声,以删除'0'的地方持有人,但需要得到回 %% b 中的文件路径的该文件应该被移到

I get the echo to remove the '0' place holder, but need to get that back into %%b in the file path of where the file should be moved to.

怎么在code修改,以获得所需的行为吗?

What to modify in code to get the desired behavior?

推荐答案

使用以下code与 JosefZ <更正一>应用和一些额外的改进/:

Use the following code with the corrections of JosefZ applied and some additional improvements:

@echo off
setlocal EnableDelayedExpansion
for /R %%F in (*_*_*.*) do (
    for /F "tokens=1-3 delims=_" %%A in ("%%~nxF") do (
        set "MiddlePart=%%B"
        set "MiddlePart=!MiddlePart:~-6!"
        if exist "%USERPROFILE%\Desktop\MOVEFILES\%%A_!MiddlePart!_%%C\*" (
            move /Y "%%F" "%USERPROFILE%\Desktop\MOVEFILES\%%A_!MiddlePart!_%%C"
        )
    )
)
endlocal
pause

在code的改进进行了详细的解释如下。

The improvements on code are explained below in details.

有关理解使用的命令以及它们如何工作,打开命令提示符窗口中,执行有下面的命令,并阅读完全针对每个命令显示的所有帮助页面非常谨慎。

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.


  • 回声/?

  • ENDLOCAL /?

  • 如果/?

  • 移动/?

  • 设置/?

  • SETLOCAL /?
    搜索结果

第一个改进是在第一个使用的模式,因为只有文件应在文件名至少2个下划线移动。这种模式仍然不是最好的,但这个任务已经足够了。

The first improvement is the pattern used in first FOR as only files should be moved with at least 2 underscores in file name. This pattern is still not the best one, but good enough for this task.

有最好使用循环变量在上壳体,以避免与改性剂的问题。

It is better to use loop variables in upper case to avoid problems with the modifiers.

例如用作循环变量 %%˚F,并使用循环 %%〜F 里面使用字符串(不能是文件或文件夹名称)循环变量˚F不包围引号,命令处理器退出批处理与错误消息,因为它预计多了一个字母,环路变量 %%〜F 是PTED作为循环变量的文件/文件夹的全名间$ p $?

For example using as loop variable %%f and using inside the loop %%~f to use the string (must not be a file or folder name) of loop variable f without surrounding quotes, command processor exits batch processing with an error message because it expects one more letter, the loop variable as %%~f is interpreted as full name of file/folder of loop variable ?.

循环变量和调节剂是区分大小写的。因此, %%〜F 是间$ P $由命令处理器PTED作为循环变量的字符串˚F周围没有引号和 %%〜fF的与循环变量˚F的文件/文件夹的完整路径和扩展名的文件/文件夹名称。

The loop variables and the modifiers are case sensitive. Therefore %%~F is interpreted by command processor as string of loop variable F without surrounding quotes and %%~fF as file/folder name with full path and extension of the file/folder of loop variable F.

其他一些字符,如,也可以作为循环变量。

Some other characters like # can be also used as loop variable.

在一个环境变量分配一个字符串,它始终是很好用

On assigning a string to an environment variable, it is always good to use

set "variable=text or other variable"

使用引号,如下所示有一个不可见的空间/最后的双引号后的标签是由命令处理器忽略的优势。

Using the quotes as shown here has the advantage that not visible spaces/tabs after last double quote are ignored by command processor.

但是,只要使用

set variable=text or other variable

一切先等号达线路终端被分配给变量包括末尾错在这条线在批处理文件添加或者空格和制表符后。这是几乎从未良好且可以通过使用引号权利很容易地避免一个批处理执行问题的一个共同的源

everything after first equal sign up to line termination is assigned to the variable including trailing spaces and tabs added perhaps by mistake on this line in the batch file. This is nearly never good and a common source of a batch execution problem which can be easily avoided by using quotes right.

使用引号,如下所示也并不好,因为在这种情况下,两个双引号赋值给变量(加上尾随空格/制表符)文本的一部分。这有时是有用的,但最常见的是编码错误。

Using the quotes as show below is also not good as in this case both double quotes are part of the text assigned to the variable (plus trailing spaces/tabs). This is sometimes useful, but is most often a coding mistake.

set variable="text or other variable"

4。延迟扩展

引用的变量集或定义的块( ... 要求推迟扩张,如果内修改当前的变量值,应使用并没有分配给该块上面的变量的值。因此,使用 %%ž是错误的原来的code作为变量以Z 上面的第一未定义FOR 循环,因此与没有在循环的执行总是更换。

4. Delayed expansion

Referencing a variable set or modified within a block defined with (...) requires delayed expansion if the current variable value should be used and not the value assigned to the variable above the block. Therefore using %%z was wrong in original code as variable z was not defined above first FOR loop and therefore was replaced always with nothing on execution of the loops.

在运行命令提示符窗口设置结果显示得到所有的predefined环境变量为当前用户帐号。有变量 USERNAME ,但也 USERPROFILE 包含与桌面文件夹中的user's配置文件目录路径其他的用户帐户相关的文件夹。使用环境变量的 USERPROFILE 使批处理文件较少依赖的Windows Windows版本和语言。

Running in a command prompt window set results in getting displayed all predefined environment variables for the current user account. There is the variable USERNAME, but also USERPROFILE containing path to the user´s profile directory with the Desktop folder and other user account related folders. Using environment variable USERPROFILE makes the batch file less dependent on Windows version and language of Windows.

注意:

第一个作为运行,因为 / R 在当前目录及其所有子目录递归的。随着内的循环将所有发现,在当前目录树中的文件以的子目录%USERPROFILE%\\桌面\\ MOVEFILES ,当前目录应该是永远不会此路径的任何目录。

The first FOR runs because of /R recursive on current directory and all its subdirectories. As the inner FOR loop moves all found files in current directory tree to subdirectories of %USERPROFILE%\Desktop\MOVEFILES, the current directory should be never any directory of this path.

这篇关于批处理脚本的循环中修改变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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