Batch file - date variables and moving files with variable date file name from one folder to another

元气小坏坏 提交于 2020-01-05 05:48:06

问题


I need some assistance, please. I'm trying to create a batch file to move files from one folder to another. The file name will have variable yyyy-mm format plus additional data before or after the date. The batch will need to move the file to a server directory with the same mmmm-yy folder name.

I've come up with the code below, but it doesn't quite work.

  1. A "Missing Operand" error is returned.
  2. The new directory is created but the files are not moving from the old folder to the new one.

My code:

@echo off

FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
  SET /A MONTH=%%D
  SET /A YEAR=%%F
)

:: Set month to last month
set /a MONTH=%MONTH%-1

:: If month equals zero, reset to 12
if %MONTH%==0 set MONTH=12

:: If month < 10, fill with zero
if %MONTH% LSS 10 set MONTH=0%MONTH%

:: If month = 12, subtract one year
if %MONTH%==12 set /a YEAR=%YEAR%-1

SET FILEDATE=%YEAR%-%MONTH%

SET FOLDER2=E:\ARCHIVE\%FILEDATE%

MKDIR %FOLDER2%

:: trying to recreate the format MOVE C:\FOLDER1\\*2013-07*.* E:\FOLDER2\2013-07 which does work
MOVE C:\FOLDER1\\*%FILEDATE%*.* %FOLDER2%

:END

EXIT

EDIT: Both responders below really helped. I tried to vote them up, but I guess my reputation is not good. Mother was right - guard your repuation! It will get you far. :)


回答1:


You should better use %date%, not wmic, but you can try:

for /f "skip=1 delims=" %%a in ('WMIC Path Win32_LocalTime Get Month^,Year /Format:table') do for /f "tokens=1,2" %%b in ("%%a") DO SET "month=%%b" &SET "year=%%c"



回答2:


Just a minor edit - it looks like it should work - as your explanation says the folder is being created just fine.

MOVE "C:\FOLDER1\*%FILEDATE%*" "%FOLDER2%"


来源:https://stackoverflow.com/questions/18156110/batch-file-date-variables-and-moving-files-with-variable-date-file-name-from-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!