1

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
DavidPostill
  • 7,120
  • 9
  • 35
  • 51
  • 1
    Quick stackoverflow search reveals: [Solution](http://stackoverflow.com/questions/4340350/how-to-check-if-a-file-exists-from-inside-a-batch-file) – Arthur Swails Aug 18 '16 at 20:15
  • Sir, how do i validate if the file exist and its todays file? i know i can use `if exist "" () ` syntax but how do you verify the the file was made today please? –  Aug 18 '16 at 20:17
  • for inspiration http://stackoverflow.com/questions/9349815/comparing-a-modified-file-date-with-the-current-date-in-a-batch-file – maytham-ɯɐɥʇʎɐɯ Aug 18 '16 at 20:17
  • 1
    @YumYumYum try to put the puzzles together. – maytham-ɯɐɥʇʎɐɯ Aug 18 '16 at 20:19
  • 1
    Try [robocopy](http://ss64.com/nt/robocopy.html); pay your attention to `/MAXAGE:n` and `/MINAGE:n` parameters. – JosefZ Aug 18 '16 at 20:20
  • 3
    `forfiles` with the `/D +0` option returns files (and folders) modified today or later; supposing you do not own a time machine, this could work for you, together with `if exist` to check file (and folder) existence... – aschipfl Aug 18 '16 at 22:07

2 Answers2

2

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.

Mofi
  • 38,783
  • 14
  • 62
  • 115
1

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 /?

MikeRodsky
  • 11
  • 2