2016-09-09 6 views
0

ディレクトリを作成し、ファイル名に基づいて画像ファイルを移動するバッチファイルを作成しようとしています(これは、起こった日付です)。変数が正しく設定されていないという問題が発生しています。私は以下のようにコードを実行し、ファイルは移動されません。Forループからのデータの解析 - バッチ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 

変数を正しく設定するにはどうすればよいですか? ありがとうございます!

+1

遅延拡張を有効にしている間は、正しく使用していません。この問題は、StackOverflowで週に6回のアドレスを取得します。 'Set" tokenyear =!token:〜0,4! "' – Squashman

+0

@Squashmanあなたは正しいですか?その(そしていくつかの大文字の問題)が問題を解決しました。迅速なご協力ありがとうございます。必ず使用する必要があることを正しく理解してください! FORループ操作では%%の代わりに?また、私はこれらの他の質問を見つけられなかったことを謝罪します。私は適切な場所で検索していなかったと思います。私は "それ"の男であることが嫌い! – Haveycode

+0

倍率は、FOR変数でのみ使用されます。環境変数はパーセントで囲まれています: '%var%'。あなたがFORコマンドのようなコードブロックの中にいて、あなたが変数を操作しているなら、遅延拡張を使う必要があります。その場合、 '!var!'を使用します。 – Squashman

答えて

0

%%tokenyearが正しくなく、変数値に展開されません。 %tokenyear%が必要です。 %tokenmonth%と同じです。環境変数は、バッチ引数や変数と全く同じではありません。

0

私は結局最後の製品です。 @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 
関連する問題