Link to home
Start Free TrialLog in
Avatar of ISBTECH
ISBTECHFlag for United States of America

asked on

Simple batch file to move a file from a static location to a directory named by the system date

Good afternoon Experts,

I'm trying to write a batch file that will move a file called "ftp.log" from the path c:\commfiles\mers to an existing directory that gets created dailiy with the name = the current date for example 20111121.

From what I've read here this should work;

copy c:\commfiles\mers\ftp.log c:\commfiles\mers\%DATE%\ftp.log  but it typing that into the command prompt I get
"The syntax of the command is incorrect" However if I replace the \%DATE%\ with \20111121\ it works so it's something to do with the variable for the date... What am I doing wrong?
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

You should create the destination directory first and then move the file, like this:

mkdir c:\commfiles\mers\%DATE%
copy c:\commfiles\mers\ftp.log c:\commfiles\mers\%DATE%\ftp.log

Open in new window


Could be that your DATE variable is not set properly, what do you get when you do this on a command line:
echo %DATE%

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ReneGe
ReneGe
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ISBTECH

ASKER

That worked like a charm Thanks!
You're welcome!

Glad I could help.
Avatar of ISBTECH

ASKER

Ok spoke to soon, it worked once, then not again.  Even if I delete the file that was copied it will not copy again, any ideas?
What will happen if you add /y after copy?: COPY /Y
Also, add a pause at the end, to see it you get an error message.
Avatar of Bill Prew
Bill Prew

@Rene,

Just be aware, not all versions of Windows have WMIC on them...

~bp
@ReneGe - think your wmic command is giving (empty) output after the line with the date fields

@ISBTECH - try this:
@ECHO OFF
REM DEFINING THE SYSTEM DATE
FOR /F "tokens=1-3 delims= " %%A IN ('WMIC Path Win32_LocalTime Get Day^,Month^,Year ^| findstr /r [0123456789]')  DO SET DestPath=c:\commfiles\mers\%%C%%B%%A
IF NOT EXIST "%DestPath%" md "%DestPath%"
copy /y "c:\commfiles\mers\ftp.log" "%DestPath%"

Open in new window

Note: /y is not really needed but will overwrite destination
@billprew:
-You have a point!
-WMIC works well for me on XP Pro, W7, WServer 2008, WServer 2003, did not try it on 2000, ME 95 3.x, nor on non-elulated MS-DOS. I am now puzzled!!!

@gerwinjansen:
-Thanks for your comment.
-I scripted the batch file without testing it. I am learning a different way to use findstr. Limiting it's output to only the ones containing numbers? Nice!! I'll remember this one!!
-You deserve the points more than I do.

@ISBTECH:
-I suggest you use gerwinjansen's script as it makes more sense than mine.
-In order for me, to kind of deserve the points you gave me, here is a working version of my script.

@ECHO OFF
CALL :DefineDate
IF NOT EXIST "%DestPath%" md "%DestPath%"
COPY /Y "c:\commfiles\mers\ftp.log" "%DestPath%"
EXIT

:DefineDate
FOR /F "skip=1 tokens=1-3 delims= " %%A IN ('WMIC Path Win32_LocalTime Get Day^,Month^,Year') DO (
SET DestPath=c:\commfiles\mers\%%C%%B%%A\
exit /b
)

Open in new window

@Rene,

FWIW, here's my preferred approach to get the date elements via WMIC.

One more thin to mention, the Day and Month will NOT be left padded with a leading ZERO to two digits, so additional logic is needed if that is the desire (which I think it would be in this case).

@ECHO OFF
CALL :DefineDate
IF NOT EXIST "%DestPath%" md "%DestPath%"
COPY /Y "c:\commfiles\mers\ftp.log" "%DestPath%"
EXIT

:DefineDate
FOR /F "skip=2 tokens=2-4 delims=," %%A IN ('WMIC Path Win32_LocalTime Get Day^,Month^,Year /Format:csv') DO (
  SET DestPath=c:\commfiles\mers\%%C%%B%%A\
)
exit /b

Open in new window

~bp
For an example of the zero padding take a look at the solution accepted here:

https://www.experts-exchange.com/questions/26175337/How-do-I-use-a-batch-file-to-zip-up-the-contents-of-a-folder.html

~bp
So for the benefit of the original poster, since the solutions provided here will likely not work on 12/1/2011 when the day drops to a single digit, here's a version that should work for you.

@echo off
setlocal
 
REM First get the pieces of todays date in various formats needed
for /f "skip=2 tokens=2-4 delims=," %%A in ('WMIC Path Win32_LocalTime Get Day^,Month^,Year /Format:csv') do (
  REM values without leading zeros
  set "d=%%A"
  set "m=%%B"
  set "yyyy=%%C"
)
 
REM Build corresponding values with leading zeros
if %m% LSS 10 (set "mm=0%m%") else (set "mm=%m%")
if %d% LSS 10 (set "dd=0%d%") else (set "dd=%d%")

set DestPath=c:\commfiles\mers\%yyyy%%mm%%dd%\
if not exist "%DestPath%" md "%DestPath%"
copy /Y "c:\commfiles\mers\ftp.log" "%DestPath%"

Open in new window

~bp