DOS脚本根据日期动态获取文件名和路径

发布于 2024-07-13 23:31:11 字数 240 浏览 11 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心如狂蝶 2024-07-20 23:31:11
copy "%date:~6%.txt" "\path\to\destination"

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

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

免责声明 - 我在德语版 Win Vista 上测试了这一点,希望它也适用于国际版本。

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.

删除会话 2024-07-20 23:31:11

一种方法是使用 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 天的内容

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

水中月 2024-07-20 23:31:11

我看到两种不同的方式来解释你所问的问题。

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

Phil 有一个很好的观点,但是如果您未能在给定日期激活 sctipt 并尝试稍后执行(例如 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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文