배치 파일에 파일이 있는지 확인하는 방법은 무엇입니까?
이 작업을 수행하는 .BAT
파일 을 만들어야합니다 .
- 경우
C:\myprogram\sync\data.handler
, 출구 존재; - 경우
C:\myprogram\html\data.sql
수행, 종료 존재하지; - 년
C:\myprogram\sync\
을 제외한 모든 파일과 폴더를 삭제 (test
,test3
과test2
) - 복사
C:\myprogram\html\data.sql
에C:\myprogram\sync\
- 옵션으로 다른 배치 파일을 호출하십시오
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 사용 방법을 명확하게 설명합니다.
To delete a complete tree except some folders, see the answer of this question: Windows batch script to delete everything in a folder except one
Finally copying just means calling COPY and calling another bat file can be done like this:
MYOTHERBATFILE.BAT sync.bat myprogram.ini
Here is a good example on how to do a command if a file does or does not exist:
if exist C:\myprogram\sync\data.handler echo Now Exiting && Exit
if not exist C:\myprogram\html\data.sql Exit
We will take those three files and put it in a temporary place. After deleting the folder, it will restore those three files.
xcopy "test" "C:\temp"
xcopy "test2" "C:\temp"
del C:\myprogram\sync\
xcopy "C:\temp" "test"
xcopy "C:\temp" "test2"
del "c:\temp"
Use the XCOPY command:
xcopy "C:\myprogram\html\data.sql" /c /d /h /e /i /y "C:\myprogram\sync\"
I will explain what the /c /d /h /e /i /y
means:
/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
Call other batch file with option sync.bat myprogram.ini.
I am not sure what you mean by this, but if you just want to open both of these files you just put the path of the file like
Path/sync.bat
Path/myprogram.ini
If it was in the Bash environment it was easy for me, but I do not know how to test if a file or folder exists and if it is a file or folder.
You are using a batch file. You mentioned earlier you have to create a .bat file to use this:
I have to create a .BAT file that does this:
참고URL : https://stackoverflow.com/questions/3022176/how-to-verify-if-a-file-exists-in-a-batch-file
'Programming' 카테고리의 다른 글
BOM이없는 UTF-8 (0) | 2020.05.24 |
---|---|
C ++에서 열거 형을 선언 할 때 typedef를 사용하는 이유는 무엇입니까? (0) | 2020.05.24 |
간단히 말해서 스택 프레임의 개념을 설명하십시오 (0) | 2020.05.24 |
AssertjUnit의 문자열에 포함 (0) | 2020.05.24 |
Asp.Net Core에서 동일한 인터페이스의 여러 구현을 등록하는 방법은 무엇입니까? (0) | 2020.05.24 |