繁体   English   中英

从For循环中解析数据-批量CMD

[英]Parsing Data from For Loop - Batch CMD

我试图写一个批处理文件,该文件创建目录并根据文件名移动图像文件(恰好是拍摄日期)。 我遇到变量设置不正确的问题。 我运行如下所示的代码,并且没有文件被移动。

@echo Off
setlocal enabledelayedexpansion
REM Collect list of file names in current directory that have the .bmp file extension. Output to text file.
dir /b "*.bmp" >BMP.txt
REM For loop examines the text file for individual file names.
FOR /F "tokens=1" %%# in (C:\Users\jhavekos\Documents\Pinger\BMP.txt) do ( 
    REM SET variable "#" to equal "token"
    Set "token=%%#&&"
    REM Extract the first 4 characters (year) from the file name and set is to variable "tokenyear"
    Set "tokenyear=%%token:~0,4%%"
    REM Extract the month characters from the file name and set the variable as "tokenmonth"
    Set "tokenmonth=%%Token:~4,2%%"
    REM Copy from current directory to: Current Directory\ImageTakenYear\ImageTakenMonth\ 
    REM Any bmps with token year and token month get copied.
    Robocopy .\ ".\%%TokenYEAR\%%TokenMonth" %%TokenYEAR%%TokenMonth*.bmp
    REM Echo the variable tokenyear
    Echo %%tokenyear
    REM Echo the variable tokenmonth
    Echo %%tokenmonth
)
pause

输出的最后一行如下所示:

%tokenmonth
%tokenyear

RoboCopy输出如下:如您所见,我需要填充变量。

-------------------------------------------------------------------------------

  Started : Fri Sep 09 14:41:38 2016

   Source : C:\Users\jhavekos\Documents\Pinger\
     Dest : C:\Users\jhavekos\Documents\Pinger\%TokenYEAR\%TokenMonth%\

    Files : %TokenYEAR%TokenMonth*.bmp

  Options : /COPY:DAT /R:1000000 /W:30

我需要更改什么才能使变量成功填充? 谢谢大家!

%%tokenyear不正确,并且不能扩展为可变值。 您想要%tokenyear%。 与%tokenmonth%相同。 环境变量与批处理参数或变量并不完全相同。

这是我最终得到的最终产品。 感谢@Squashman的帮助,已对其进行了编辑。 我还添加了将月份目录更改为月份名称的逻辑。

再次感谢!

@echo Off
setlocal enabledelayedexpansion
REM Collect list of file names in current directory that have the .bmp file extension. Output to text file.
dir /b "*.bmp" >BMP.txt
REM For loop examines the text file for individual file names.
FOR /F "tokens=1" %%# in (C:\Users\jhavekos\Documents\Pinger\BMP.txt) do ( 
    REM SET variable "#" to equal "token"
    Set "token=%%#"
    REM Extract the first 4 characters (year) from the file name and set is to variable "tokenyear"
    Set "tokenyear=!token:~0,4!"
    REM Extract the month characters from the file name and set the variable as "tokenmonth"
    Set "tokenmonth=!token:~4,2!"
    IF !tokenmonth!==01 (Set "tokenmonthdirectory=!tokenmonth! January")
    IF !tokenmonth!==02 (Set "tokenmonthdirectory=!tokenmonth! February")
    IF !tokenmonth!==03 (Set "tokenmonthdirectory=!tokenmonth! March")
    IF !tokenmonth!==04 (Set "tokenmonthdirectory=!tokenmonth! April")
    IF !tokenmonth!==05 (Set "tokenmonthdirectory=!tokenmonth! May")
    IF !tokenmonth!==06 (Set "tokenmonthdirectory=!tokenmonth! June")
    IF !tokenmonth!==07 (Set "tokenmonthdirectory=!tokenmonth! July")
    IF !tokenmonth!==08 (Set "tokenmonthdirectory=!tokenmonth! August")
    IF !tokenmonth!==09 (Set "tokenmonthdirectory=!tokenmonth! September")
    IF !tokenmonth!==10 (Set "tokenmonthdirectory=!tokenmonth! October")
    IF !tokenmonth!==11 (Set "tokenmonthdirectory=!tokenmonth! November")
    IF !tokenmonth!==12 (Set "tokenmonthdirectory=!tokenmonth! December")
    REM Copy from current directory to: Current Directory\ImageTakenYear\ImageTakenMonth\ 
    REM Any bmps with token year and token month get copied.
    Robocopy .\ ".\!tokenyear!\!tokenmonthdirectory!" !tokenyear!!tokenmonth!*.bmp
    REM Echo the variable tokenyear
    Echo !tokenyear!
    REM Echo the variable tokenmonth
    Echo !tokenmonth!
    Echo !tokenmonthdirectory!
)
pause

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM