MSFN Forum: IF ELSE commands in DOS for XP.... - MSFN Forum

Jump to content


Unattended CD/DVD Guide Homepage · MSFN Forum Rules

If you have questions about customizing Windows XP that are nLite-specific, please post them in the nLite forum, not here. If you have questions regarding the unattended installation of Windows XP, please post them in the Unattended Windows 2000/XP/2003 section.
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

IF ELSE commands in DOS for XP.... Rate Topic: -----

#1 User is offline   ceez 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 581
  • Joined: 06-September 03

  Posted 25 February 2006 - 12:42 PM

Hello fellow msfn'ers...

I am trying to create a batch file with the IF / ELSE command.

ie: I want it to look for a file on c:\test.txt <b>but</b> if the file already exist, I dont want to perform the action of copying the test.txt file from a remote location to c:\

This is the actual steps I am trying:


REM *** DELETING LINKS FROM START MENU ****
IF EXIST "C:\Documents and Settings\All Users\Start Menu\Add A Printer.url" DEL "C:\Documents and Settings\All Users\Start Menu\Add A Printer.url"

IF EXIST "C:\Documents and Settings\All Users\Start Menu\IT Home Page.url" DEL "C:\Documents and Settings\All Users\Start Menu\IT Home Page.url"

IF EXIST "C:\Documents and Settings\All Users\Start Menu\Open A Ticket.url" DEL "C:\Documents and Settings\All Users\Start Menu\Open A Ticket.url"

REM *** CREATE 'IT LINKS' FOLDER ****

IF EXIST "C:\Documents and Settings\All Users\Start Menu\IT Links" ELSE EXIT my prob is here, if that folder 'IT LINKS' exist then do not continue the batch, just exit OR do the following 2 lines of creating the folder and copying the links inside the folder.

md "C:\Documents and Settings\All Users\Start Menu\IT Links"


copy "\\bamdc001\sysvol\BAGLOBAL.NET\IT Links\*.*" "C:\Documents and Settings\All Users\Start Menu\IT Links\*.*"


thanks for the help

ceez

This post has been edited by ceez: 25 February 2006 - 12:43 PM



#2 User is offline   Bâshrat the Sneaky 

  • aka Wim Leers
  • PipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 2,214
  • Joined: 29-October 03
  • OS:none specified
  • Country: Country Flag

Posted 25 February 2006 - 12:45 PM

Your question is not very clear, but I guess that your problem is that you aren't using brackets to execute multiple commands if a false value is returned by the 'if exist' function.

You should do it like this:
IF EXIST "C:\blabla" (
RD "C:\blabla"
) ELSE (
MD "C:\blabla_reloaded"
copy "X:\thisfile" "C:\blabla_reloaded"
)


I hope this answered your question.

#3 User is offline   Sonic 

  • Sonic
  • Group: Patrons
  • Posts: 1,603
  • Joined: 04-December 03

Posted 25 February 2006 - 04:05 PM

I think Bâshrat the Sneaky 's guide is the best answer.

#4 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,351
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 25 February 2006 - 04:20 PM

Or the VBS Script Way
I have used what information you have posted, I have not tested this.

Quote

Dim Act, Fso, File, All_User_StartMenu
	Set Fso = CreateObject("Scripting.FileSystemObject")
	Set Act = CreateObject("Wscript.Shell")
	 All_User_StartMenu = Act.SpecialFolders(StartMenu)
	  '''' ARRAY FOR FILE NAMES 
	  File = Array(All_User_StartMenu &  "\IT Home Page.url", All_User_StartMenu &  "\Open A Ticket.url")
	For Each strF In File '''' LOOP THAT USES ALL THE ARRAY INFORMATION 
	 If Fso.FileExists(strF) Then Fso.DeleteFile(strF) End If 
	Next 
	 If Not Fso.FolderExists(All_User_StartMenu & "\IT Links") Then '''' START A CHECK FOR THE FOLDER THEN COPY
	  Fso.CreateFolder(All_User_StartMenu & "\IT Links")
	  Fso.CopyFile("\\bamdc001\sysvol\BAGLOBAL.NET\IT Links\*.*" ), (All_User_StartMenu & "\IT Links"),True 
	Else '''' IF THE FOLDER WAS THERE
	 Fso.CopyFile("\\bamdc001\sysvol\BAGLOBAL.NET\IT Links\*.*" ), (All_User_StartMenu & "\IT Links"),True 
	End If


#5 User is offline   ceez 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 581
  • Joined: 06-September 03

Posted 25 February 2006 - 08:08 PM

@Bâshrat the Sneaky, thkz for the tip. I am trying to put it to use but it doesnt work. It wont even get to the pause at the end of the batch, it just exists and doesnt even copy anything. It deletes the url's in the first three lines but the rest is ignored. This is what I am writing:

Quote

echo off

IF EXIST "C:\Documents and Settings\All Users\Start Menu\Add A Printer.url" DEL "C:\Documents and Settings\All Users\Start Menu\Add A Printer.url"


IF EXIST "C:\Documents and Settings\All Users\Start Menu\IT Home Page.url" DEL "C:\Documents and Settings\All Users\Start Menu\IT Home Page.url"


IF EXIST "C:\Documents and Settings\All Users\Start Menu\Open A Ticket.url" DEL "C:\Documents and Settings\All Users\Start Menu\Open A Ticket.url"


IF EXIST "C:\Documents and Settings\All Users\Start Menu\IT Links (
) ELSE (
MD "C:\Documents and Settings\All Users\Start Menu\IT Links"
)

COPY "C:\IT Links\*.*" "C:\Documents and Settings\All Users\Start Menu\IT Links\*.*"

pause
exit


@gunsmokingman, I'll give the VBScripting a try later... :) Seems so much more complicated, considering I dont know VB! :(

thkz again,

ceez
:thumbup

#6 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,363
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 26 February 2006 - 03:40 AM

Just change the line to:
IF NOT EXIST "%AllUsersProfile%\Start Menu\IT Links" EXIT


#7 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,351
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 26 February 2006 - 06:31 AM

Here is how the VBS Script I posted works

Quote

These are varible names
Dim Act, Fso, File, All_User_StartMenu

Quote

These are object that are using the varible names
All_User_StartMenu = Act.SpecialFolders(StartMenu) = Drive letter:\Documents and Settings\All Users\Start Menu
Set Fso = CreateObject("Scripting.FileSystemObject")
	Set Act = CreateObject("Wscript.Shell")
	 All_User_StartMenu = Act.SpecialFolders(StartMenu)

Quote

This is a Array that holds all the files names
'''' ARRAY FOR FILE NAMES 
	  File = Array(All_User_StartMenu &  "\IT Home Page.url", All_User_StartMenu &  "\Open A Ticket.url")

Quote

This is a Loop that uses the Array to perform the delete function, it will only do as many names are in the array, It is using If Exixts method
For Each strF In File '''' LOOP THAT USES ALL THE ARRAY INFORMATION 
	 If Fso.FileExists(strF) Then Fso.DeleteFile(strF) End If 
	Next

Quote

This is a If Not Statement, it say if it not there then do something
If Not Fso.FolderExists(All_User_StartMenu & "\IT Links") Then '''' START A CHECK FOR THE FOLDER THEN COPY
	  Fso.CreateFolder(All_User_StartMenu & "\IT Links")
	  Fso.CopyFile("\\bamdc001\sysvol\BAGLOBAL.NET\IT Links\*.*" ), (All_User_StartMenu & "\IT Links"),True 
	Else '''' IF THE FOLDER WAS THERE
	 Fso.CopyFile("\\bamdc001\sysvol\BAGLOBAL.NET\IT Links\*.*" ), (All_User_StartMenu & "\IT Links"),True 
	End If


#8 User is offline   Mr Snrub 

  • Former MSFT
  • Group: Super Moderator
  • Posts: 773
  • Joined: 14-September 04
  • OS:Windows 8 x64
  • Country: Country Flag

Posted 26 February 2006 - 06:46 AM

View PostYzöwl, on Feb 26 2006, 10:40 AM, said:

Just change the line to:
IF NOT EXIST "%AllUsersProfile%\Start Menu\IT Links" EXIT
That's the opposite of what he wants to achieve - he wants to exit the batch file early if the folder DOES exist.

I prefer to have a single exit point in a program or batch file, at the very end - so I use GOTOs to skip code based on IF statement evaluations:
DEL "%AllUsersProfile%\Start Menu\Add A Printer.url" >nul
DEL "%AllUsersProfile%\Start Menu\IT Home Page.url" >nul
DEL "%AllUsersProfile%\Start Menu\Open A Ticket.url" >nul

IF EXIST "%AllUsersProfile%\Start Menu\IT Links" GOTO SKIPCOPY
md "%AllUsersProfile%\Start Menu\IT Links"
xcopy "\\bamdc001\sysvol\BAGLOBAL.NET\IT Links\*.*" "%AllUsersProfile%\Start Menu\IT Links\"
:SKIPCOPY
I don't bother using IF EXIST statements for deleting individual files, as you incur a file operation anyway, and two if it does exist - piping the output to NUL means you don't have to care about the "file not found" message and you will always perform a single file operation.
(It also helps prevent extremely long command lines which reduce readability as they go off-screen or wrap.)

It is preferable to use the environment variables such as "%systemroot%", "%UserProfile%" and "AllUsersProfile%" rather than hard-coding paths too.

I also tend to prefer batch files for simple jobs like this in case there is a problem with the scripting engine or there is a 3rd party AV script proxy in the way which can foul things up.

This post has been edited by Mr Snrub: 26 February 2006 - 07:35 AM


#9 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,363
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 26 February 2006 - 07:27 AM

View PostMr Snrub, on Feb 26 2006, 12:46 PM, said:

That's the opposite of what he wants to achieve - he wants to exit the batch file early if the folder DOES exist.
Thanks for the wake up call...however, reading the requirements, all that is needed is to not perform two operations if a folder exists.
This can easily be achieved in a single line, with the if not exist statement I previously gave. There is no need in this case for an else statement:
IF NOT EXIST "%ALLUSERSPROFILE%\START MENU\IT LINKS" (XCOPY "\\bamdc001\SYSVOL\BAGLOBAL.NET\IT LINKS" "%ALLUSERSPROFILE%\START MENU\IT Links" /SIQHK >NUL)


#10 User is offline   ceez 

  • Senior Member
  • PipPipPipPip
  • Group: Members
  • Posts: 581
  • Joined: 06-September 03

Posted 26 February 2006 - 07:46 AM

****...you guys are amazing, it's like you do this with your eyes closed!

I think Mr Snrub got it, but I will definitely look into all of your options as a learning opportunity.

I really appreciate it,

ceez
:thumbup

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy