February 25, 200620 yr 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 helpceez Edited February 25, 200620 yr by ceez
February 25, 200620 yr 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.
February 25, 200620 yr Or the VBS Script WayI 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
February 26, 200620 yr Author @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 offIF 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\*.*" pauseexit@gunsmokingman, I'll give the VBScripting a try later... Seems so much more complicated, considering I dont know VB! thkz again,ceez
February 26, 200620 yr Just change the line to:IF NOT EXIST "%AllUsersProfile%\Start Menu\IT Links" EXIT
February 26, 200620 yr Here is how the VBS Script I posted worksThese are varible namesDim Act, Fso, File, All_User_StartMenuThese are object that are using the varible namesAll_User_StartMenu = Act.SpecialFolders(StartMenu) = Drive letter:\Documents and Settings\All Users\Start MenuSet 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 NextThis is a If Not Statement, it say if it not there then do somethingIf 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
February 26, 200620 yr Just change the line to:IF NOT EXIST "%AllUsersProfile%\Start Menu\IT Links" EXITThat'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" >nulDEL "%AllUsersProfile%\Start Menu\IT Home Page.url" >nulDEL "%AllUsersProfile%\Start Menu\Open A Ticket.url" >nulIF EXIST "%AllUsersProfile%\Start Menu\IT Links" GOTO SKIPCOPYmd "%AllUsersProfile%\Start Menu\IT Links"xcopy "\\bamdc001\sysvol\BAGLOBAL.NET\IT Links\*.*" "%AllUsersProfile%\Start Menu\IT Links\":SKIPCOPYI 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 February 26, 200620 yr by Mr Snrub
February 26, 200620 yr 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)
February 26, 200620 yr Author ****...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
Create an account or sign in to comment