将远程服务器上的文件夹移动到远程服务器上的新位置时,如何修复附加时间戳?

路德

我正在尝试在我的本地机器上运行一个批处理脚本,它将处理某些服务器上的一些日志归档。我可以通过文件资源管理器“\SERVERNAME\C$\SOME FOLDER”访问服务器。当我尝试从本地 xcopy 从源到目标并附加时间戳时,它就像 TIMESTAMP 变量不存储我的日期/时间串联。

这是针对 Windows 2012r2 服务器,我尝试只将日期\时间附加到最后,效果很好,但是,它不是我正在寻找的所需格式,它开始将目录与日期嵌套但没有时间和它看起来一团糟。:(

我也尝试过使用 wmic,但是这是我第一次编写批处理文件来自动执行某些任务,因此所有这些都是一次很棒的学习体验。

我试过了echo %TIMESTAMP%,没有任何回报?我什至尝试将串联(%CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%)直接添加到文件目录中,但它不起作用:(

REM Check to see if a service on the machine is stopped (it is always stopped by the time it gets here) before we move the files from the logging directory to a new one.
for /F "tokens=3 delims=: " %%H in ('sc \\REMOTESERVER query "SOME SERVICE NAME" ^| findstr "        STATE"') do (
  if /I "%%H" == "STOPPED" (
    REM substring the date and time and then concat it together at the end to make the desired timestamp variable
      set CUR_YYYY = %date:~10,4%
      set CUR_MM = %date:~4,2%
      set CUR_DD = %date:~7,2%
      set CUR_HH = %time:~0,2%
      set CUR_NN = %time:~3,2%
      set CUR_SS = %time:~6,2%
      set CUR_MS = %time:~9,2%
      set TIMESTAMP = %CUR_YYYY%%CUR_MM%%CUR_DD%-%CUR_HH%%CUR_NN%%CUR_SS%

    REM copy files from the servers source directory and then move the files to a newly created logging folder with a timestamp appened at the end
    echo d | xcopy /f /y "\\REMOTE SERVER\src" "\\REMOTE SERVER\dest\Logging_%TIMESTAMP%" /E /I
    REM delete the contents of the servers source directory to keep things nice and clean
    pushd \\REMOTE SERVER\src && del . /F /Q popd
  )
)

预期结果如下: 服务器上的 SourceFolder 将在那里,但空的 DestinationFolder 将创建一个新的 Logging 文件夹 Logging_20190325010101 并且在新创建的日志文件夹中,SourceFolder 中的所有内容都应该在那里。

格哈德

您需要=在 set 命令之前和之后去除空格,此外,您需要delayedexpansion在代码块中更改变量,并且有更好的方法来去除冒号和逗号。

@echo off
setlocal enabledelayedexpansion
REM Check to see if a service on the machine is stopped (it is always stopped by the time it gets here) before we move the files from the logging directory to a new one.
for /F "tokens=3 delims=: " %%H in ('sc \\REMOTESERVER query "SOME SERVICE NAME" ^| findstr "        STATE"') do (
  if /I "%%H" == "STOPPED" (
REM substring the date and time and then concat it together at the end to make the desired timestamp variable
  set "CUR_YYYY=%date:~10,4%"
  set "CUR_MM=%date:~4,2%"
  set "CUR_DD=%date:~7,2%"
  set "mytime=!time::=!"
  set "mytime=!mytime:,=!"
  set "TIMESTAMP=!CUR_YYYY!!CUR_MM!!CUR_DD!-!mytime!"

REM copy files from the servers source directory and then move the files to a newly created logging folder with a timestamp appened at the end
echo d | xcopy /f /y "\\REMOTE SERVER\src" "\\REMOTE SERVER\dest\Logging_!TIMESTAMP!" /E /I
REM delete the contents of the servers source directory to keep things nice and clean
pushd \\REMOTE SERVER\src && del . /F /Q popd
 )
)

然而,为了解释你的问题,当你设置一个变量时,空格是变量的一部分。所以:

set variable = value

将导致变量带有尾随空格%variable %而值带有前导空格<space>value所以我们总是去掉空格,最好使用双引号来消除值后面可能出现的空格。例如:

set "variable=value"

这将创建%variable%value

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用SSH将文件从文件夹移动到远程服务器上的文件夹,而无需将文件下载到本地计算机

重命名远程服务器上的文件夹

FTP在FileZilla中的远程服务器上移动文件/文件夹

如何使用 Java Config 动态提供文件名,将文件从一个文件夹移动到远程 sftp 服务器上的另一个文件夹?

如何使用SharpSsh和C#将文件从一个文件夹移动到远程服务器上的另一个文件夹

将/ etc文件夹保留在远程服务器上

将mysqldump命令启动到远程服务器,并将mysqldump的备份文件保存在同一远程服务器的特定文件夹中

如何将文件从目录A移动到远程服务器中的目录B?

如何使用rsync将多个文件移动到远程服务器?

Python将SFTP服务器上的文件移动到另一个文件夹

Grunt.js-删除/清理远程服务器上的文件夹

git忽略并取消跟踪远程服务器上的文件夹

查询:rsync-将本地文件夹从远程服务器复制到远程服务器

如何使用 ftplib 写入远程服务器上的文件

导航到远程服务器的文件夹结构

远程服务器上的新终结器窗口

Jupyter Notebook:在远程服务器上执行:文件是否写入远程服务器?

如何配置rsyslog以将文件从特定程序发送到远程服务器上的特定位置

在python中使用sftp将文件从本地移动到远程服务器

在Python上重命名远程服务器上的文件

如何将文件从远程ubuntu服务器传输到远程ubuntu服务器?

在后面的代码中在远程服务器上创建新文件夹

在远程服务器上安装Mapserver

远程服务器上的子进程

远程服务器上的UnintentionalCodeFirstException(Azure)

远程服务器上的天气API

远程服务器上的 Apollo Playground

在远程服务器上使用Intellij

在远程服务器上分配变量