0

So I'm trying to copy a local file to my network using batch scripting and would like to be able to get a result to know if the process completed properly.

My code is below:

@echo off
set locatie_folder=G:\Info\instructiuni.mht
for /f "tokens=1,2 delims= " %%a in (IP.txt) do (
    copy %locatie_folder% "\\%%a\vsr">> temp.tmp
    set /p VAR=<temp.tmp
    echo %%b %VAR%
    del temp.tmp
)
pause
exit

My IP.txt file looks like so:

10.117.14.10 100-01
10.117.14.11 100-02
10.117.14.12 100-03

First is the IP and second is a dedicated name for that IP in the network.

I need to output something like

100-01 1 file(s) copied.
100-02 0 file(s) copied.
100-03 1 file(s) copied.

Now my problem is that for some reason the variable VAR is not getting me the status "0 file(s) copied." or "1 file(s) copied." while reading even if it writes properly in the temp.tmp file.

What am I doing wrong here?

Mofi
  • 46,139
  • 17
  • 80
  • 143

2 Answers2

2

You need to enable delayed expansion for getting the new value of VAR, because you are setting and reading it within a block of code (the for loop). The immediate %VAR% expansion always returns the value when the entire block of code is parsed; with delayed !VAR! expansion, you will get the updated value.

Try this:

@echo off
setlocal EnableDelayedExpansion

set "locatie_folder=G:\Info\instructiuni.mht"

for /F "usebackq tokens=1,2 delims= " %%A in ("IP.txt") do (
    > "temp.tmp" copy "%locatie_folder%" "\\%%~A\vsr"
    < "temp.tmp" set /P VAR=
    echo "%%~B" !VAR!
    del "temp.tmp"
)
pause
endlocal
exit /B

You can make it all even simpler though, without using a temporary file, when you let another for /F loop capture the output of the copy command:

@echo off

set "locatie_folder=G:\Info\instructiuni.mht"

for /F "usebackq tokens=1,2 delims= " %%A in ("IP.txt") do (
    for /F "delims=" %%X in ('copy "%locatie_folder%" "\\%%~A\vsr"') do (
        echo "%%~B" %%X
    )
)
pause
exit /B
Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    Yep. That is exactly what I was trying to imply with my code but mine removes all the leading spaces from the output. – Squashman Oct 23 '15 at 19:35
  • I see, @Squashman; my intention was to achieve exactly the same results with both versions though... – aschipfl Oct 23 '15 at 20:06
1

Put your COPY command inside a FOR /F command to capture the verbose output to a variable. Something like this.

for /F "tokens=* delims= " %%G in ('copy temp.txt C:\temp\') do echo %%b %%G

Edit: with your exact code.

@echo off

set locatie_folder=G:\Info\instructiuni.mht

for /f "tokens=1,2 delims= " %%a in (IP.txt) do (
    for /F "tokens=* delims= " %%G in ('copy %locatie_folder% "\\%%a\vsr"') do echo %%b %%G
)
pause
exit
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • 1
    seems to not work correct me if am wring it properly `@echo off set locatie_folder=G:\PPE\04.Process_technology\05_Cutting_Technology\12_Instructiuni_de_lucru\instructiuni\CV\instructiuni.mht for /f "tokens=1,2 delims= " %%a in (IP.txt) do ( for /F "tokens=* delims= " %&G in ('copy %locatie_folder% "\\%%a\vsr"') do echo %%b %%G ) pause exit` I'm getting a message locatie_folderG was unexpected at this time. – Lup Gabriel Oct 23 '15 at 18:50
  • 1
    one `&` instead of `%` – npocmaka Oct 23 '15 at 19:15
  • @npocmaka - Nice EAGLE EYE. I will fix that. – Squashman Oct 23 '15 at 19:40
  • @GwolfWolfy - I fixed the typo in my code. Sorry about that. – Squashman Oct 23 '15 at 19:42
  • @npocmaka - I assume you have enough REP to edit my answers. Go ahead and fix it next time. I won't be offended. – Squashman Oct 23 '15 at 19:46