[windows] 배치 파일에 파일이 있는지 확인하는 방법은 무엇입니까?

이 작업을 수행하는 .BAT파일 을 만들어야합니다 .

  1. 경우 C:\myprogram\sync\data.handler, 출구 존재;
  2. 경우 C:\myprogram\html\data.sql수행, 종료 존재하지;
  3. C:\myprogram\sync\을 제외한 모든 파일과 폴더를 삭제 ( test, test3test2)
  4. 복사 C:\myprogram\html\data.sqlC:\myprogram\sync\
  5. 옵션으로 다른 배치 파일을 호출하십시오 sync.bat myprogram.ini.

Bash 환경에 있다면 나에게는 쉽지만 파일이나 폴더가 있는지 여부와 파일 또는 폴더인지 테스트하는 방법을 모르겠습니다.



답변

IF EXIST를 사용하여 파일을 확인할 수 있습니다.

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

“else”가 필요하지 않은 경우 다음과 같이 할 수 있습니다.

set __myVariable=
IF EXIST "C:\folder with space\myfile.txt" set __myVariable=C:\folder with space\myfile.txt
IF EXIST "C:\some other folder with space\myfile.txt" set __myVariable=C:\some other folder with space\myfile.txt
set __myVariable=

파일 또는 폴더를 검색하는 실제 예는 다음과 같습니다.

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
)


답변

IF /?를 입력하십시오. IF EXIST 사용 방법을 명확하게 설명합니다.

일부 폴더를 제외한 전체 트리를 삭제하려면이 질문에 대한 답변을 참조하십시오. 하나의 폴더를 제외한 모든 폴더를 삭제하는 Windows 배치 스크립트

마지막으로 복사한다는 것은 COPY를 호출하고 다음과 같이 다른 bat 파일을 호출하는 것을 의미합니다.

MYOTHERBATFILE.BAT sync.bat myprogram.ini


답변

다음은 파일이 존재하거나 존재하지 않는 경우 명령을 수행하는 방법에 대한 좋은 예입니다.

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

우리는이 3 개의 파일을 가져 와서 임시 장소에 둘 것입니다. 폴더를 삭제하면이 세 파일이 복원됩니다.

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

XCOPY 명령을 사용하십시오 .

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

/c /d /h /e /i /y의미가 무엇인지 설명하겠습니다 .

  /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

sync.bat myprogram.ini 옵션을 사용하여 다른 배치 파일을 호출하십시오.

나는 이것이 무엇을 의미하는지 잘 모르지만,이 두 파일을 모두 열고 싶다면 파일의 경로를 입력하십시오.

Path/sync.bat
Path/myprogram.ini

Bash 환경에 있다면 나에게는 쉽지만 파일이나 폴더가 있는지 여부와 파일 또는 폴더인지 테스트하는 방법을 모르겠습니다.

배치 파일을 사용하고 있습니다. 앞에서 언급했듯이 이것을 사용하려면 .bat 파일을 만들어야합니다.

이 작업을 수행하는 .BAT 파일을 만들어야합니다.


답변