• Announcements

    • xper

      MSFN Sponsorship and AdBlockers!   07/10/2016

      Dear members, MSFN is made available via subscriptions, donations and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, become a site sponsor and ads will be disabled automatically and by subscribing you get other sponsor benefits.
Sign in to follow this  
Followers 0
ceez

IF ELSE commands in DOS for XP....

10 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

@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

0

Share this post


Link to post
Share on other sites

Just change the line to:

IF NOT EXIST "%AllUsersProfile%\Start Menu\IT Links" EXIT

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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)

0

Share this post


Link to post
Share on other sites

****...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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.