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
Solved

Read TXT file Loop through till EOF

Posted on 2013-05-20
9
1,394 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 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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 …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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