Solved

Read TXT file Loop through till EOF

Posted on 2013-05-20
9
1,320 Views
Last Modified: 2013-05-22
Hey Folks,

I'm still a novice at writing batch scripts. I need to create a script to do backups of user profiles. I have a TXT file that has a list of all computer IP's and OS version. In this format:
"WINXP - 10.10.10.1". I am using Robocopy to copy the files. So far I am able to set variables for OS and IP and use them. The problem I'm having is reading through the TXT file to the EOF. What's happening is the program is not reading pass the first record. It keeps looping the first record. I need it to advance to the next line after it processes the previous record. Here is the code I have so far, remember I'm new at this so please keep it simple.:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
:loop
for /F "tokens=3*" %%A in (AllIPList.txt) do (
set ipvar=%%A

   
for /F "tokens=1" %%B in (AllIPList.txt) do (

    ECHO My Address is %%B
    ECHO My IP is %%A

    if %%B==WIN_7 (
      goto win_7
) else if %%B==WINXP (
      goto winxp
) else if %%B=="" (
       goto exit
)

:win_7
ECHO Its Windows 7
ECHO %ipvar%
Robocopy.exe \\%ipvar%\C$\Users G:\\Profiles\%ipvar%\ /MIR /W:0 /R:0 /NP /XF *.tmp *.log /XD LocalService NetworkService temp "temporary internet files" cache Administrator "John Doe" Default Public /LOG:"C:\Users\MyDirectory\Documents

\Backup Project\%ipvar%.txt"
goto loop

:winxp
ECHO Its Windows XP
Robocopy.exe "\\%ipvar%\C$\Documents and Settings" G:\\Profiles\%ipvar%\ /MIR /W:0 /R:0 /NP /XF *.tmp *.log /XD LocalService NetworkService temp "temporary internet files" cache Administrator "John Doe" "Administrator.*" "All Users"

"Default User" /LOG:"C:\Users\MyDirectory\Documents\Backup Project\%ipvar%.txt"
goto loop

))

:exit
ECHO End of file reached Exiting program!!!
pause
exit
0
Comment
Question by:rraymond1130
  • 5
  • 4
9 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
"for /f" already creates a loop that reads all lines of a file, you don't need to add your own loop around it. Variables set inside a loop require to be expanded with "delayed expansion" enabled, using "!" instead of "%" (enter "help set" in a command prompt for details).
Try it like this; the script is currently in test mode and will only display the robocopy commands it would normally run. Remove the uppercase ECHO in line 14 to run it for real.
@echo off
setlocal enabledelayedexpansion
set Target=
set RCOptions=/MIR /W:0 /R:0 /NP
set RCExcludeFiles=/XF *.tmp *.log
set RCExcludeDirs=/XD LocalService NetworkService temp "temporary internet files" cache Administrator "John Doe" Default Public "Administrator.*" "All Users" "Default User"
for /F "tokens=1,3" %%a in (AllIPList.txt) do (
	set OS=%%a
	set ipvar=%%b
	if /i "!OS!"=="WIN_7" set Source=\\!ipvar!\C$\Users
	if /i "!OS!"=="WINXP" set Source=\\!ipvar!\C$\Documents and Settings
	set Target=G:\Profiles\!ipvar!
	set LogFile=!ipvar!.txt
	ECHO robocopy.exe "!Source!" "!Target!" %RCOptions% %RCExcludeFiles% %RCExcludeDirs% /LOG:"C:\Users\MyDirectory\Documents\Backup Project\!LogFile!"
)
echo Done.
pause

Open in new window

0
 

Author Closing Comment

by:rraymond1130
Comment Utility
Excellent That worked...  Is there a way to create a single log that captures everything that runs?
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Just set LogFile to something static and use /LOG+ instead of /LOG:
@echo off
setlocal enabledelayedexpansion
set Target=
set RCOptions=/MIR /W:0 /R:0 /NP
set RCExcludeFiles=/XF *.tmp *.log
set RCExcludeDirs=/XD LocalService NetworkService temp "temporary internet files" cache Administrator "John Doe" Default Public "Administrator.*" "All Users" "Default User"
set LogFile=ProfileBackup.txt
for /F "tokens=1,3" %%a in (AllIPList.txt) do (
	set OS=%%a
	set ipvar=%%b
	if /i "!OS!"=="WIN_7" set Source=\\!ipvar!\C$\Users
	if /i "!OS!"=="WINXP" set Source=\\!ipvar!\C$\Documents and Settings
	set Target=G:\Profiles\!ipvar!
	ECHO robocopy.exe "!Source!" "!Target!" %RCOptions% %RCExcludeFiles% %RCExcludeDirs% /LOG+:"C:\Users\MyDirectory\Documents\Backup Project\!LogFile!"
)
echo Done.
pause

Open in new window

0
 

Author Comment

by:rraymond1130
Comment Utility
Sorry to Re-Hash this question but I've noticed a weird thing happening here. For PC's that are windows 7 it seems the program is renaming the folders from the IP address name to Users? I have been going nuts all night trying to figure out why this is happening. It seems fine with the Windows XP PC's but only does it with Win 7. I haven't modified the code you gave me. If you look at the logfile the file shows the destination folder with the correct name. But if you browse to the destination folder the folder name is not the IP Address but "Users" Any ideas?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 82

Expert Comment

by:oBdA
Comment Utility
What exactly do you mean with "renaming the folders from the IP address name to Users"?
Please verify the input file format, and that the Win7 lines actually are of the exact format "WIN_7<space><hyphen><space><IP>".
0
 

Author Comment

by:rraymond1130
Comment Utility
Ok... in the AllIPList.txt file the data format is WIN_7 - 10.10.10.1. The program is reading the information correctly and populating the variables correctly. So for example here is the code to set the destination folder: set Target=G:\Profiles\!ipvar! , the destination folder should be whatever the value of the variable is. In the Robocopy log the destination folder shows correctly.
Started : Wed May 22 08:55:14 2013

   Source : \\10.8.222.41\C$\Users\
     Dest : G:\Profiles\10.8.222.41\   --> This is Correct

    Files : *.*
          
Exc Files : *.tmp
          *.log
          
 Exc Dirs : LocalService
          NetworkService
          temp
          temporary internet files
          cache
          Administrator
          Default
          Public
          All Users
          Default User
          Administrator.*
          
  Options : *.* /NS /NC /NDL /NFL /S /E /COPY:DAT /PURGE /MIR /NP /R:0 /W:0

 With Windows 7 PC's instead of the folder name being the ip address it would be "Users" after all the processing is finished.. I tried renaming the folders to the IP Address but when you ran the program again the folder name would change back to Users. For Windows XP systems there is no problem it shows
Windows 7 folder results

This is what I'm seeing with Win_7 pc's
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
This is probably just Windows Explorer playing mind games (you can't have two folders with the same name in the same folder).
Open a command prompt, enter dir /a "G:\Profiles"
You should now see all your folders with the original IP address names.
Then check the "Win_7" folders; there's probably a file "desktop.ini" in each of them (with Hidden and System attributes, so adjust the Explorer view accordingly); if you delete it, the view will return to the full name.
0
 

Author Comment

by:rraymond1130
Comment Utility
Thanks, deleting the desktop.ini renamed it back to the IP name. Now is there a better way to handle this situation than deleting the ini file? I can create a BAT file to delete them but I think this may have to be done everytime I run this program..
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Well, you can safely ignore it (you can still see the original folder names in a command prompt, and you can use Explorer by entering the IP address in the address bar).
Or you can just add it to the exclusion list.
@echo off
setlocal enabledelayedexpansion
set Target=
set RCOptions=/MIR /W:0 /R:0 /NP
set RCExcludeFiles=/XF *.tmp *.log
set RCExcludeDirs=/XD LocalService NetworkService temp "temporary internet files" cache Administrator "John Doe" Default Public "Administrator.*" "All Users" "Default User"
set LogFile=ProfileBackup.txt
for /F "tokens=1,3" %%a in (AllIPList.txt) do (
	set OS=%%a
	set ipvar=%%b
	if /i "!OS!"=="WIN_7" (
		set Source=\\!ipvar!\C$\Users
		set RCExcludeFiles=%RCExcludeFiles% "\\!ipvar!\C$\Users\desktop.ini"
	)
	if /i "!OS!"=="WINXP" set Source=\\!ipvar!\C$\Documents and Settings
	set Target=G:\Profiles\!ipvar!
	ECHO robocopy.exe "!Source!" "!Target!" %RCOptions% !RCExcludeFiles! %RCExcludeDirs% /LOG+:"C:\Users\MyDirectory\Documents\Backup Project\!LogFile!"
)
echo Done.
pause

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now