如何从Windows批处理文件的递归副本中排除特定目录?

如何从Windows批处理文件的递归副本中排除特定目录?,windows,batch-file,Windows,Batch File,我正在编写一个shell脚本,将当前目录的内容备份到一个带有时间戳的子目录,/STATES/$DATE。显然,我不希望每次备份文件夹时都重新复制目录本身,因此我需要以某种方式将其从副本中排除 下面是一个未经测试的shell脚本,展示了我如何在*nix上实现这一点: ID="$(date +%Y%b%d%H%M%S)" COMMITABLE="$(ls | egrep --invert-match ^STATES\$)" STATE_PATH="$(pwd)/STATES/$ID" mkdir -

我正在编写一个shell脚本,将当前目录的内容备份到一个带有时间戳的子目录,
/STATES/$DATE
。显然,我不希望每次备份文件夹时都重新复制目录本身,因此我需要以某种方式将其从副本中排除

下面是一个未经测试的shell脚本,展示了我如何在*nix上实现这一点:

ID="$(date +%Y%b%d%H%M%S)"
COMMITABLE="$(ls | egrep --invert-match ^STATES\$)"
STATE_PATH="$(pwd)/STATES/$ID"
mkdir --parents "$STATE_PATH"
cp $COMMITABLE "$STATE_PATH"
ln -s "$STATE_PATH" PARENT

如何在批处理文件中实现这一点?

给你。这来自我自己的脚本:

set now=%date:~-4%-%date:~-10,2%-%date:~-7,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%

rem Before 10:00 o'clock a space is used, this makes it a zero.
set now=%now: =0%

xcopy . copydir-%now% /i
警告一句:这使用美国日期格式。要匹配不同的格式,您必须更改它。在我与荷兰和美国系统合作时,我个人使用以下代码:

rem US date versus Dutch: test 5th char is /
if "%date:~-5,1%"=="/" (
   rem Date format is mm/dd/yyyy
   set now=%date:~-4%-%date:~-10,2%-%date:~-7,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
) else (
   rem Date format is dd-mm-yyyy
   set now=%date:~-4%-%date:~-7,2%-%date:~-10,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
)

给你。这来自我自己的脚本:

set now=%date:~-4%-%date:~-10,2%-%date:~-7,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%

rem Before 10:00 o'clock a space is used, this makes it a zero.
set now=%now: =0%

xcopy . copydir-%now% /i
警告一句:这使用美国日期格式。要匹配不同的格式,您必须更改它。在我与荷兰和美国系统合作时,我个人使用以下代码:

rem US date versus Dutch: test 5th char is /
if "%date:~-5,1%"=="/" (
   rem Date format is mm/dd/yyyy
   set now=%date:~-4%-%date:~-10,2%-%date:~-7,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
) else (
   rem Date format is dd-mm-yyyy
   set now=%date:~-4%-%date:~-7,2%-%date:~-10,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
)

xcopy
/exclude:$FILE
选项可用于指定包含要忽略的文件名的文件。我的一位朋友将脚本转换为如下批处理:

@echo off
set thedate=%date:~0,2%-%date:~3,2%-%date:~6,4%
md %thedate%
echo STATES > excludefile.txt
echo excludefile.txt >> excludefile.txt
xcopy <root folder of directory structure> %thedate% /e /i /exclude:excludefile.txt
del excludefile.txt
@echo关闭
设置日期=%date:~0,2%-%date:~3,2%-%date:~6,4%
md%日期%
echo STATES>excludefile.txt
echo excludefile.txt>>excludefile.txt
xcopy%thedate%/e/i/exclude:excludefile.txt
del excludefile.txt

xcopy
/exclude:$FILE
选项可用于指定包含要忽略的文件名的文件。我的一位朋友将脚本转换为如下批处理:

@echo off
set thedate=%date:~0,2%-%date:~3,2%-%date:~6,4%
md %thedate%
echo STATES > excludefile.txt
echo excludefile.txt >> excludefile.txt
xcopy <root folder of directory structure> %thedate% /e /i /exclude:excludefile.txt
del excludefile.txt
@echo关闭
设置日期=%date:~0,2%-%date:~3,2%-%date:~6,4%
md%日期%
echo STATES>excludefile.txt
echo excludefile.txt>>excludefile.txt
xcopy%thedate%/e/i/exclude:excludefile.txt
del excludefile.txt

看起来您正在尝试运行自己的版本控制。你有没有考虑过改用mercurial这样的软件?这是为了一个除了shell脚本之外我什么都不能运行的系统。Hg很好,但不幸的是,这不是一个选择(嗯,git不是不久前才有一堆shell脚本吗?:-看起来你正在尝试推出自己的版本控制。你考虑过使用mercurial之类的工具吗?这是为了一个除了shell脚本我什么都不能运行的系统。Hg很好,但不幸的是,在这里它不是一个选择。)(嗯,git刚才不是只是一堆shell脚本吗?:-谢谢,但这不会导致将所有以前的copydir复制到新的中,导致目录大小膨胀吗?不会,因为我没有指定/s,所以不会复制子目录。我通常会复制子目录,但不会复制到c的子目录当前目录。如果需要,请使用xcopy/?了解/EXCLUDE选项。谢谢,但这不会导致将以前的所有copydir复制到新目录中,导致目录大小增大吗?不会,因为我没有指定/s,所以不会复制子目录。我通常会使用子目录进行复制,但不指向当前目录的子目录。如果您需要/EXCLUDE选项,请使用xcopy/?了解该选项。