Windows 我如何验证文件是否存在,以及它是否';从今天起呢?

Windows 我如何验证文件是否存在,以及它是否';从今天起呢?,windows,batch-file,cmd,scripting,copy,Windows,Batch File,Cmd,Scripting,Copy,我有这个批处理文件,在应用副本提交之前,如何确保源文件C:\file.zip存在并且它是今天的文件?以下内容在我尝试时不起作用: echo off cls echo will do a back if [ C:\file.zip ] then echo found, will validate if its the file created today? copy C:\file.zip "\\to_computer_of_enterprise\\granted\\to\\upl

我有这个批处理文件,在应用副本提交之前,如何确保源文件
C:\file.zip
存在并且它是今天的文件?以下内容在我尝试时不起作用:

echo off
cls

echo will do a back

if [ C:\file.zip ]
then

  echo found, will validate if its the file created today?

  copy C:\file.zip "\\to_computer_of_enterprise\\granted\\to\\upload\\here"

else:
  echo sorry do not exist

fi

这是一个批处理代码,不使用命令
处理文件
,默认情况下,该批处理代码在WindowsXP或以前版本的Windows上不可用,仅在Windows Vista和更高版本的Windows上可用,这绝对是执行此任务的最佳选择

@echo off
set "FileName=C:\file.zip"

if not exist "%FileName%" goto FileNotExist

rem Get last modification date of the file.
for %%I in ("%FileName%") do set "FileDate=%%~tI"

rem Compare the first 10 characters from file date string with the last
rem 10 characters from current local date hold in environment variable DATE.
if not "%FileDate:~0,10%" == "%DATE:~-10%" goto FileNotToday

copy "%FileName%" "\\to_computer_of_enterprise\granted\to\upload\here"
goto :EndFileCheck

:FileNotToday
echo The file %FileName% is not from today.
goto :EndFileCheck

:FileNotExist
echo The file %FileName% does not exist.

:EndFileCheck
set "FileDate="
set "FileName="
上述批处理代码中环境变量
date
%%~tI
引用的文件日期的日期字符串格式取决于Windows区域和语言设置。因此,如果将文件日期与当前本地日期进行比较,可能需要稍微修改代码并在条件中使用其他字符索引

在命令提示符窗口中运行以下命令行,以查看计算机上本地日期和文件日期的日期格式:

@cls & @echo Local date: %DATE% & @for %I in ("C:\file.zip") do @echo  File date: %~tI
在我的计算机上输出:

Local date: 19.08.2016
 File date: 19.08.2016 10:53
因此,环境变量
DATE
(本地日期)的最后10个字符与环境变量
FileDate
(文件日期)的前10个字符相比,实际上比较了两个日期字符串

独立于区域和语言设置的解决方案是:

@echo off
setlocal EnableExtensions
set "FileName=C:\file.zip"

if not exist "%FileName%" goto FileNotExist

rem The command wmic requires the name of the file with full path and
rem each backslash in path must be escaped with one more backslash.
set "EscapedFileName=%FileName:\=\\%"

rem Get last modification date of the file in region independent format.
for /F "usebackq skip=1 tokens=1 delims=." %%T in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='%EscapedFileName%'" get lastmodified`) do set "FileDate=%%T" & goto GetLocalDate

rem Get current local date in region independent format.
:GetLocalDate
for /F "tokens=2 delims==." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS get localdatetime /VALUE') do set "LocalDate=%%T"

rem Compare the first 8 characters from file date string with the first
rem 8 characters from current local date string. The format is for both
rem date strings YYYYMMDD... independent on region and language settings.
if not "%FileDate:~0,8%" == "%LocalDate:~0,8%" goto FileNotToday

copy "%FileName%" "\\to_computer_of_enterprise\granted\to\upload\here"
goto :EndFileCheck

:FileNotToday
echo The file %FileName% is not from today.
goto :EndFileCheck

:FileNotExist
echo The file %FileName% does not exist.

:EndFileCheck
endlocal
对于此解决方案,必须始终使用完整路径指定文件名

这个解决方案比第一个慢得多,因为调用两次(Windows Management Instrumentation命令行实用程序)需要一秒钟以上的时间

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • cls/?
  • 复制/?
  • echo/?
  • endlocal/?
  • 获取/?
  • goto/?
  • 如果/?
  • rem/?
  • 设置/?
  • setlocal/?
注意:文件存在性检查不会检查现有文件系统条目是否确实是文件或文件夹。

这可能会有所帮助

forfiles/d+0/p C:\file.zip/C“cmd/C copy@path[path\u to\u copy]”

/d+0表示修改日期为今天的文件


有关详细信息,请执行
forfiles/?

快速堆栈溢出搜索揭示:先生,如何验证文件是否存在及其当前文件?我知道我可以使用
if exist”“()
语法,但您如何验证文件是今天生成的?请@YumYumYum尝试将拼图放在一起。试试;请注意
/MAXAGE:n
/MINAGE:n
参数。
forfiles
使用
/D+0
选项返回今天或以后修改的文件(和文件夹);假设您没有时间机器,这可以为您工作,与
if exist
一起检查文件(和文件夹)是否存在。。。