windows - copying files using a batch script throws a syntax error -


i have following batch script , when run it, throws error:

@echo off cls /f "tokens=2 delims==" %%i in ('wmic os localdatetime /format:list') set datetime=%%i  set datetime=%datetime:~0,8%-%datetime:~8,6%  set agfile=c:\vendor\my work\file.txt" if exist %agfile%  ( echo "file exists" copy %agfile% %agfile%.%datetime% ) 

when run script, syntax error. how fix syntax error @ copy part?

you wrote

if exist %agfile%  ( echo "file exists" copy %agfile% %agfile%.%datetime% ) 

but syntax error. action taken if when true must on same line if token. easy fix:

if exist %agfile% (   echo "file exists"   copy %agfile% %agfile%.%datetime% ) 

which works because ( begins compound statement, , beginning on same line if sufficient. similarly, if need use else in code this, needs written ) else ( on 1 line.


Comments