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

问题描述 投票:0回答:2

我正在尝试在我的本地计算机上运行批处理脚本,该脚本将负责某些服务器上的某些日志存档。我可以通过文件资源管理器“\ 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中的所有内容都应该在那里。

batch-file batch-rename
2个回答
1
投票

你需要在你的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


0
投票

在括号内的代码块中,在检索设置它们的同一块中的变量值时,必须延迟扩展。例如:

(
    set "test=1"
    echo [%test%]
)

...将回显“[]”,因为%test%是在设置它的同一个括号代码块中检索的。在评估%test%时,它没有任何价值。你必须通过使用setlocal enabledelayedexpansion或使用call echo [%%test%%](或cmd /c或类似)延迟扩展。当使用setlocal enabledelayedexpansion时,通过使用!而不是%来表示变量来延迟扩展。有关详细信息,请参阅cmd控制台中的setlocal /?set /? - 特别是开头的set /?部分“最后,添加了对延迟环境变量扩展的支持。”

此外,使用wmic os get localdatetime组合您的时间戳更简单,更多区域设置不可知。例:

for /f "delims=." %%I in ('wmic os get localdatetime /value ^| find "="') do set "%%~I"
echo %localdatetime%

这应该导致%localdatetime%包含YYYYMMDDHHMMSS的当前数值。

© www.soinside.com 2019 - 2024. All rights reserved.