.cmd file gives error was unexpected at this time.

Lukas Valancauskas 5 Reputation points
2023-04-12T05:45:01.44+00:00

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
if NOT EXIST OLD (
	md old
	move *.xml old\
)
cd old
echo %~dp0
pause

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 ..
echo.
pause


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,462 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Lukas Valancauskas 5 Reputation points
    2023-04-12T15:44:38.7566667+00:00

    Here is a picture: Capture


  2. MotoX80 32,911 Reputation points
    2023-04-12T16:32:32.9166667+00:00

    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,906 Reputation points
    2023-04-12T22:40:53.0233333+00:00

    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!' "