将文件夹中的文件作为.bat文件移动和重命名,并在开始处添加修改日期 [英] Move and rename files from folder to folder as a .bat file and adding modified date at beginning

查看:559
本文介绍了将文件夹中的文件作为.bat文件移动和重命名,并在开始处添加修改日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间试图解决这个问题,但没有运气.

I have spent many hours trying to work this out and no luck.

场景: 文件夹中有多个不同的文件名,但具有相同的扩展名.在Windows Server 2008 64位上. 想要使用计划批处理作业将它们移动到另一个文件夹. .蝙蝠.在移动期间,我想重命名它们以包括其上次修改日期.我正在尝试使用以下格式:

Scenario: Have multiple different file names, but same extension in a folder. On Windows server 2008 64bit. Want to move them to another folder using a schedule batch job. .bat. During the move I want to rename them to include their last modified date. I am trying to put into this format:

yyyymmddhhmmss-name.zip

我看过PowerShell和for命令,但无法解决.

I have looked at PowerShell and for commands and just can't work it out.

我发现了这三行代码,它在Windows 7上运行良好,但在Windows Server 2008上却无法运行!

I found this 3-line piece of code and it worked well on Windows 7 but not on Windows Server 2008!

@echo off
set Date=%date:~10,4%%date:~4,2%%date:~7,2%
move d:\Test\*.zip d:\Test1\*%Date%.zip

在服务器上,它无法理解*%Date%.一旦我删除了*,它就起作用了.我曾尝试使用引号等其他变体,但无法正常工作.我知道日期不是我要解决的办法,但是如果我无法使这条简单的线正常工作,那么其余的事情都将无关紧要.

On the Server, it could not understand the *%Date%. As soon as I removed the *, it worked. I have tried different variations with quotes etc. but not working. I understand the date is not the solution I am after, but if I can't get this simple line to work it wouldn't matter with the rest.

哦,偏移量确实起作用了,所以我得到了显示为yyyymmdd的日期. 我避免使用PowerShell,因为我不太擅长使用它,而且我也查看了VBS.但这不是重点.有人可以协助吗?

Oh and the offsets did work so I got the date to appear yyyymmdd. I have avoided PowerShell as I am not good with it and I also looked at VBS. But it's not strong points. Can anyone assist?

我知道有很多类似的问题,但是没有什么完全符合我的想法.

I know lots of questions are out there similar but nothing quite matches what I am trying to do.

推荐答案

WMIC可用于将最后修改的时间戳记为秒精度.前14个字符以您想要的格式提供时间戳.

WMIC can be used to get the last modified timestamp to fractional second precision. The first 14 characters give the timestamp in exactly the format you are looking for.

这是一种快速解决方案,可以完全满足您的要求:

Here is a fast solution that does exactly what you asked:

@echo off
for /f "skip=1 delims=" %%A in (
  'wmic datafile where "drive='d:' and path='\\test\\' and extension='zip'" get lastModified^,name'
) do for /f "tokens=1*" %%B in ("%%A") do (
  set "timestamp=%%B"
  set "file=%%~fC"
  set "fileName=%%~nxC"
  setlocal enableDelayedExpansion
  move "!file!" "d:\test1\!timestamp:~0,14!-!fileName!"
  endlocal
)

如果您想更改要求,以上内容是很挑剔的.如果不小心,您的WMIC命令可能会花费很长的时间,并且有些文件掩码根本无法用WMIC来模拟.

The above is finicky if you want to change the requirements. Your WMIC command can take a loooong time if you are not careful, and there are some file masks that simply cannot be emulated with WMIC.

下面是一个非常慢的解决方案,非常方便.您传入源文件掩码和目标路径作为参数.假设脚本名为"addTimeStamp.bat",那么您将使用addTimeStamp "d:\test\*.zip" "d:\test1"

Below is a much slower solution that is very convenient. You pass in the source file mask and destination path as parameters. Assume the script is named "addTimeStamp.bat", then you would use addTimeStamp "d:\test\*.zip" "d:\test1"

@echo off
setlocal disableDelayedExpansion
set "filePath="
if "%~2" neq "" for /f "eol=: delims=" %%F in ("%~2\") do set "filePath=%%~fF"
for %%F in (%1) do (
  set "file=%%~fF"
  set "fileName=%%~nxF"
  setlocal enableDelayedExpansion
  for /f "skip=1" %%A in (
    'wmic datafile where "name='!file:\=\\!'" get lastModified'
  ) do for /f %%A in ("%%A") do (
    set "timestamp=%%A"
    move "!file!" "!filePath!!timestamp:~0,14!-!fileName!
  )
  endlocal
)

这篇关于将文件夹中的文件作为.bat文件移动和重命名,并在开始处添加修改日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆