Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

IF ELSE commands in DOS for XP....

- - - - -

  • Please log in to reply
9 replies to this topic

#1
ceez

ceez

    Senior Member

  • Member
  • PipPipPipPip
  • 581 posts
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

Edited by ceez, 25 February 2006 - 12:43 PM.

ceez


How to remove advertisement from MSFN

#2
Bâshrat the Sneaky

Bâshrat the Sneaky

    aka Wim Leers

  • Member
  • PipPipPipPipPipPipPipPip
  • 2,214 posts
  • OS:none specified
  • Country: Country Flag
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
Sonic

Sonic

    Sonic

  • Patrons
  • 1,603 posts
I think Bâshrat the Sneaky 's guide is the best answer.

#4
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
Or the VBS Script Way
I have used what information you have posted, I have not tested this.

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




GunSmokingMan



#5
ceez

ceez

    Senior Member

  • Member
  • PipPipPipPip
  • 581 posts
@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:

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
ceez

#6
Yzöwl

Yzöwl

    Wise Owl

  • Member
  • PipPipPipPipPipPipPipPipPip
  • 4,532 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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


#7
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
Here is how the VBS Script I posted works

These are varible names

Dim Act, Fso, File, All_User_StartMenu

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)

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")

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

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




GunSmokingMan



#8
Mr Snrub

Mr Snrub

    Former MSFT

  • Super Moderator
  • 775 posts
  • OS:Windows 8 x64
  • Country: Country Flag

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.

Edited by Mr Snrub, 26 February 2006 - 07:35 AM.

My TechNet Blog
I have CDO. It's like OCD except the letters are in alphabetical order, as they should be.


#9
Yzöwl

Yzöwl

    Wise Owl

  • Member
  • PipPipPipPipPipPipPipPipPip
  • 4,532 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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
ceez

ceez

    Senior Member

  • Member
  • PipPipPipPip
  • 581 posts
****...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
ceez




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN