.cmd file gives error was unexpected at this time.

Lukas Valancauskas 5 Reputation points

Hello, may I know why this .cmd file displays this error while trying to run this file? Here is the code:

@echo off

rem This is only a primitive and incomplete check for the location of the script - be careful !
IF /I EXIST "..\..\..\..\..\RailWorks.exe" (
		echo "RW found. - All OK." 
	) ELSE ( 
	echo Execute this script only in Content\Routes\--Routefolder--\Scenery\ !
	echo You have started this script in %~dp0
echo %~dp0
	md old
	move *.xml old\
cd old
echo %~dp0

rem We create this list-file only once - so you can edit list-xml.txt later and run the script with another set of files again.
if NOT EXIST list-xml.txt (
	dir /B *.xml > list-xml.txt

Setlocal EnableDelayedExpansion 

for /f "eol=# delims==" %%i IN ( list-xml.txt ) DO (
	set MyFile=%%i
		IF NOT EXIST "!MyFile!" (
			echo "Not found:       !MyFile!" >> error.log
		) ELSE (
		powershell -Command "(gc "!MyFile!") -replace '\>HiLineAssets\\', '>' | Out-File -encoding ASCII "..\!MyFile!" "
		echo !MyFile! processed.

if EXIST error.log (
	echo There was an error - check error.log please.

cd ..

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,407 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Lukas Valancauskas 5 Reputation points

    Here is a picture: Capture

  2. MotoX80 32,246 Reputation points

    Check the encoding. Edit the cmd file with Notepad and verify that it shows "Windows (CRLF) UTF-8" in the lower right corner of the window. If it's something else use the file saveas dialog to set the encoding. Then from the command prompt, use the TYPE command to display the file contents. Does it look ok?

    0 comments No comments

  3. Rich Matheisen 45,111 Reputation points

    The quoting on this line in your .bat file isn't right:

    powershell -Command "(gc "!MyFile!") -replace '\>HiLineAssets\\', '>' | Out-File -encoding ASCII "..\!MyFile!" "

    Try replacing it with this:

    powershell -Command "(gc '!MyFile!') -replace '\>HiLineAssets\\', '>' | Out-File -encoding ASCII '..\!MyFile!' "