Jump to content

batch programming


Recommended Posts


The "usual" way to check for the existence of a directory (no matter if it has a dot in the name) is to check for the nul device inside it. Under NTFS or Novell it is more correct to check for nul.ext, i.e.:

if exist corb.dir\nul ECHO YES

or (better)

if exist corb.dir\nul.ext ECHO YES

Reference here:

http://www.msfn.org/board/Batch_Scripts_Im..._Ap_t98853.html

jaclaz

Link to comment
Share on other sites

C:\>if exist c:\program files\nul (@echo/folder found) else (@echo/folder not found)

C:\>if exist "c:\program files\nul" (@echo/folder found) else (@echo/folder not found)
folder not found

C:\>if exist c:\program files (@echo/folder found) else (@echo/folder not found)

C:\>if exist "c:\program files" (@echo/folder found) else (@echo/folder not found)
folder found

C:\>if exist c:\windows\nul (@echo/folder found) else (@echo/folder not found)
folder found

C:\>if exist "c:\windows\nul" (@echo/folder found) else (@echo/folder not found)
folder not found

C:\>if exist c:\windows (@echo/folder found) else (@echo/folder not found)
folder found

C:\>if exist "c:\windows" (@echo/folder found) else (@echo/folder not found)
folder found

If you were to give us an example and show us the actual location of the unrecognised directory, then a more fitting reply could be formulated.

Link to comment
Share on other sites

Here is the way to do it using a VBS script.

I tested this using a folder called New.Folder

Save As CheckFolder.vbs

 Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'/-> The Folder To Check For Place Folder Path Here
Dim Folder :Folder = "New.Folder"

If Fso.FolderExists(Folder) Then
WScript.Echo "Confirm " & Folder
Act.Run(Chr(34) & Folder & Chr(34)),1,False
Else
WScript.Echo "Missing " & Folder
End If

Link to comment
Share on other sites

thanks for the replies guys but Im still having problems :(

I get "goto was unexpected at this time"

the purpose of this batch file is to create a command that will "safe" delete the files by moving them to c:/corb.dir/

@echo off
rem ========================================================================
rem sdel.cmd
rem date : 23 octobre 2007
rem but: Automatiser des tâches dans un environnement Windows
rem ========================================================================
if not %2=="" goto langue
if %1=="" goto langue
if %1=="/?" goto langue
if not exist %1 goto erreur
if not exist c:\corb.dir\nul mkdir c:\corb.dir\ goto deplacer
if exist c:\corb.dir\%~n1 choice /c:oyn /t:n,05 Écraser le fichier SpecFICH (O)ui ,(N)on?: Overwrite file FileSPEC (Y)es or (N)o
if errorlevel 3 goto fin
if errorlevel 2 goto deplacer
if errorlevel 1 goto deplacer
:deplacer
move %~n1 c:\corb.dir\
:langue
choice /c:fe Quel langue préférez-vous ? / What language do you prefer (F)rançais ou / or (E)nglish?:
if errorlevel 2 goto anglais
if errorlevel 1 goto francais

:francais
echo SYNTAXE : sdel [SPECFICH][/?]
echo SPECFICH : Représente une spécification de fichier selon la syntaxe
echo echosuivante : [lecteur :][chemin]nomfich[.ext]
echo Exemple 1 : sdel /? (affiche cet écran d’aide)
echo Exemple 2 : sdel toto (Déplace le fichier toto dans c:\corb.dir)
pause
:anglais
echo SYNTAX : sdel [FILESpec][/?]
echo FILESpec : Represents a file specification using the following syntax:
echo [drive :][path]filename[.ext]
echo Example 1 : sdel /? (Shows this help screen)
echo Example 2 : sdel toto (Moves toto to the recycle bin c:\corb.dir)
pause
:erreur
echo Fichier SpecFICH introuvable – File FileSPEC not found
pause
:fin

Edited by ronmanp
quote tags replaced by codebox tags
Link to comment
Share on other sites

As I've already suggested without response, I need to know what OS this script will be running! Based on the use of 'choice' I would have thought Windows 9x.

In quick respose to your direct question however, change

if not exist c:\corb.dir\nul mkdir c:\corb.dir\ goto deplacer

to

if not exist c:\corb.dir\nul (

mkdir c:\corb.dir

goto deplacer)

Link to comment
Share on other sites

A small "side" suggestion. :)

When using the GOTO or CALL statement, it is advisable to follow them with complete name of the label (including the ":" ) this way it is easier to find them at a glance.

As Yzöwl pointed out, this is wrong

if not exist c:\corb.dir\nul mkdir c:\corb.dir\ goto deplacer

you cannot put two statements after an IF without grouping the commands with parenthesis or using the "&" sign:

if not exist c:\corb.dir\nul mkdir c:\corb.dir\&goto :deplacer

should work as well

jaclaz

Link to comment
Share on other sites

I'm glad you got it working, however the batch file is not very well written, and will not work as you expect.

For example, when you've ran the command under your deplacer label, you will be asked for your language choice. Similar behaviour will occur after most of your label sections have ran.

Link to comment
Share on other sites

I think I fixed it by moving my deplacer label to the end ..

here my final version (sent to the teacher)

rem sdel.cmd
rem par : My name
rem date : 23 octobre 2007
rem but: Automatiser des tâches dans un environnement Windows
rem ========================================================================
if NOT "%2" == "" GOTO langue
if "%1" == "" GOTO langue
if "%1" == "/?" GOTO langue
if NOT EXIST "%1" GOTO erreur
if EXIST c:\corb.dir\%~n1 choice /c:oyn /t:n,05 Écraser le fichier SpecFICH (O)ui ,(N)on?: Overwrite file FileSPEC (Y)es or (N)o
if errorlevel == 3 goto fin
if errorlevel == 2 goto deplacer
if errorlevel == 1 goto deplacer
if NOT EXIST c:\corb.dir\nul mkdir c:\corb.dir\
GOTO deplacer
:langue
echo Quel langue préférez-vous? / What language do you prefer (F)rançais ou / or (E)nglish?:
choice /c:fe
if errorlevel == 2 GOTO anglais
if errorlevel == 1 GOTO francais
:francais
echo SYNTAXE : sdel [SPECFICH][/?]
echo SPECFICH : Représente une spécification de fichier selon la syntaxe
echo suivante : [lecteur :][chemin]nomfich[.ext]
echo Exemple 1 : sdel /? (affiche cet écran d’aide)
echo Exemple 2 : sdel toto (Déplace le fichier toto dans c:\corb.dir)
echo Appuyez sur une touche pour quitter….
pause>nul
goto fin
:anglais
echo SYNTAX : sdel [FILESpec][/?]
echo FILESpec : Represents a file specification using the following syntax:
echo [drive :][path]filename[.ext]
echo Example 1 : sdel /? (Shows this help screen)
echo Example 2 : sdel toto (Moves toto to the recycle bin c:\corb.dir)
echo Hit any key to quit…
pause>nul
goto fin
:erreur
echo Fichier SpecFICH introuvable – File FileSPEC not found
goto fin
:deplacer
move /Y "%1" c:\corb.dir\%~n1
:fin

Edited by ronmanp
Link to comment
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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...