0

I'm programatically creating a batch file. Limitations in the language I'm using (old delphi) mean I can't write to separate lines to the .bat. So my batch has to be a single line.

I have read this and was able to condense a few of the lines. But using the double ampersands && doesn't work for some of the lines and I haven't had success with the ping pause either.

Here's where I am at...

@echo off & setlocal enableextensions&&set myfolder_=Z:\MyFolder
for /f "delims=" %%f in ('dir /o:d /a:-d /b "%myfolder_%\*.mp3"') do (set latest_=%myfolder_%\%%~nxf)
if defined latest_ (echo rem latest file "%latest_%"&xcopy "%latest_%" "Z:\MyFolder\PlayMe" /V /D /Y) else (echo No designated files found in "%myfolder_%")   
Pause

Is it possible to further reduce the lines of code to a single line so that the batch still runs?

Community
  • 1
  • 1
square_eyes
  • 1,269
  • 3
  • 22
  • 52
  • at first, you should use one ampersand, not two. Two ampersands work like `if %errorlevel%==0` – Endoro Sep 08 '13 at 15:33
  • Thanks I have updated the example. However moving lines 2 & 3 with a single `&` still breaks the script. – square_eyes Sep 08 '13 at 15:44
  • try to put them in brackets... – npocmaka Sep 08 '13 at 15:53
  • I put line 2 in brackets, preceded it with `&` and moved it to line 1 and got the same result. Is that what you meant? – square_eyes Sep 08 '13 at 16:08
  • I believe what was intended was `line1&(line2)&(line3)` However, this scheme is doomed to fail because you are using `%var%` which would be replaced at parse-time by (probably) an empty string. At a guess, I'd suggest you need to include `ENABLEDELAYEDEXPANSION` in your `setlocal` and use `!var!` in place of `%var%` – Magoo Sep 08 '13 at 16:14
  • ENABLEDELAYEDEXPANSION and !var! got it. Thanks. I'll mark it s answered if you submit it. – square_eyes Sep 08 '13 at 16:39

1 Answers1

1

You need to include ENABLEDELAYEDEXPANSION in your setlocal and use !var! in place of %var%

Magoo
  • 77,302
  • 8
  • 62
  • 84