Simple Batch File

June 3, 2009 at 15:28:26
Specs: Windows XP
Hi Guys,

I am trying to write a batch file that will:
1. Read a text document(Ex:loginlog.txt)
2.find the comma place and save all the text before the comma into a variable
%computerName% and then save the name into a file.

here are some lines from the file im trying to pull the computer names from:
((((((((((((((((((((((((((((((
W2BMHLABTEMP,11/6/2007 8:51:55
CDCOX1,11/8/2007 3:47:46
BIRMETRO1,11/9/2007 8:14:58
)))))))))))))))))))))))))))))))

Any suggestions?

Thanks in advance

Adam


See More: Simple Batch File

Report •


#1
June 3, 2009 at 19:18:27
if you have Python on Windows
for line in open("logfile.txt"):
    if "," in line:
        computername=line.strip().split(",")[0]
        print computername


Report •

#2
June 4, 2009 at 05:45:37
unfortunatly, I do not have python.
This batch file will be ran on close to 10,000 PCs so im trying to keep from having to install anything extra. =T

Thanks for the suggestion though!


Report •

#3
June 6, 2009 at 02:08:16
@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=1 delims=," %%a in (thefile) do (
echo %%a>> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
June 10, 2009 at 10:05:58
Thank you so much Mech!!
The code worked beautifully!!!

By chance would you know how to reverse the order of the computers in the list?

for instance:

CDCOX1
BIRMETRO1
BIRMETRO1
INBOUND2
-------------------

This is the list I am getting.. I would like to list the bottem one first(Inbound2) because it is the last computer that was logged into. Woul it be possible to reverse the order?


Report •

#5
June 10, 2009 at 18:06:02
2 ways you can do reverse, one , do it with your original file, which i believe you have no control over it. second use arrays and some routine to reverse order.

I know you can't install but here's the solution in Python anyway, just to show the concept.

import sys
logfile=sys.argv[1]
computers=[]
for line in open(logfile):
    if "," in line:
        computername=line.strip().split(",")[0]
        computers.append(computername)
for items in reversed(computers):
    print items

output
C:\test>more file.txt
W2BMHLABTEMP,11/6/2007 8:51:55
CDCOX1,11/8/2007 3:47:46
BIRMETRO1,11/9/2007 8:14:58

C:\test>python test.py file.txt
BIRMETRO1
CDCOX1
W2BMHLABTEMP


>>>>> This batch file will be ran on close to 10,000 PCs so im trying to keep from having to install anything extra. =T

here's the executable if you are interested (tested on XP,W2k) which you can bring to your PCs using your deployment tool.


Report •

#6
June 10, 2009 at 19:23:29
@echo off > newfile & setLocal EnableDelayedExpansion

for /f "tokens=1 delims=," %%a in (thefile) do (
echo %%a>> newfile
)
===============================

I guess the output would be better called: reversed; but you get the idea.

set N=0

for /f "tokens=1 delims=" %%a in (newfile) do (
set /a N+=1
set v!N!=%%a
)

@echo off > sorted.txt

for /L %%i in (!N! -1 1) do (
echo !v%%i!>> sorted.txt
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#7
June 10, 2009 at 19:45:59
@m2, can it be done without creating intermediate file (ie newfile) ? it will be more efficient that way.

Report •

#8
June 10, 2009 at 19:58:05
Hi ghostdog,

Good question.

============================
@echo off > newfile & setLocal EnableDelayedExpansion

set N=0

for /f "tokens=1 delims=," %%a in (thefile) do (
set /a N+=1
set v!N!=%%a
)

for /L %%i in (!N! -1 1) do (
echo !v%%i!>> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#9
June 11, 2009 at 06:51:28
Mech thanks, once again the code worked great!
I'm now moving onto the next step of the batch and instead of making many post with diffrent things/questions, I am going to just write in sudo code what I am trying to do and if you guys have any suggestions or code that might help out please let me know =).

-------------------------------
The purpose of this batch file is to decompress a users profile on the server that is experiancing encryption/compression issues.
--------------------------------
1. prompt user for their user id
2.look at the log files on the server and find out the last computer or computers(if the last one they logged into is not online check the next).
3.From the last PC they logged in, remotly get an enviroment variable and/or registry key that list their profile servername (commanline example: echo %homeserver% = alxaptr2)
(Registry example: [HKEY_CURRENT_USER\Environment]
"HOMESERVER"="AGERPFS15")
4. Once their homeserver has been found, run the following command to decompress their profile:
compact /a /u %homeserver%/%username%.USR/*.*
---------------------------------------------------------------
___________________________________________
Somewhat sudo code
(exactly whats written above except Simplified)

1.Prompt for userID
2.get logfile from network (example path:\\logs\%user%.txt)
3.find last PC logged into
4 ping that PC to see if it is online
5. if so then goto 6 if not then goto next pc logged into and ping it, if it is online goto 6 if not goto next pc logged in(repeat until we find one that is online)
6.get homeserver name from last PC via registry or wmic
7.decompress profile on homeserver


---------------------------------------------------------------------
ok so mabey this batch file isnt so simple =T

Thanks for any and all help though guys (Mech!)


Report •

#10
June 11, 2009 at 06:53:21
I meant to post what I Had so far:

______________________________________
@echo off > c:\tempfile & setLocal EnableDelayedExpansion
cls
echo --------------------------------------------------------------
echo BLT Profile Decompression Tool v3.1
echo --------------------------------------------------------------
SET /P Choice=Type in the user ID that is having profile problems:
for /f "tokens=1 delims=," %%a in (\\logs.blah.com\logs$\UserLogs\Logins\%Choice%.txt) do (
echo Retreaving Computer Name: %%a
echo %%a>> %Choice%.txt
)


set N=0
for /f "tokens=1 delims=" %%a in (%Choice%.txt) do (
set /a N+=1
set v!N!=%%a
)
@echo off > Loginlog.txt
for /L %%i in (!N! -1 1) do (
echo !v%%i!>> Loginlog.txt
echo Sorting Computer Name: %%i
)


____________________________________


Report •

#11
June 11, 2009 at 08:00:51
The code I just posted is what I have so far and takes care of steps 1-3 in my sudo code example, above the previous post.

Report •

#12
June 11, 2009 at 10:24:59
Looks like you've got it right so far. Now code the ping.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#13
June 11, 2009 at 12:42:23
ok so I do like a
C:\>Ping %computerName% > pingstats.txt

and somehow check the 4th line to see if it has the word
Reply in it. If so, GOTO BlahBlah if not, goto next computer name. Or how do you think I should check in the code if it was successful or not?

------------------------------------------------------------
Failed Attempt
-------------------------------------------------------------

Pinging birmetro1.southernco.com [172.25.102.39] with 32 bytes of data:

Request timed out.

Request timed out.

Request timed out.

Request timed out.

Ping statistics for 173.25.102.39:

Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),


--------------------------------------------------------------------------
Sucessful Attempt
--------------------------------------------------------------------------
Pinging birmetro2.southernco.com [172.25.88.185] with 32 bytes of data:

Reply from 172.25.88.185: bytes=32 time<1ms TTL=128

Reply from 172.25.88.185: bytes=32 time<1ms TTL=128

Reply from 172.25.88.185: bytes=32 time<1ms TTL=128

Reply from 172.25.88.185: bytes=32 time<1ms TTL=128

Ping statistics for 172.25.88.185:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

Approximate round trip times in milli-seconds:

Minimum = 0ms, Maximum = 0ms, Average = 0ms


Report •

#14
June 11, 2009 at 12:56:25
Usually best to avoid creating files.

ping localhost | find "Reply" > nul
if errorlevel 1 goto :failed
goto :OK


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#15
June 11, 2009 at 13:42:31
Good call..
would it be possible to do this...

if NOT errorlevel 1 goto :PASSED
instead of :if errorlevel 1 goto :failed

I can't seemd to get the failed working correctly:


-------------
What Im workin with--------
------------------------------------------------------------------------
@echo off > c:\loginlog.txt
for /L %%i in (!N! -1 1) do (

echo !v%%i!>> c:\loginlog.txt
echo Pinging: !v%%i!
ping -w 1 !v%%i! | find "Reply" > nul
if errorlevel 1 goto :failed
goto :OK
:failed
echo Can't Connect to !v%%i!
pause
)

pause
exit /b
:OK
echo Can Connect to:!v%%i!
pause
--------------------------------------------------------


Report •

#16
June 11, 2009 at 15:22:57
I see nothing wrong with your code.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#17
June 12, 2009 at 06:24:09
well I changed the code a little to this: and it seems that when it fails the ping..it goes out of the loop and dosent try the next computername.

--------------------------------
code
----------------------------------
set N=0
for /f "tokens=1 delims=" %%a in (c:\%Choice%.txt) do (
set /a N+=1
set v!N!=%%a
)
@echo off > c:\loginlog.txt
for /L %%i in (!N! -1 1) do (

echo !v%%i!>> c:\loginlog.txt

echo Pinging: !v%%i!
ping !v%%i! | find "Reply" > nul
if errorlevel 1 goto :failed
goto :OK
:failed
echo Nooooooooooooooooooo cant get !v%%i!
pause
)

exit
pause

:OK
echo woooooooooooooo connected !v%%i!
pause
--------------------------------
output
-------------------------------
Retreaving Computer Name: DLMACON1
Sorting.....
Please Wait
Pinging: DLMACON1
Nooooooooooooooooooo cant get
Press any key to continue . . .

(At this point, I press enter and the command window closes without checking the next computer
------------------------------------------------------------

any thoughts? =T)


Report •

#18
June 12, 2009 at 07:04:26
I think your goto :failed is busting out of the for loop.

Try changing it to:

call :failed

and put :failed outside the loop.

[not tested]


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#19
June 12, 2009 at 07:18:45
where would I need to put failed outside of the loop to where it would continue the the loop until it found one that was online?

would it be possible to use

if NOT errorlevel 1 CALL/GOTO :success


Report •

#20
June 12, 2009 at 07:32:31
I tested..and IF NOT DOES WORK!!!!!! w0oohoo!!!
next problem though:

its reading from the list like this:
_________________
birmetro1
birmetro1
birmetro1
cdcox2
cdcox2
jghou7
____________________

so it pings birmetro1 3 times before it moves to cdcox2

What kind of if statment could I use to check and see if the new computer name matches the previously used computer name.

example

If %computername% == %oldcomputername% goto :skip
else
(continue with pinging)
---------------------------------------------------------------
This is what I have so far that seems to be working
----------------------------------------------------------------

set N=0
for /f "tokens=1 delims=" %%a in (c:\%Choice%.txt) do (
set /a N+=1
set v!N!=%%a
echo %%a


)
@echo off > c:\loginlog.txt
for /L %%i in (!N! -1 1) do (

echo !v%%i!>> c:\loginlog.txt


echo Pinging: !v%%i! .....
ping -w 10 !v%%i! | find "Reply" > nul
if not errorlevel 1 GOTO :OK
echo Nooooooooooooooooooo cant get !v%%i!
pause

)
echo loop end
pause
exit


:OK
echo woooooooooooooo connected !v%%i!
pause


Report •

#21
June 12, 2009 at 08:57:58
set N=0
for /f "tokens=1 delims=" %%a in (c:\%Choice%.txt) do (
set /a N+=1
set v!N!=%%a
echo %%a
)

@echo off > c:\loginlog.txt

for /L %%i in (!N! -1 1) do (
ping -w 10 !v%%i! | find "Reply" > nul
if not errorlevel 1 GOTO :OK
if errorlevel 1 call :failed
)

goto :eof

:OK
echo connected !v%%i!
goto :eof

:failed
echo failed
goto :eof


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question