Reputation:
I have this BATCH file, How to make sure the source file C:\file.zip
exist and it's today's file, before applying the copy submission? following is not working when I tried:
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
Upvotes: 1
Views: 9729
Reputation: 11
This may be helpful
forfiles /d +0 /p C:\ file.zip /c "cmd /c copy @path [path_to_copy]"
the /d +0 means files with modified date of today
for more info do forfiles /?
Upvotes: 1
Reputation: 49126
Here is a batch code not using command forfiles
which is by default not available on Windows XP or even former versions of Windows, only on Windows Vista and later Windows versions which is definitely the best choice for this task.
@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="
The format of date string of environment variable DATE
and file date referenced with %%~tI
in batch code above depends on Windows Region and Language settings. So it might be necessary to slightly adapt the code and use other character indices in the IF condition comparing file date with current local date.
Run in a command prompt window the following command line to see the date format for local date and file date on your computer:
@cls & @echo Local date: %DATE% & @for %I in ("C:\file.zip") do @echo File date: %~tI
On my computer is output:
Local date: 19.08.2016
File date: 19.08.2016 10:53
Therefore the last 10 characters of environment variable DATE
(local date) compared with first 10 characters of environment variable FileDate
(file date) really compares the two date strings.
A solution being independent on Region and Language settings would be:
@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
For this solution the file name must be always specified with full path.
And this solution is much slower than the first one because calling twice wmic - the Windows Management Instrumentation command-line utility - takes more than a second.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cls /?
copy /?
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
Note: The file existence check does not check if the existing file system entry is really a file or a folder.
Upvotes: 2