for statement syntax / tokens and delims

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
corwinr
Posts: 1
Joined: 20 Sep 2010 14:03

for statement syntax / tokens and delims

#1 Post by corwinr » 20 Sep 2010 14:48

I have a few classes that are recorded every day and the files are automatically sorted in to folders when the recording completes. I am having some trouble with my routine:

My base directory has a file called RenameAndMove.bat and it contains the following script:


@For /F "tokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 delims=," %%E in (classnames.txt) DO @(
For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
Set All=%%C%%A%%B
For %%a in ("%%E-*%All%*.wmv") do @(
move %%a "backup\%%E\%Month%-%Day%.wmv"
)
)
)


The file classnames.txt contains a list of classes: calculus2,psych,crimlaw,criminaljust

When files are recorded, the output file format is: calculus2-tues-fa10_20100920135300.wmv

The script above grabs the file and renames it to the date, 09-20.wmv, and puts it in a calculus2 folder inside a backup folder.

The above script is copy and pasted five times and the last for statement has the %%E changed to %%F (for the second class name) and %%G (for the thrid class name) and so on.

When the script runs, the first class does not rename correctly. The file simply shows up as "-.wmv" with no date. The other classes in the list work okay. When I switch the names around in the list, the same thing happens, the first class name does not work.


Is there something I am doing wrong?

Thanks,

Ryan

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: for statement syntax / tokens and delims

#2 Post by aGerman » 20 Sep 2010 15:21

You could use delayed expansion

Code: Select all

setlocal EnableDelayedExpansion

For /F "tokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 delims=," %%E in (classnames.txt) DO (
  For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do (
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
    Set All=%%C%%A%%B
    For %%a in ("%%E-*!All!*.wmv") do (
      move %%a "backup\%%E\!Month!-!Day!.wmv"
    )
  )
)


But why do you split the date for each line in classnames.txt again and again? What about

Code: Select all

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do (
  Set Month=%%A
  Set Day=%%B
  Set Year=%%C
  Set All=%%C%%A%%B
)
For /F "tokens=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 delims=," %%E in (classnames.txt) DO (
  For %%a in ("%%E-*%All%*.wmv") do (
    move %%a "backup\%%E\%Month%-%Day%.wmv"
  )
)


And a last question:
Why do you define 20 tokens if one of the default delimiters in a simple for loop is a comma? Try

Code: Select all

For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do (
  Set Month=%%A
  Set Day=%%B
  Set Year=%%C
  Set All=%%C%%A%%B
)
For /F "delims=" %%D in (classnames.txt) DO (
  For %%E in (%%D) DO (
    For %%a in ("%%E-*%All%*.wmv") do (
      move %%a "backup\%%E\%Month%-%Day%.wmv"
    )
  )
)


BTW I didn't try any of this codes so I hope they will work.
[EDIT quotes removed in last code /]

Regards
aGerman

Post Reply