Batch file help.

Status
Not open for further replies.

Rastlin17

Seniorius Lurkius
1
Good evening all,

I'm trying to write a batch file (paste 1) that needs to read a list of computers from a text file and ping them to ensure that they are online, and if they are online it needs to copy over a folder located on my desktop to each specific machine on the list. The batch file only writes the following information to the log file (paste 2) and I think that my problem lies in either my variables or my FOR statement. I would greatly appreciate anyone's help in this matter, thanks in advance.

Paste 1:

::This is a batch file that will copy the revised Employee Management databse to a list of computers
CLS
@Echo off
::Create log file
ECHO This is a test of copying log > C:\This_is_a_test_of_copying_logfile.txt

::Set Variables
set whack=\
for /f "tokens=1" %%1 in (C:\This_is_a_test_list.txt) DO CALL :FILECOPY %%1
:FILECOPY
set pcname=%1%
set UNC=%whack%%whack%%pcname%

::Check target status
Ping –n 1 %1 | find "Reply" > nul
IF %ERRORLEVEL% EQU 1 (
GOTO OFFLINE
) ELSE (
GOTO COPY
)

:COPY
IF EXIST "%UNC%\C$\Program Files\Employee Management" (
COPY /Y "C:\Documents and Settings\pptadmsanchezjc\Desktop\Test Copy\*.*" "%UNC%\C$\Program Files\Employee Management\*.*
"ECHO New Employee Management Front End copied on %UNC% >> C:\This_is_a_test_of_copying_logfile.txt
GOTO END
) ELSE (
xcopy “C:\Documents and Settings\pptadmsanchezjc\Desktop\Test Copy" "%UNC%\C$\Program Files\Employee Management" /i /y /e
IF NOT EXIST "%UNC%\C$\Program Files\Employee Management" ECHO %UNC% was alive but Employee Management did not get copied over >> C:\This_is_a_test_of_copying_logfile.txt
)

:OFFLINE
ECHO %1 is offline >> C:\This_is_a_test_of_copying_logfile.txt

:END

Paste 2:
This is a test of copying log
PPTTPCTRAIN01 is offline
PPTTPCTRAIN02 is offline
PPTTPCTRAIN03 is offlineis
offline
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Rastlin17":1rgoqt2z said:
for /f "tokens=1" %%1 in (C:\This_is_a_test_list.txt) DO CALL :FILECOPY %%1
:FILECOPY
set pcname=%1%
This looks wrong.
  • Do you really mean to use %1 as FOR's loop variable? It's probably better to use different name since %1, %2, etc. typically refer to command-line arguments, and using them for anything other than that is pretty confusing.
  • %1% should be just %1

If that doesn't help, to debug your script, I'd recommend:
  • Entering the commands manually.
  • Echoing the commands you run with the variables expanded.
  • Running your script without "@echo off".
 

bk0

Ars Tribunus Militum
1,801
Not difficult in python:

Code:
#!/usr/bin/env python3

#Untested!

import os, shutil

srcpath = "C:\Documents and Settings\pptadmsanchezjc\Desktop\Test Copy\"
targetpath = "\C$\Program Files\Employee Management\Test Copy"

file = open("list.txt", "r")

for site in file:
    if os.system("ping -n 1 {}".format(site)):
        print("Copying to: {}".format(site))
        shutil.copytree(srcpath, "{0}{1}".format(site, targetpath))

file.close()

Though I'm surprised there apparently is no ping function in the standard library.
 
Status
Not open for further replies.