For循环在Windows批处理文件:错误:"该命令的语法不正确" [英] For loop in Windows batch file: Error: "The syntax of the command is incorrect"

查看:1552
本文介绍了For循环在Windows批处理文件:错误:"该命令的语法不正确"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续行动,我的问题在这个线程问道:
<一href=\"http://stackoverflow.com/questions/20004597/extracting-string-after-last-instance-of-delimiter-in-a-batch-file\">Extracting在一个批处理文件分隔符的最后一个实例后的字符串

this is a follow-up to my question asked in this thread: Extracting string after last instance of delimiter in a Batch file

我正在写一个批处理文件,它接受一个未知的数字字符串从Java程序(它调用的.bat)作为参数表示的目录。这些目录保存在一个阵列称为迪尔斯。

I am working on writing a batch file that takes an unknown number of strings denoting directories from a java program (which calls the .bat) as arguments. These directories are saved in an "array" called "dirs".

for /F "tokens=2 delims==" %%z in ('set dirs[') do (
    set "parm=%~1"
    for %%a in (%parm:\= %) do set folder=%%a
    echo %folder%
    shift
)

在显示目录数组将举行到实际目录不以任何方式改变它,最终我将使用它作为XCOPY目录从被复制。帕姆需要投入批处理文件的实际参数,并操纵它来解压目录的最后一部分。称此目录名,并打印到命令行文件夹专卖店的变量。然而,当我运行程序我不断地得到一个错误说该命令的语法不正确。我试图缩小到底在哪问题是,它绝对是上面的循环中。为了使外环工作,并在%% Z具有要以某种方式操作的?当我用一个简单的回声%% Z替换其内容的循环工作,所以这让我怀疑这就是问题所在。

The "dirs" array will hold on to the actual directory without changing it in any way, and eventually I will be using it with xcopy as the directory to be copied from. Parm takes the actual parameters put into the batch file and manipulates it to extract the last part of the directory. A variable called "folder" stores this directory name and prints it to the command line. However, when I run the program I constantly get an error saying "The syntax of the command is incorrect". I tried narrowing down where exactly the problem was, and it's definitely within the above loop. In order for the outer loop to work, does the %%z have to be operated on in some way? The loop works when I replace its contents with a simple "echo %%z", so it made me wonder if that's where the issue lies.

编辑:这里是更新循环,与包括XCOPY行:

Here is the updated loop, with the xcopy line included:

for /F "tokens=2 delims==" %%z in ('set dirs[') do (
set "param=%~1"
set param=!param:\= !
for %%a in (!param!) do set lastDir=%%a
    echo !lastDir!
XCOPY %%z\*.* !outdrive!:\!dir!\!lastDir! /C /S /D /Y /I
shift
)

在你需要知道,outdrive的情况是外部硬盘驱动器,dir是先前在程序中创建的备份目录,lastDir应该是不言自明的。它的文件夹名称,即照片或文档

In case you need to know, "outdrive" is an external hard drive, "dir" is the backup directory created earlier in the program, and lastDir should be self-explanatory. It's the folder name, i.e. "pictures" or "documents"

最后编辑:

感谢大家谁回答。我踢我自己之前没有看到的答案,但我想通了一些code按预期工作。这里是循环,因为它目前是,正常操作:

Thank you all who replied. I am kicking myself for not seeing the answer before, but I have figured out some code that works as expected. Here is the loop as it currently sits, operating properly:

for /F "tokens=2 delims==" %%z in ('set dirs[') do (
    set param=%%z
    set param=!param:\= !
    for %%a in (!param!) do set lastDir=%%a
    XCOPY %%z\*.* !outdrive!:\!dir!\!lastDir! /C /S /D /Y /I
)

而不是使用批处理文件中传递的参数,我直接从迪尔斯数组工作。我再设置一个不同的变量到指定的目录字符串,并在该变量操作,而不是存储在数组中的字符串。

Instead of using the parameters that were passed in to the batch file, I am working directly from the "dirs" array. I am then setting a different variable to the specified directory string, and operating upon that variable, rather than the string stored in the array.

推荐答案

基于更新的信息,这应该显示你所需要的命令,并在此code片段不需要延迟扩展。

Based upon the updated information, this should display the commands you need, and delayed expansion isn't needed in this code snippet.

@echo off
set outdrive=E
set dir=backup

set dirs[1]=c:\folders1
set dirs[2]=c:\folders2
set dirs[3]=c:\folders3
set dirs[4]=c:\folders4

for /F "tokens=2 delims==" %%z in ('set dirs[') do (
echo XCOPY "%%z\*.*" "%outdrive%:\%dir%\%%~nxz\" /C /S /D /Y /I
)
pause

这篇关于For循环在Windows批处理文件:错误:&QUOT;该命令的语法不正确&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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