-1

I'm very new to using batch files and programming in cmd. After failing to find a free wallpaper program that would set a specific wallpaper at a specific time, and supported dual monitors, I decided to create a batch file that pulls the time of day (in military format) and uses if statements to set the appropriate wallpaper if it hasn't already been set. Here's a section of the code so far:

@echo off

set /A mystate=0

:start

For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)

if %mytime% lss 800 (
    if NOT %mystate% == 1 (

        reg add "HKCU\Control Panel\Desktop" /v Wallpaper /f /t REG_SZ /d C:\Users\zach.norstedt\Pictures\Wallpaper\ASunrise.jpg

        :: Change last number to 0 to not tile, set it to 1 to tile.

        reg add "HKCU\Control Panel\Desktop" /v TileWallpaper /t REG_SZ /f /d 1

        :: The following lines energize desktop.

        %SystemRoot%\System32\RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters

        set /A mystate=1
    )
)

sleep 1

goto start

Running this file gives the error "The syntax of the command is incorrect.", so I know at least one of my commands doesn't have the right syntax but I can't figure out which one it is. Most of this code was copied and pasted from different sources across the internet. I have effectively no knowledge of correct cmd syntax.

  • so start debugging: start commenting out lines until you find the one causing the error. – Marc B Jul 15 '16 at 17:08
  • I've been chugging away at that for a while. I receive the same error no matter which line or combination of lines I've commented out so far. – Zachary Norstedt Jul 15 '16 at 17:14
  • then comment out EVERYTHING, and start uncommenting. if you get the error with the totally commented version, then it's how you're CALLING this batch that's causing the error. – Marc B Jul 15 '16 at 17:16
  • I don't think hitting the enter key while having this file selected would be the cause of the error. I came here with a syntax question, so I could learn more about MS-DOS syntax. I already know how to iteratively debug code. – Zachary Norstedt Jul 15 '16 at 17:20
  • if you remove the `@echo off`, it will tell you exactly, where the syntax error occures. (btw. this is *not* MS-DOS code, but `cmd` code) – Stephan Jul 15 '16 at 17:27
  • Thanks, I'll correct my post and give that a try. – Zachary Norstedt Jul 15 '16 at 17:30
  • 1
    *I already know how to iteratively [sp] debug code*. Then do so to isolate the line that's causing the error, and you can ask a specific question about the syntax in that line. Don't expect us to do it for you. – Ken White Jul 15 '16 at 17:44
  • Fair enough. What I expected was my mistake to be visually obvious. I guess I was wrong. – Zachary Norstedt Jul 15 '16 at 21:19

1 Answers1

1

maybe not that obvious: don't use :: inside code blocks. Use REM instead.
(I personally don't use :: at all for consistence reasons)
It's technically a label and labels are not allowed in code blocks.

Stephan
  • 53,940
  • 10
  • 58
  • 91