1

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

0

2 Answers 2

3

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"
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response. Unfortunately, that code produced more errors. .... '"SET month=8"' is not recognized as an internal or external command,operable program or batch file. MONTH=0-1 YEAR=2013....Also, The problem is with moving the files instead of parsing the date fields.
In addition, this batch file will be used by all employees and I can't be sure what format they have their system date set to. Research indicates the WMIC is safer in that instance. But thanks for your input.
I had a wrong double quote there. In your MOVE command the asterisk behind %FILEDATE% is missing, see Foxidrive's answer.
Thank you. That adjustment eliminated the missing operand error and the other one, too. I really appreciate your help. Now on to the 2nd part. =)
1

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%"

3 Comments

Thank you, foxidrive. I made a typo in my original post. My code actually does have the asterisk (move C:\FOLDER1\*%FILEDATE%*.* %FOLDER2%). But I added quotes around the two elements as you showed in your example and it still did not work. I also removed the .* after %FILEDATE% and it still does not work. Stumped!
Put a pause command after the MOVE line and tell us what error message appears.
Thanks for the quick response. The error message didn't really help, but I echoed all the variables again and it turns out the MONTH variable picked up a trailing space somewhere after the FOR routine. After research and modification, the trailing space is now gone and the MOVE command works. I really appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.