1

我必须在 Windows 中编写一些批处理/dos 脚本,将文件放入 UNIX 框中。但是路径和文件名在 Windows 中每年和每月都会分别发生变化。

假设 Windows 中路径 C:/2009MICS 的目录将保存全年的文件(12 个文件)。我的批次将每月运行,并且应该只选择相应月份的文件。例如,如果我的批次在 2 月 9 日运行。它应该从 2009MICS 文件夹中选择并传输 2 月的文件。

4

3 回答 3

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

会将 2009.txt 复制到目标路径。要包括月份,请使用

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

免责声明 - 我在德文版的 Win Vista 上进行了测试,希望它也适用于国际版。

于 2009-02-05T14:40:47.630 回答
1

一种方法是使用 GetDate.cmd(最后)将今天的日期检索到环境变量中。从中您可以将%mm%变量(当前月份)与文件日期戳的月份进行比较,如下所示:

  @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
        )

     )

结果:

  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

如果日期戳不可靠/未使用,您可以使用类似的令牌结构来解析文件名中的日期。

  ::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%

进一步阅读:

http://ss64.com/nt/syntax-getdate.html - 将日期返回到环境变量中,独立于区域日期设置 http://ss64.com/nt/syntax-datemath.html - 从任何日期添加或减去天数 http ://ss64.com/nt/syntax-delolder.html - 从单个文件夹中删除超过 N 天的文件

于 2009-08-28T19:02:49.257 回答
0

我看到两种不同的方式来解释你在问什么..

日期是由您运行脚本的日期驱动的,因此实际上是使用系统日期,还是由要复制的文件的文件日期驱动?

菲尔有一个很好的观点,但是如果您未能在给定日期激活脚本并尝试稍后再做,例如 3 月 1 日,将约会过程锁定到当前日期将会使您的排序陷入困境。

对于文件部分,我会做类似的事情:

@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

我希望我的问题是正确的,否则这是一个开始:)

于 2009-08-20T12:56:42.403 回答