Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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"
share|improve this answer
    
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. –  jubilare Aug 10 '13 at 0:17
    
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. –  jubilare Aug 10 '13 at 0:22
    
I had a wrong double quote there. In your MOVE command the asterisk behind %FILEDATE% is missing, see Foxidrive's answer. –  Endoro Aug 10 '13 at 4:42
    
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. =) –  jubilare Aug 12 '13 at 14:13

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%"
share|improve this answer
    
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! –  jubilare Aug 12 '13 at 14:18
    
Put a pause command after the MOVE line and tell us what error message appears. –  foxidrive Aug 12 '13 at 14:20
    
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. –  jubilare Aug 12 '13 at 15:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.