Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Move batch files ...  (Read 3853 times)

0 Members and 1 Guest are viewing this topic.

yasinirshad

    Topic Starter


    Newbie

    • Experience: Experienced
    • OS: Windows 2003
    Move batch files ...
    « on: March 18, 2013, 03:15:21 AM »
    Hi,

    How to write batch file commands to move files (if exists) from root directory to the final <year>.<month> directory?

    The files in root directory will be of format "ABC_121002_00023_21_8.dat" Please note here that in 121002, 12 is Year and 10 is month and 02 is date ...

    Source : C:\ABCD\ABC_121002_00023_21_8.dat
    Destination :  C:\ABCD\201210\ABC_121002_00023_21_8.dat  <-- 201210 ... 2012 is year and 10 is month from Filename. Assume this folder is already created ... we just need to move files to corresponding folder from filename.

    Please let me know.
    Thanks.

    Lemonilla



      Apprentice

    • "Too sweet"
    • Thanked: 70
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Move batch files ...
    « Reply #1 on: March 18, 2013, 05:21:47 PM »
    This seems to work.
    Code: [Select]
    for /f "delims=" %%A in ('dir /b ^| find *.dat') do (
    for /f "tokens=2 delims=_" %%B in ("%%A") do (
    move %%A %%B\%%A
    )
    )
    though it seems to freak out if it doesn't have anything to sort.
    Quote from: patio
    God Bless the DOS Helpers...
    Quote
    If it compiles, send the files.

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Move batch files ...
    « Reply #2 on: March 18, 2013, 05:34:51 PM »
    This seems to work.
    Code: [Select]
    for /f "delims=" %%A in ('dir /b ^| find *.dat') do (
    for /f "tokens=2 delims=_" %%B in ("%%A") do (
    move %%A %%B\%%A
    )
    )
    though it seems to freak out if it doesn't have anything to sort.
    You are missing the leading 20 for the output year.

    This can be done with a single FOR loop. Just use tokens=1,2* delims=_
    strip the first 4 characters off of %B and add in the leading 20.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Move batch files ...
    « Reply #3 on: March 18, 2013, 11:18:59 PM »
    This should work.  @Lemonilla: your find.exe syntax is fubar.

    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    for %%a in (*.dat) do (
    for /f "tokens=2 delims=_" %%b in ("%%a") do (
    set "info=%%b"
    set "year=20!info:~0,2!"
    set "month=!info:~2,2!"
    md "!year!!month!" 2>nul
    echo "%%a"
    move "%%a" "!year!!month!"
    )
    )
    pause

    yasinirshad

      Topic Starter


      Newbie

      • Experience: Experienced
      • OS: Windows 2003
      Re: Move batch files ...
      « Reply #4 on: March 19, 2013, 12:57:44 AM »
      Thank you all.
      Thank you foxidrive . it works.