1.

Solve : Move batch files ...?

Answer»

Hi,

How to write batch file commands to move files (if exists) from root directory to the final . 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.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: Lemonilla 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.
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.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!"
)
)
pauseThank you all.
Thank you foxidrive . it works.


Discussion

No Comment Found