1

Every time I run this .bat file it keeps on giving me a "syntax error command". Any idea on how to solved this?

if !numFiles! gtr 2 (
   gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o %out_path%\A!yearDay!.a1_file.file.tif !fileList!
   set "fileList=!fileList:~0,-1!"
   move !fileList: =,! "%proc_path%"
8
  • 1
    Run it with @echo off and it should hopefully tell you the exact line to look at. Commented Nov 28, 2014 at 2:54
  • Did that already and the "moving of files" part of the script is causing the error. How will I fix that part? Commented Nov 28, 2014 at 3:00
  • What system do you use? I've never seen statement like 'set "fileList=!fileList:~0,-1!"'. I think 'set "..."' statements make errors. Commented Nov 28, 2014 at 3:01
  • I'm using windows 7. How will I modify that part? Commented Nov 28, 2014 at 3:03
  • @Fumu, that's just stripping the last character off the string, it's valid from what I remember. Commented Nov 28, 2014 at 3:09

1 Answer 1

3

Okay, since you have narrowed your problem down to the following block:

if !numFiles! gtr 2 (
   gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o %out_path%\A!yearDay!.a1_file.file.tif !fileList!
   set "fileList=!fileList:~0,-1!"
   move !fileList: =,! "E:\Proc\Proc_Pro"
)

and cmd.exe doesn't identify the specific line in that block, you need to change it (temporarily) to isolate it to a specific line. Change the code section to:

if not !numFiles! GTR 2 goto xyzzy
  echo AAA xx!fileList!xx
  gdal_merge.py -n 0 -a_nodata -32767 -of GTiff -o %out_path%\A!yearDay!.a1_file.file.tif !fileList!
  echo BBB xx!fileList!xx
  set "fileList=!fileList:~0,-1!"
  echo CCC xx!fileList!xx
  echo DDD xx!fileList: =,!xx
  move !fileList: =,! "E:\Proc\Proc_Pro"
:xyzzy

Then run it with echo on again. Structuring this way will allow cmd to output individual lines before execution, rather than the entire if block.

This, and the added echo statements, should hopefully be enough to track it down.


Having performed those debug steps on some relevant test data, it boils down to basically this:

C:\USERS\pax\Documents>echo >qqq1
C:\USERS\pax\Documents>echo >qqq2
C:\USERS\pax\Documents>echo >qqq3
C:\USERS\pax\Documents>mkdir qqq
C:\USERS\pax\Documents>move qqq1,qqq2,qqq3 qqq
The syntax of the command is incorrect.

In other words, move does not permit you to move multiple comma-separated files to a destination directory, despite the fact it appears to be possible according to the output of move /?.

This is a known issue, which you can read about here.

You will need to find another way. Provided you can guarantee there are no spaces in the file names themselves (if there were, your original solution would not have worked anyway), you can use something like:

@setlocal enableextensions enabledelayedexpansion
@echo off

rem Clean up first.
del /s qqq1.txt qqq2.txt qqq3.txt >nul: 2>&1
rmdir /s /q qqq >nul: 2>&1

rem Make the files and directories.
echo >qqq1.txt
echo >qqq2.txt
echo >qqq3.txt
mkdir qqq

rem Set up space-separated list.
set filelist=qqq1.txt qqq2.txt qqq3.txt

rem Move the files, this is the important bit.
for %%f in (!filelist!) do move %%f qqq >nul: 2>&1

rem Check it worked.
dir qqq

@endlocal

The bulk of that is test harness, the important bit is the line:

for %%f in (!filelist!) do move %%f qqq >nul: 2>&1
Sign up to request clarification or add additional context in comments.

4 Comments

Tried to remove the , from the move !fileList: =,! "%proc_path%" and the syntax error command doesn't show up anymore but it shows a The system cannot find the file specified. error instead.
@user, that's because your command will now be move file1.txtfile2.txtfile3.txt somewhere - all you're doing is removing the spaces. You need to process the files one by one, I'll update the answer.
I've indicated the whole script I am using.
@user: see the last part of my answer, the bit beginning "Having performed those debug steps" - it states clearly what your problem is, and how to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.