Generate dynamic log files.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Generate dynamic log files.

#1 Post by alexandredneto » 07 Mar 2013 21:19

Hi, guys.

Generate logs of commands for the content of the text file (txt). And direct them command output to a log file in which the name is formed dynamically.
Users.txt file:

Code: Select all

sp\alonso;contoso\12345678
ac\willian;contoso\87654321
rj\ronda;contoso\78662223

I'm in my house, and thinking of a script to run at work tomorrow.
I imagined a code:

Code: Select all

@echo off
SET SERVER=google
SET TXT=users.txt
SET log=%SERVER%_process.log
cd /d "C:\" rem incomplete
for /F "tokens=1,2 delims=;" %%a in (%TXT%) do (
   for /F "delims=" %%c in ('stsadm -o migrateuser -oldlogin %%a -newlogin %%b -ignoresidhistory ^> "%%a-%%b.log"') do echo %%c >> %log%
)
ECHO Bye!

The code will generate a fixed name for a file. To generate the other files variable names, for example:

Code: Select all

sp_alonso-contoso_12345678.log
ac_willian-contoso_87654321.log
rj_ronda-contoso_78662223.log
google_process.log

Before, you must treat the variables _a and _b, and then replace the slash (\) by underscore (_).
Help me with the code. So that there is failure in the code.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Generate dynamic log files.

#2 Post by Squashman » 07 Mar 2013 21:41

Why not use the slash as a delimiter. Just break it up into TOKENS=1-4 and delims=;\

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Generate dynamic log files.

#3 Post by foxidrive » 07 Mar 2013 23:17

This is as Squashman says - I think that is where you need the log file.

Code: Select all

@echo off
SET SERVER=google
SET TXT=users.txt

cd /d "C:\incomplete\folder\path"
for /F "tokens=1-4 delims=;\" %%a in (' type "%TXT%" ') do (
for /F "delims=" %%z in ('stsadm -o migrateuser -oldlogin %%a -newlogin %%b -ignoresidhistory') do >> "%%a_%%b-%%c_%%d.log" echo.%%z
)
ECHO Bye!

alexandredneto
Posts: 39
Joined: 28 Feb 2013 13:40

Re: Generate dynamic log files.

#4 Post by alexandredneto » 08 Mar 2013 09:45

Hi

I can not customize the code, for the creation of log files.
Code:

Code: Select all

@echo off
SET STSADM="C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN"
SET SERVER=google
for /f "delims=" %%d in ('CHDIR') do set DIR=%%d
SET TXT=%DIR%\users.txt
SET LOG=%DIR%\%SERVER%_process.log

for /f "tokens=1,2,3 delims=/" %%a in ("%date%") do set day=%%a&set month=%%b&set year=%%c
for /f "tokens=1,2,3 delims=:" %%h in ("%time%") do set hour=%%h&set min=%%i&set seg=%%j
>> %LOG% echo %year%-%month%-%day% %hour%:%min%:%seg% - Started

ECHO %TXT%

cd /d %STSADM%
type "%TXT%"

for /F "tokens=1-4 delims=;\" %%k in ('type "%TXT%"') do (
    SET "olddomain=%%k" & SET "olduser=%%l" & SET "newdomain=%%m" & SET "newuser=%%n"
    ECHO olddomain=%olddomain% olduser=%olduser% newdomain=%newdomain% newuser=%olduser% >>

%DIR%\%SERVER%_FROM_%olddomain%_%olduser%_TO_%newdomain%_%newuser%.log
)

cd /d %DIR%

for /f "tokens=1,2,3 delims=/" %%a in ("%date%") do set day=%%a&set month=%%b&set year=%%c
for /f "tokens=1,2,3 delims=:" %%h in ("%time%") do set hour=%%h&set min=%%i&set seg=%%j
>> %LOG% echo %year%-%month%-%day% %hour%:%min%:%seg% - Finalizado

ECHO Bye!

On the screen appears:

Code: Select all

C:\test\BAT\users.txt
sp\alonso;contoso\12345678
ac\willian;contoso\87654321
rj\ronda;contoso\78662223
Bye!

The log files are existing and google_process.log google_FROM_rj_ronda_TO_contoso_78662223.log
I hoped that the other files created:

Code: Select all

google_FROM_sp_alonso_TO_contoso_12345678.log
google_FROM_ac_willian_TO_contoso_87654321.log

The content of google_FROM_rj_ronda_TO_contoso_78662223.log is:

Code: Select all

OldDomain = rj = olduser ronda newdomain = contoso newuser = 78662223
OldDomain = rj = olduser ronda newdomain = contoso newuser = 78662223
OldDomain = rj = olduser ronda newdomain = contoso newuser = 78662223

How to solve this?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Generate dynamic log files.

#5 Post by Squashman » 08 Mar 2013 10:12

We told this to you once before. You do not need to set the token variables to another variable to use them. Just use the token variables as is.

Post Reply