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?

1
  • Think about using a different Tool and / or checking the errorlevel of that tool Commented Oct 23, 2015 at 18:56

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Yep. That is exactly what I was trying to imply with my code but mine removes all the leading spaces from the output.
I see, @Squashman; my intention was to achieve exactly the same results with both versions though...
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

5 Comments

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.
one & instead of %
@npocmaka - Nice EAGLE EYE. I will fix that.
@GwolfWolfy - I fixed the typo in my code. Sorry about that.
@npocmaka - I assume you have enough REP to edit my answers. Go ahead and fix it next time. I won't be offended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.