Articles

Please review for me (batch file)

self
September 16, 2006 at 09:08:39
Specs: Windows XP, 6.8 ghz/2 gb ddr2800

Hey all, i'm running into a little problem with my batch file. To explain, i am trying to use the rename command (as well as a few other commands) that produce responses without errorlevels hardcoded in. If for example, i were to rename a file to a name that is already in use, it produces: "A Duplicate file name exists, or the file cannot be found", however, there is no error code for it, and i need to be able to reference it in a if /I "%1"=="A Duplicate file name exists, or the file cannot be found" goto:resolution1 format. Here is what i have so far in the batch file:
ECHO ***Renaming file to include computername for better logging purposes***
if exist C:\bootex.log goto:process1
else goto:error1

:process1
rename c:\bootex.log %computername%chkdsk.log
if /I "%1"=="A Duplicate file name exists, or the file cannot be found" goto:resolution1
goto:process2

:process2
ECHO ***Moving file that has been renamed to central log file location***
move c:\%computername%chkdsk.log "\\fl01_admin\home\mis\chkdsk logs"
if /I "%1"=="Access is denied." goto:error2
goto:end


:resolution1
ECHO ***Deleting previous file that caused an error and reattempting process***
del C:\%computername%chkdsk.log /q
goto:process1

:resolution2
move C:\%computername%chkdsk.log "\\fl01_admin\home\logs"
if /I "%1"=="Access is denied." goto:error3
else goto:res2part2
:res2part2
if /I "%1"=="1 file(s) moved." goto:end
goto:error4

:error1
ECHO ***File does not exist. This is probabbly normal and chkdsk has not been run recently, the file is exiting without log.***


:error2
ECHO ***User/System does not have adequate access to the directory being copied to, attempting to go to fall back directory***
goto:resolution2


:error3
ECHO ***user/system still cannot move file to remote fall back directory. Program will exit after attempt to copy log to local directory***
>c:\failuretocopy.log


:error4
ECHO ***error is unknown, program will exit after attempt to copy log to local directory***
>c:\failuretoidentifyproblem.log

:end
ECHO ***move was completed, program will exit normally.***



See More: Please review for me (batch file)

Report •


#1
September 16, 2006 at 09:12:56

Also to note, the program runs all the way through without looping, but it is not providing the correct response into those if == parts so that it will go to the end as if nothing failed.

Report •

#2
September 17, 2006 at 01:00:48

In XP, M$ included a GOTO :EOF which goes to the end of the script when executed no matter what. Try using a GOTO EOF statement instead of :END

"Computer security." — Oxymoron


Report •

#3
September 17, 2006 at 02:53:55

if exist \\fl01_admin\home\logs\%computername%chkdsk.log del \\fl01_admin\home\logs\%computername%chkdsk.log
::==
move c:\chkdsk.log \\fl01_admin\home\logs\%computername%chkdsk.log

##################
Those are 2 lines separated by ::==


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

M2



Report •

Related Solutions

#4
September 17, 2006 at 03:01:15

Well... not so fast, M2.

if exist \\fl01_admin\home\logs\%computername%chkdsk.log del \\fl01_admin\home\logs\%computername%chkdsk.log
::==
if exist c:\chkdsk.log move c:\chkdsk.log \\fl01_admin\home\logs\%computername%chkdsk.log


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

M2



Report •

#5
September 17, 2006 at 03:25:48

Whats up M2! I think it' about time for Tony to go to bed. It's 5:20 AM here.

I got a quick question first about the FOR command if you got time to explain it to me.

I was looking at some of your old posts about TXT file manipulation using the delims and tokens options of the FOR command. Could kindly please explain in detail how to use those options? I have tried using for /?, but the examples aren't very clear to me. Maybe I'm just mental or something, but I don't think so since I have used your other examples to teach myself NT Shell scripting, and I think I understand everything except for the FOR commands options. The only problem I am having is the FOR commands options as listed above.

Thanks,

Tony


Report •

#6
September 17, 2006 at 03:42:34

Hi Tony,

It's 17:30 here; you must be in Central time zome.

IVO is the expert, but here goes.

Let's say we have a one line file that contains:

the quick brown fox

The 'normal' delims are SPACE [and TAB?] so if we:

for "tokens=*"

we get:

the quick brown fox

OOPS You better get some sleep and I better study up. Do you Skype?


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

M2



Report •

#7
September 17, 2006 at 03:45:59

I'm not tired, lol. No, sorry I don't use VoIP. What about MSN?

Report •

#8
September 17, 2006 at 04:00:42

Nah. Anyway

Try these3 lines for tokens:

for /f "tokens=*" %%T in (fox.txt) do echo %%T
for /f "tokens=2-3" %%T in (fox.txt) do echo %%T %%U

##############
As I was looking through the for /? the area below tokens talks about back quoted string.

W*T*F is a back quote? Maybe that's something you only get with a $100 KBD.


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

M2



Report •

#9
September 17, 2006 at 04:06:56

Thanks, but I my brain is too wore out for the day to learn anything more. I'll have a look in the morning and post back.

Thanks,

Tony


Report •

#10
September 17, 2006 at 16:10:23

The back quote (I call it a back tick) is lower case, just to the left of the number 1 (upper case is a tilde).

It is used (even in Win CMD files) for what Unix folks would call 'command substitution'.



Report •

#11
September 17, 2006 at 20:34:01

Thanks, Guy


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

M2



Report •

#12
September 19, 2006 at 02:46:15

Guy: (Or M2)
I didnt quite get that thing with command substitution.. Can some of you give me a example of its use?

Report •

#13
September 19, 2006 at 15:17:32

Well, I had to kick my wife off her Win box for a minute to look, but .....

If you ask for help for the for command:

help for

You see several pages worth of 'stuff'. Part of that says about 'usebackq' that it 'specifies that new semantics are in place .....'.

Read that and understand what the 'new semantics' are.


Later it says that 'usebackq' is used with 'for /F' to parse the output of a command.

It does give one trivial/nonsensical command line example:

for /F "usebackq delims==" %i in (`set`) DO @echo %i

Bottom line is it allows you to execute any command, capture and parse the output in to script variables, and do something based on that.

Once you get your head around it, it is a very powerful concept.

Guy



Report •

#14
September 20, 2006 at 06:44:11

Guy: Thnx. I'll have a look into it.

Report •


Ask Question