我必须创建一个
.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
REM "The ELSE clause must occur on the same line
as the command after the IF"
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 exist C:\myprogram\sync\data.handler echo Now Exiting && Exit
if not exist C:\myprogram\html\data.sql Exit
我们将把这三个文件放在一个临时位置。删除文件夹后,它将恢复这三个文件。
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 文件来执行此操作:
输入 IF /?要获得有关 if 的帮助,它清楚地解释了如何使用 IF EXIST。
要删除除某些文件夹之外的完整树,请参阅此问题的答案:Windows批处理脚本删除文件夹中除一个之外的所有内容
最后复制只是意味着调用 COPY 并调用另一个bat文件可以这样完成:
MYOTHERBATFILE.BAT sync.bat myprogram.ini