は、Windows Vistaで、Windowsの XPまたはWindowsのも、旧バージョンで使用可能なデフォルトしないことであるコマンドforfiles
を使用していないバッチコードおよびそれ以降のバージョンの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="
バッチコードに%%~tI
で参照環境変数DATE
とファイルの日付の日付文字列の形式は、上記のWindows地域と言語の設定に依存します。したがって、コードを若干適合させ、ファイル日付を現在のローカル日付と比較する条件の中の他の文字インデックスを使用する必要があるかもしれません。コマンドプロンプトウィンドウで
を実行し、コンピュータ上のローカルの日付とファイルの日付の日付形式を参照するには、次のコマンドライン:したがって
Local date: 19.08.2016
File date: 19.08.2016 10:53
:私のコンピュータで
@cls & @echo Local date: %DATE% & @for %I in ("C:\file.zip") do @echo File date: %~tI
が出力されます環境変数DATE
(ローカル日付)の最後の10文字が環境変数FileDate
(ファイル日付)の最初の10文字と比較され、実際には2つの日付文字列が比較されます。
地域と言語の設定に依存しないソリューションは、次のようになります。このソリューションでは
@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
ファイル名は、常にフルパスで指定する必要があります。
このソリューションは、wmic(Windows Management Instrumentationコマンドラインユーティリティ)を2回呼び出すのに1秒以上かかるため、最初の方法よりもはるかに時間がかかります。
使用されているコマンドとその動作方法を理解するには、コマンドプロンプトウィンドウを開き、次のコマンドを実行して、コマンドごとに表示されているすべてのヘルプページをすべてよく読んでください。
cls /?
copy /?
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
注:既存のファイルシステムのエントリが実際にファイルやフォルダの場合は、ファイルの存在チェックチェックしません。
[Solution](http://stackoverflow.com/questions/4340350/how-to-check-if-a-file-exists-from-inside-a-batch-file) –
サーファイルが存在するかどうか、そして今日のファイルを検証するにはどうすればよいですか?私は 'if exists '()'構文を使うことができますが、ファイルが今日作成されたことをどのように確認しますか? – YumYumYum
インスピレーションのために@stackoverflow.com/questions/9349815/comparing-a-modified-file-date-with-the-current-date-in-a-batch-file –