ISBTECH
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked like a charm Thanks!
You're welcome!
Glad I could help.
Glad I could help.
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.
@Rene,
Just be aware, not all versions of Windows have WMIC on them...
~bp
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:
@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%"
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.
-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
)
@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).
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
~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
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%"
~bp
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:
Open in new window