0

Here is what I've come up with, however I can't get the correct reference of xcopy in my code from what Aacini has provided in another post. cmd console will say it cannot find file "!lastName!!baseExt!" and then show that 0 files have been copied. It's not copying because I think the xcopy syntax will not allow for substitutions for the "source" "directory" relationship following xcopy.

Any help would be much appreciated. Thanks

@Echo Off
:: variables
set drive=C:\Users\me\Desktop\Test Source Folder
set backupcmd=xcopy /m /s /c /d /e /h /i /r /y /exclude:AutoFileCopy_Rev1.bat

set basename=
for %%a in ("C:\Users\me\Desktop\Test Source Folder") do (
    if not defined baseName (
    rem Is first name of first set
    set baseName=%%~Na
    set baseExt=%%~Xa
    set lastname=%%~Na
    ) else (
    rem Check if this name begin with same baseName
    set name=%%~Na
    for %%b in (!baseName!) do set name=!name:*%%b=!
      if "!name!" neq "%%~Na" (
         rem Yes: Is next name of same set
         set lastName=%%~Na
      ) else (
         rem No: Is first name of next set: copy previous set and pass to next one
         %backupcmd% "!lastName!!baseExt!" "C:\Users\me\Desktop\Test Source Folder\!    baseName!!baseExt!"
         set baseName=%%~Na
         set baseExt=%%~Xa
         set lastName=%%~Na
      )
   )      
)
rem Copy last set

Set _Delay=10
Set _Monitor=C:\Users\me\Desktop\Test Source Folder\
Set _Base=%temp%\BaselineState.dir
Set _Chck=%temp%\ChkState.dir
Set _OS=6
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 Set _OS=5 & Set /A _Delay=_Delay*1000
:_StartMon
Call :_SetBaseline "%_Base%" "%_Monitor%"
:_MonLoop
If %_OS%==5 (Ping 1.0.0.0 -n 1 -w %_Delay%>Nul) Else Timeout %_Delay%>Nul
Call :_SetBaseline "%_Chck%" "%_Monitor%"
FC /A /L "%_Base%" "%_Chck%">Nul
If %ErrorLevel%==0 Goto _MonLoop



echo ___ Backing up JobBoss files...
::%backupcmd% "C:\Users\john.weakley\Desktop\Test Source     Folder" "C:\Users\me\Desktop\Test Destination Folder\"

::CALL "C:\users\me\Desktop\Test Source Folder\Test.bat"

ECHO ___ Checking for new file revisions...
%backupcmd% "!lastName!!baseExt!" "C:\Users\me\Desktop\Test Source Folder\!baseName!!    baseExt!"



Echo.Backup Complete!

Goto :_StartMon
:::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutine
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetBaseline
If Exist "%temp%\tempfmstate.dir" Del "%temp%\tempfmstate.dir"
For /F "Tokens=* Delims=" %%I In ('Dir /S "%~2"') Do (
Set _Last=%%I
>>"%temp%\tempfmstate.dir" Echo.%%I
)
>"%~1" Findstr /V /C:"%_Last%" "%temp%\tempfmstate.dir"
Goto :EOF
cheapkid1
  • 469
  • 7
  • 18
  • 32

1 Answers1

1

You need to enable delayed expansion in order to support !VAR! syntax.

Change your first line to:

@echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Ok, now it displays "Cannot perform a cyclic copy". Does that mean I have some code out of order? – cheapkid1 Nov 29 '12 at 18:25
  • 1
    Instead of actually trying to do the copy, `echo` the command that would run, as in `@echo %backupcmd%`. What does it show you? (Also, check the spaces in the "Checking for new file revisions" between `!basenName!!` and ` baseext!' – Ken White Nov 29 '12 at 18:32
  • I put '@echo' before '%backupcmd%' and checked spacing and it displays ___ Backing up JobBoss files...(next line) Checking for new file revisions...(nl) xcopy /m /s /c /d /e /h /i /r /y /exclude:AutoFileCopy_Rev1.bat "Test Source Folder" "C:\Users\me\Desktop\Test Source Folder\Test Source Folder" (nl) Backup Complete – cheapkid1 Nov 29 '12 at 18:51
  • 1
    Read the actual folder names. The xcopy can't tell what it's supposed to do; it thinks you want to copy files from the source folder into the source folder, and of course that's not allowed. IOW, you can't copy "Test Source Folder" to "Test Source Folder" when they're both the same place, and as far as the xcopy command you're providing it thinks they are one and the same. – Ken White Nov 29 '12 at 19:39
  • Understand that, ok. I made sure that looks correct now, but is there a way I can call the for else loop as a subroutine? Because the order it's in now runs the for else loop once, then gets stuck at the Set Delay - line in the code - and stays there everytime a new file is created or updated. Not sure how to properly subroutine this correctly. – cheapkid1 Nov 29 '12 at 20:34
  • Ok, I tried doing a subroutine on the "for else loop" and it works, but now I have the cmd console asking me everytime wether or not it's to specify a file name or a directory name on the target. Any way around that? I thought /y got rid of that, but it's there.?? – cheapkid1 Nov 29 '12 at 20:46
  • 1
    That's a separate question on its own, and should be submitted as one. (One question per post is the general rule here, and your original question was about fixing your "cannot find file "!lastName!!baseExt!"" issue. Separating out the else loop into it's own subroutine is a different issue altogether. (That other question can be much shorter, like a question and a short `if ... ( something) else ( something else )`, with a link back to this question for more information. – Ken White Nov 29 '12 at 20:48
  • Roger that. Thanks for your help I appreciate it. – cheapkid1 Nov 29 '12 at 20:57
  • 1
    Don't know about that. `/Y` just suppresses the `File exists. Overwrite?` prompt. The closest I can see that might apply to file/directory is `/I`, but you're using that already. (See `xcopy /?` at the command prompt.) – Ken White Nov 29 '12 at 20:59