Cómo verificar si un archivo existe en un DOS (Símbolo del sistema de Windows).¿BAT file?


Tengo que crear un archivo .BAT que haga esto:

  1. Si C:\myprogram\sync\data.handler existe, salir;
  2. Si C:\myprogram\html\data.sql no existe, salir;
  3. En C:\myprogram\sync\ elimine todos los archivos y carpetas excepto (test, test3 and test2)
  4. Copiar C:\myprogram\html\data.sql a C:\myprogram\sync\
  5. Llama a otro archivo por lotes con la opción sync.bat myprogram.ini.

Si estaba en el entorno Bash fue fácil para mí, pero no sé cómo probar si existe un archivo o carpeta y si es un archivo o carpeta.

Author: Jagger, 2010-06-11

3 answers

Puede usar SI EXISTE para buscar un archivo:

IF EXIST "filename" (
  REM Do one thing
) ELSE (
  REM Do another thing
)

Aquí hay un ejemplo de trabajo de búsqueda de un archivo o una carpeta:

REM setup

echo "some text" > filename
mkdir "foldername"

REM finds file    

IF EXIST "filename" (
  ECHO file filename exists
) ELSE (
  ECHO file filename does not exist
)

REM does not find file

IF EXIST "filename2.txt" (
  ECHO file filename2.txt exists
) ELSE (
  ECHO file filename2.txt does not exist
)

REM folders must have a trailing backslash    

REM finds folder

IF EXIST "foldername\" (
  ECHO folder foldername exists
) ELSE (
  ECHO folder foldername does not exist
)

REM does not find folder

IF EXIST "filename\" (
  ECHO folder filename exists
) ELSE (
  ECHO folder filename does not exist
)
 231
Author: stuartd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-18 14:08:24

Escriba IF/? para obtener ayuda sobre if, explica claramente cómo usar IF EXIST.

Para eliminar un árbol completo excepto algunas carpetas, consulte la respuesta de esta pregunta: Windows batch script para eliminar todo en una carpeta excepto una

Finalmente copiar solo significa llamar a COPIAR y llamar a otro archivo bat se puede hacer así:

MYOTHERBATFILE.BAT sync.bat myprogram.ini
 11
Author: Patrick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:55:05

Aquí hay un buen ejemplo de cómo hacer un comando si un archivo existe o no:

if exist C:\myprogram\sync\data.handler echo Now Exiting && Exit
if not exist C:\myprogram\html\data.sql Exit

Tomaremos esos tres archivos y los pondremos en un lugar temporal. Después de eliminar la carpeta, restaurará esos tres archivos.

xcopy "test" "C:\temp"
xcopy "test2" "C:\temp"
del C:\myprogram\sync\
xcopy "C:\temp" "test"
xcopy "C:\temp" "test2"
del "c:\temp"

Utilice el comando XCOPY :

xcopy "C:\myprogram\html\data.sql"  /c /d /h /e /i /y  "C:\myprogram\sync\"

Voy a explicar lo que el /c /d /h /e /i /y significa:

  /C           Continues copying even if errors occur.
  /D:m-d-y     Copies files changed on or after the specified date.
               If no date is given, copies only those files whose
               source time is newer than the destination time.
  /H           Copies hidden and system files also.
  /E           Copies directories and subdirectories, including empty ones.
               Same as /S /E. May be used to modify /T.
  /T           Creates directory structure, but does not copy files. Does not
               include empty directories or subdirectories. /T /E includes
  /I           If destination does not exist and copying more than one file,
               assumes that destination must be a directory.
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.

`To see all the commands type`xcopy /? in cmd

Llama a otro archivo por lotes con la opción sync.bate myprogram.ini.

No estoy seguro de lo que quiere decir con esto, pero si solo ¿quieres abrir estos dos archivos que acaba de poner la ruta del archivo como

Path/sync.bat
Path/myprogram.ini

Si fue en el entorno de Bash fue fácil para mí, pero no lo hago saber cómo comprobar si un archivo o carpeta existe y si es un archivo o carpeta.

Está utilizando un archivo por lotes. Usted mencionó anteriormente que tiene que crear un.archivo bat para usar esto:

Tengo que crear un .Archivo BAT que hace esto:

 10
Author: Avrumi Sherman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-29 03:16:00