1

I have an issue with my batch file that incorporates a 10sec delay between everytime it looks at a specified folder then CALLS a subroutine for naming convention:...batch that copies modified files in directory to another directory

I have modified the code according to Ken White which has been very helpful, however I have another issue that is further related to this batch, being that when the batch is ran and I do a test file update and save within the source directory, the batch asks me in the cmd console "to specify a file name or a directory name on the target" for EVERY file that it looks at. Since I want this to be automatically running in the background I don't want to have to have user input for it to run properly. Any help or suggestions would be very helpful.

Here is what I have currently:

@Echo Off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:: variables
set drive=C:\Users\me\Desktop\Test Source Folder\
set backupcmd=xcopy /m /s /c /d /e /h /i /r /y
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

:_RevLoop
set basename=
for %%a in (*.*) 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 Destination Folder\!baseName!!baseExt!"
         set baseName=%%~Na
         set baseExt=%%~Xa
         set lastName=%%~Na
      )
   )      
)
rem Copy last set
%backupcmd% "!lastName!!baseExt!" "C:\Users\me\Desktop\Test Destination Folder\!    baseName!!baseExt!"


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\me\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...


GOTO :_RevLoop


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
Community
  • 1
  • 1
cheapkid1
  • 469
  • 7
  • 18
  • 32
  • possible duplicate of [xcopy file, rename, suppress "Does xxx specify a file name..." message](http://stackoverflow.com/questions/3018289/xcopy-file-rename-suppress-does-xxx-specify-a-file-name-message) – Neil Nov 29 '12 at 22:18

1 Answers1

1

Assuming you don't need to rename the files as you copy them, try

%backupcmd% "!lastName!!baseExt!" "C:\Users\me\Desktop\Test Destination Folder\"

Note: I find that the trailing \ negates the need for the /i switch.

Edit: New strategy. Copy the base file first using the above command, then when you copy the final file it will overwrite it without prompting.

for %%a in (*.*) do (
    if not defined baseName (
        rem Is first name of first set
        set baseName=%%~Na
        set baseExt=%%~Xa
        set lastname=%%~Na
        %backupcmd% "%%a" "C:\Users\me\Desktop\Test Destination Folder\"
    ) 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 Destination Folder\!baseName!!baseExt!"
            set baseName=%%~Na
            set baseExt=%%~Xa
            set lastName=%%~Na
            %backupcmd% "%%a" "C:\Users\me\Desktop\Test Destination Folder\"
        )
    )
)
Neil
  • 54,642
  • 8
  • 60
  • 72
  • I do need to rename them, source directory contains (ie) example.xls and exampleA.xls. – cheapkid1 Nov 29 '12 at 21:58
  • I do need to rename them, source directory contains (ie): example.xls and exampleA.xls, the dest dir contains: example.xls. The batch file will ev 10s monitor the source dir and copy any updated or new files (ie) exampleA.xls would be copied into the dest dir as example.xls(replacing the existing file named example.xls) – cheapkid1 Nov 29 '12 at 22:08
  • Can anyone help me with this problem? – cheapkid1 Dec 10 '12 at 15:49
  • It still asks me in the cmd console "to specify a file name or a directory name on the target" for EVERY file that it looks at. And for some reason it's not copying all the test files in the source folder that should be copied over to the destination folder with that latest edit. I don't understand why it still asks me that when I'm using xcopy. – cheapkid1 Dec 12 '12 at 19:59
  • It worked before all except for the specify 'F' for filename or 'D' for directory name on target message in the console. That's what I need to go away, I don't want to be prompted on what supposed to be a background running automated console batch. – cheapkid1 Dec 12 '12 at 21:00
  • 1
    @cheapkid1 Whoops, I typoed my `%a`s - they should of course be `%%a`. Sorry about that. Does that help? – Neil Dec 13 '12 at 21:26
  • That worked, except it lists in the console even the files that it's not copying as well. Such As 0 File(s) copied. If I have a lot of files, which I do. This will still pose a time consuming problem. Is there a way to just display only the files being copied?? – cheapkid1 Dec 26 '12 at 17:34
  • @cheapkid1 Perhaps you could pipe the output of the command into `find /v "0 File(s) copied"`. – Neil Dec 26 '12 at 21:39
  • Would that make the process quicker? Or will the batch still check every file regardless, just only displaying the files that were copied? – cheapkid1 Dec 27 '12 at 22:03
  • 1
    The slowest part of the operation will be copying the files that need to be copied. Only the first file of each batch is checked, and that operation will be comparatively quick. Reducing the output using `find /v` may actually end up making a bigger speed difference. – Neil Dec 27 '12 at 23:54
  • where do I place the find /v "0 File(s) copied"? – cheapkid1 Dec 28 '12 at 16:41
  • 1
    @cheapkid1 You could append `| find /v "0 File(s) copied"` to the end of both the `%backupcmd%` lines or alternatively changing the last line to `) | find /v "0 File(s) copied"` should also work. – Neil Dec 29 '12 at 00:14