For loop in Windows batch file: Error: “The syntax of the command is incorrect”

孤街浪徒 提交于 2019-12-25 04:33:08

问题


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

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
)

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.

EDIT: 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
)

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"

Final Edit:

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.


回答1:


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



回答2:


Use Delayed Expansion and the changed syntax for !variables! if you need to use the variables in the loop.

The shift command is doing nothing to the loop variables.



来源:https://stackoverflow.com/questions/20010117/for-loop-in-windows-batch-file-error-the-syntax-of-the-command-is-incorrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!