views:

1934

answers:

3

I have to write some batch/dos script in windows which will put the files in UNIX box. But the path and filenames are getting changed every year and month respectively in windows.

Suppose a directory in windows at path C:/2009MICS which will hold the files for whole year( 12 files). My batch will run monthly and should pick the files for respective months only. e.g if my batch is running in Feb,09. It should pick and transfer the file for feb month from 2009MICS folder.

+1  A: 
copy "%date:~6%.txt" "\path\to\destination"

would copy 2009.txt to the destination path. To include the month, use

copy "%date:~3%.txt" "\path\to\destination"

Disclaimer - I tested that on a German edition of Win Vista, hope it works with the International editions as well.

Phil Reif
That has nothing to do with the language of the OS, more with the set locale. Date/time stuff in cmd is inherently brittle since you only get localized dates instead of culture-neutral ones. For me, using standard ISO 8601 dates (YYYY-MM-DD) it would break, for example. But I haven't found a better solution so far; you just need to be aware that it won't work with differing date formats.
Joey
A: 

I see two different ways to interpret what you are asking..

Is the date driven by the day you run the script, so actually using the system date, or driven by the file date of the files to be copied?

Phil has a good point, but locking the dating process to the current date will jinx your sort if you fail to activate the sctipt on a given date and try to do it later, like march 1st..

for the file part, i'd do something like:

@echo off
setlocal ENABLEDELAYEDEXPANSION
pushd C:\2009MICS
for /F "delims=" %%f in ('dir /b /a-d') do (
    for /f "tokens=2 delims=- " %%t in ("%%~tf") do set TimeStamp=%%t
    if not exist !TimeStamp!\ mkdir !TimeStamp!\
    copy %%f !TimeStamp!\ >nul
)
exit /b

I hope I got the question right, else that's a start right here :)

Jay
+1  A: 

One approach would be to retrieve todays date into environment vars with GetDate.cmd (at end). From that you could compare the %mm% variable (current month) with the month of the file's datestamp with something like this:

  @echo off
  :: see http://ss64.com/nt/syntax-args.html
  for %%f in (*.bat) do (
     echo.
     echo Parameter '~tf' reports '%%~tf' for '%%f'

     :: see http://ss64.com/nt/for_f.html
     echo.
     for /f "tokens=1-2" %%g in ("%%~tf") do (
        echo the file date %%g
        echo the file time %%h
        )

     for /f "tokens=1-3 delims=- " %%i in ("%%~tf") do (
        echo the year     %%i
        echo the month    %%j
        echo the day      %%k
        )

     )

Result:

  Parameter '~tf' reports '2009-08-28 11:52 AM' for 'test-date.bat'

  the file date 2009-08-28
  the file time 11:52
  the year     2009
  the month    08
  the day      28

You could use similar token structure to parse the date out of the filename if the datestamp is not reliable/used.

  ::GetDate.cmd - Source: http://ss64.com/nt/syntax-getdate.html :
  @echo off
  SETLOCAL
  FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (call :s_fixdate %%G %%H %%I %%J)
  goto :s_print_the_date

  :s_fixdate
  if "%1:~0,1%" GTR "9" shift
  FOR /f "skip=1 tokens=2-4 delims=(-)" %%G IN ('echo.^|date') DO (
      set %%G=%1&set %%H=%2&set %%I=%3)
  goto :eof

  :s_print_the_date
  echo Month:[%mm%]  Day:[%dd%]  Year:[%yy%]
  ENDLOCAL
  SET mm=%mm%&SET dd=%dd%&SET yy=%yy%

Further reading:

http://ss64.com/nt/syntax-getdate.html - return date into environment vars independent of regional date settings http://ss64.com/nt/syntax-datemath.html - Add or subtract days from any date http://ss64.com/nt/syntax-delolder.html - Delete files older than N days from a single folder

matt wilkie
Nice links at the bottom, thanks.
Ruben Bartelink