# Batch script



## jat421 (Jun 14, 2008)

Hi,

I am trying to write a batch script, that will copy users "My Documents" Folder. I want to put a for loop in the script that will ping the computer first and if the ip address of the computer is in 10.10.10.0 to 10.10.10.200 range, then only it will prompt the user to copy enter user name, otherwise it will exit. This is what I got so far. It does the copy and prompt the user fine, but I don't know how to get the for or if loop working to decide only to do the backup for 10.10.10.0 to 10.10.10.200 range.


```
@echo off
Title "Back up My Documents"

ipconfig | find "IP Address" > ip.txt
for /F "tokens=* delims=:" %%I in (ip.txt) do GOTO copy

:Copy
start /wait NET USE /delete /Y z:
start /wait NET USE z: "\\servername"
set /p source="Enter your username: "
echo Please wait...Backing up My Documents...

If not exist "c:\Documents and settings\%source%" (echo You have entered wrong user name. Program will exit. Contact the admin. & pause & exit)

xcopy "c:\Documents and settings\%source%\My Documents\*.*" "z:\%source%\My Documents" /e /q /r /h /d /k /c /y /i

end if

start /wait NET USE /delete /Y z: 
exit
```
Thanks for all the help
Jat421


----------



## devil_himself (Apr 7, 2007)

if u need to search only the last octet then here a way 

```
for /l %%a in (1,1,200) do (
    ping -n 1 -w 100 10.10.10.%%a >nul
    if %errorlevel%==0 echo more code here 
)
```


----------



## jat421 (Jun 14, 2008)

Hi,

Thanks a lot for your help. I have put the for statement to do the backup, but it doesn't do anything. What am I doing wrong?


```
REM 
"This batch file backup My Documents"
@echo off
for /L  %%a in (1,1,255) do (
    ping -n 1 -w 100 10.10.15.%%a >nul
    if %errorlevel% EQL 0 GOTO copy
 
) else (GOTO pause)
 
:Copy
XCOPY "%userprofile%\My Documents\*.*" "c:\%username%\My Documents" /I/C/E/Y/R/K/D/M/V
exit
:pause
pause
```
Thanks for helping me out..really appreciate your help.


----------



## devil_himself (Apr 7, 2007)

here try this


```
REM "This batch file backup My Documents"
@echo off
for /L  %%a in (1,1,255) do (
    ping -n 1 -w 100 10.10.15.%%a >nul
    if %errorlevel% EQu 0 (goto copy) else (goto pause)
)

:Copy
XCOPY "%userprofile%\My Documents\*.*" "c:\%username%\My Documents" 

/I/C/E/Y/R/K/D/M/V
exit
:pause
pause
```


----------



## jat421 (Jun 14, 2008)

Hi, thanks you very much for your help.

The code didn't seem to work. It just copied the files from my own computer. I modified it littlt bit and it seems to loop through the IP address but not copy any files from the PC's that are alive.


```
REM "This batch file backup My Documents"
 
for /L  %%a in (1,1,254) do (ping -n 1 -w 100 10.10.15.%%a > nul)
    if errorlevel EQu 0 (goto Copy) else (echo Dead)
 
 
:Copy
XCOPY "%userprofile%\My Documents\*.*" "c:\%username%\My Documents" /e /q /r /h /d /k /c /y /i
 
endif
```


----------



## devil_himself (Apr 7, 2007)

```
if errorlevel EQu 0 (goto Copy) else (echo Dead)
```
goto in a for loop is a mess

why not use my code in post #2


```
for /l %%a in (1,1,200) do (
    ping -n 1 -w 100 10.10.10.%%a >nul
    if %errorlevel%==0 ( echo copy your files here
   )  
)
```
umm ..where do you want to copy the files ?

```
XCOPY "%userprofile%\My Documents\*.*" "c:\%username%\My Documents" /e /q /r /h /d /k /c /y /i
```
this will copy the files from the current user's my documents to c:\current username\my documents

is this what you want to achieve ?

or do you want to copy file on other shared pc's on your network ?


```
@echo off & setlocal enableextensions enabledelayedexpansion
for /l %%a in (1,1,200) do (
    ping -n 1 -w 100 10.10.10.%%a >nul
    if !errorlevel! EQU 0 xcopy "%userprofile%\My Documents\*.*" "\\10.10.10.%%a\c$\%username%\My Documents\" 
)
```
C drive is a 'hidden share'. To access it remotely you use C$.


----------



## jat421 (Jun 14, 2008)

Hi, Thanks a lot for you help!...Sorry for the confusion. Actually we have like 30 computer and I am try to copy all 30 computers My Document folder to a Backup server. I will test the script you posted and let you know how it goes. Again thanks for all the help!.


----------



## devil_himself (Apr 7, 2007)

> all 30 computers My Document folder to a Backup server




```
@echo off & setlocal enableextensions enabledelayedexpansion
for /l %%a in (1,1,200) do (
    ping -n 1 -w 100 10.10.10.%%a >nul
    if !errorlevel! EQU 0 xcopy "\\10.10.10.%%a\c$\%username%\My Documents\" \\anotherserver\backups
)
```


----------

