BATch question(s)

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

BATch question(s)

Post by VegasNath »

Hello,
The following (as an example) BATch script.......

Code: Select all

@echo off & setLocal EnableDelayedExpansion
echo.
echo BATch file started on !Date! @ %time:~0,5%
echo.
copy "C:\Users\Nathan Docs\X.txt" "C:\Users\Nathan Docs\Y.txt"
echo X copied to Y.
echo.
echo BATch file finished on !Date! @ %time:~0,5%
echo.
echo Press any key to exit. & pause > nul
...... produces the following result:
Capture2.JPG
Q1: Is there a way to stop the line "1 file<s> copied" from being recorded in the output? I would rather use something more meaningful, "echo X copied to Y"
Q2: Is there a way to capture the result of the instruction, whether it was a success or a failure? I would like to specify (for example) "echo copy X to Y successful" or "echo copy X to Y failed"
You do not have the required permissions to view the files attached to this post.
:wales: Nathan :uk:
There's no place like home.....

User avatar
StuartR
Administrator
Posts: 12604
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: BATch question(s)

Post by StuartR »

You can direct the output of the copy command somewhere else by putting > after the command and then the target, so:
copy "C:\Users\Nathan Docs\X.txt" "C:\Users\Nathan Docs\Y.txt" > "C:\Users\Nathan Docs\result.txt"
will send the 1 file(s) copied to a new text file called result.txt, and
copy "C:\Users\Nathan Docs\X.txt" "C:\Users\Nathan Docs\Y.txt" > NUL
will just discard the output.

You can test the result of many commands, including copy, by looking at the environment variable errorlevel, so you would use something a bit like:
copy "C:\Users\Nathan Docs\X.txt" "C:\Users\Nathan Docs\Y.txt" > NUL
if %errorlevel%=0 goto success
Echo it failed
:success
StuartR


JoeP
SilverLounger
Posts: 2069
Joined: 25 Jan 2010, 02:12

Re: BATch question(s)

Post by JoeP »

You should investigate using Technet - Robocopy. It is standard in Vista, Windows 7, Windows Server 2008, & Server 2008 R2. If you have a prior version of Windows you can get it from the Windows Server 2003 resource kit tools.

Robocopy is much more robust than Copy. See The Coolest Command-Line Tool in Windows Vista - Tim Sneath - Site Home - MSDN Blogs for more information. See Return codes that are used by the Robocopy utility in Windows Server 2008 for even more.

If you like Robocopy and want to use it outside of the command line environment, you can download a free GUI frontend from Robocopy! - Nick MacKechnie - Site Home - MSDN Blogs.

Joe
Joe

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: BATch question(s)

Post by VegasNath »

Thankyou very much Stuart! Your response has given me everything that I need. :cheers:
This appears to suit...

Code: Select all

copy "C:\Users\Nathan Docs\X.txt" "C:\Users\Nathan Docs\Y.txt" > NUL
If %errorlevel% NEQ 1 (echo Copy X to Y - SUCCESSFUL @ %time:~0,5%) Else (echo Copy X to Y - FAILED @ %time:~0,5%)

copy "C:\Users\Nathan Docs\F.txt" "C:\Users\Nathan Docs\G.txt" > NUL
If %errorlevel% NEQ 1 (echo Copy F to G - SUCCESSFUL @ %time:~0,5%) Else (echo Copy F to G - FAILED @ %time:~0,5%)
Joe, thanks for the suggestions and links, I will certainly take a look. :thankyou:
:wales: Nathan :uk:
There's no place like home.....

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: BATch question(s)

Post by VegasNath »

A new question.
Using some VBA supplied by Hans recently, I am using excel to call/execute a Bat file at a specific time daily. Is it posible to reverse the scenario and execute a macro in the bat calling workbook.
eg:
Excel instigates bat
Bat copies / creates various files
Once complete, bat calls another macro in the same workbook that originally called the bat.

:question:

TIA.
:wales: Nathan :uk:
There's no place like home.....