Solved

Read TXT file Loop through till EOF

Posted on 2013-05-20
9
1,422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 39181774
"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
ID: 39182488
Excellent That worked...  Is there a way to create a single log that captures everything that runs?
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39182714
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:rraymond1130
ID: 39187390
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
 
LVL 84

Expert Comment

by:oBdA
ID: 39188839
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
ID: 39189013
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 84

Expert Comment

by:oBdA
ID: 39189035
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
ID: 39189610
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 84

Expert Comment

by:oBdA
ID: 39189979
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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