-1

I'm writing a script on a BAT file to use when necessary, to backup a folder of an application on several computers.

This script works on Windows 7: will it also work on Windows 10?

:: Backup script with logging
@echo off

net use \\SERVER\Shared_Folder userPassword /USER:userName

set PATH=c:\WINDOWS\system32;
set SRC="C:\Program Files (x86)\ApplicationName\TargetFolder"
set DST=\\SERVER\Shared_Folder\Backups
set LOG=%DST%\Backup_LogFile.log

echo:>>%LOG%
echo Backup from computer %COMPUTERNAME% >>%LOG%
echo Starts -- %DATE% %TIME% >>%LOG%
echo Wait please: backup is running...

xcopy %SRC% %DST%\%COMPUTERNAME%\ /A /D /E /J /Y /Z>>%LOG%

echo Ends -- %DATE% %TIME% >>%LOG%
echo:>>%LOG%

My script works fine but I want a better response on terminal for the user than execute it. The script adds correctly the actions on a log file, but I want the user can see only the number of file copied not the list of all files copied.

XIM
  • 35
  • 1
  • 1
  • 6
  • 3
    Get rid of the `set PATH` line, you should be very careful when modifying a built-in system variable, especially one as important as this, and one which **should** already include that location. – Compo May 08 '19 at 13:10
  • @CompoIf I don't declare the `PATH` system variable, XCOPY doesn't work... – XIM May 08 '19 at 15:14
  • Your PC is broken then, because on both Windows 7, and Windows 10, `%PATH%` should contain at least: `C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\ `. – Compo May 08 '19 at 15:19
  • @Compo I've tried and `XCOPY` works even without the `PATH` system variable! I had tried on a virtual machine and it didn't work but on this which, is a physical PC, it works properly. I'll have to reinstall that other virtual machine most likely... Thank you! – XIM May 08 '19 at 15:31
  • On that _broken_ VM do `echo %path%` and post the result please. – Gerhard May 08 '19 at 17:11

1 Answers1

1

Here is one way to accomplish what you ask. There are other ways too. The secret here is using "for /F" and sending each result to another function. The other function will log each line to a file. It will then look for xcopy's "File(s) copied" line and pipe that to the user if it sees it.

Also... note the "goto :EOF" statements. These tell the batch interpreter to return to the caller much like any other programming language.

I hope this does what you are asking. :)

:: Backup script with logging
@echo off

net use \\SERVER\Shared_Folder userPassword /USER:userName

set SRC="C:\Program Files (x86)\ApplicationName\TargetFolder"
set DST=\\SERVER\Shared_Folder\Backups
set LOG=%DST%\Backup_LogFile.log

echo:>>%LOG%
echo Backup from computer %COMPUTERNAME% >>%LOG%
echo Starts -- %DATE% %TIME% >>%LOG%
echo Wait please: backup is running...

for /f "delims=" %%f in ('xcopy %SRC% %DST%\%COMPUTERNAME%\ /A /D /E /J /Y /Z') do call :log_items "%%f"
echo Ends -- %DATE% %TIME% >>%LOG%
echo:>>%LOG%

goto :EOF

:log_items
Set InputLine=%~1

:: Log everything
echo %InputLine%>>%LOG%

:: Check if the line coming in contains "File(s) copied" if it doesn't, return
if "%InputLine:File(s) copied=%"=="%InputLine%" goto :EOF

:: If it does, show it to the user and return
echo %InputLine%
goto :EOF

The comparison done for the files copied looks like this:

For a line with your file name: (here they match so it returns)

C:\git\ps>if "test\targetver.h" == "test\targetver.h" goto :EOF

For a line with your number of files: (here they dont match do it doesn't return)

C:\git\ps>if "205 " == "205 File(s) copied" goto :EOF
Señor CMasMas
  • 4,290
  • 2
  • 14
  • 21
  • 2
    Your `set`ting of path variables are bad. Double quotes should include the variable name. `set "var=value" `. Also net use user info is best used as `/user:username password`. Your if statement on inputline will never be true because of the substitution. – Gerhard May 08 '19 at 17:20
  • I tested it. It works perfectly. If it doesn't see "File(s) copied" in the line, it returns. Other than that, nice tips. I removed his path setting from his original batch.. however; there are times to clobber the path with whatever you want.. IMHO 'it is bad' is not always true. – Señor CMasMas May 08 '19 at 17:45
  • 1
    No, you're comparing apples with oranges. if `var` is `123` and you compare `var` with `var` but substituting 2 with nothing, you're effectively doing `if 13 == 123` As for the path variables, I am refering to `set SRC="C:\Program Files (x86)\ApplicationName\TargetFolder"` the first double quote should be before the variable to make sure double quotes are not part of the value. `set "SRC=C:\Program Files (x86)\ApplicationName\TargetFolder"` – Gerhard May 08 '19 at 19:27
  • "13" == "123" is EXACTLY what I am doing. I use this method all of the time and I also always test my code. I turned echo on and scraped the text I added above. This code will break if you do "var=val" unless you also add quotes to the %SRC% on the xcopy line. There is nothing wrong with var="val" if you want the quotes included and aren't trying to append something to the path. – Señor CMasMas May 08 '19 at 20:00
  • Whats going on here?? Someone keeps up-voting incorrect comments? Sure seems fishy. – Señor CMasMas May 09 '19 at 15:29