Move files based on last 3 characters

  • Thread starter Thread starter jason.peace
  • Start date Start date
J

jason.peace

I need help writing a batch file that will move a file based on the
last 3 characters of a file name.

For example, a file with the name of 216316CGA0010100.tif will be
moved into a subdirectory z:\1\0\0 and a file with the name of
216316CGA0010727.tif will be moved into a subdirectory z:\7\2\7.
The files will always be 11 or 16 characters long.

Any help would be greatly appreciated!!
 
I need help writing a batch file that will move a file based on the
last 3 characters of a file name.

For example, a file with the name of 216316CGA0010100.tif will be
moved into a subdirectory z:\1\0\0 and a file with the name of
216316CGA0010727.tif will be moved into a subdirectory z:\7\2\7.
The files will always be 11 or 16 characters long.

Any help would be greatly appreciated!!

----- batch begins -------
[1]@echo off
[2]setlocal enabledelayedexpansion
[3]:: find each ".tif", process each name
[4]for %%i in (*.tif) do (
[5]set yfn=%%~ni
[6]REM grab last 3 characters of name & construct subdirectories
[7]ECHO MD z 2>nul
[8]ECHO MD z\!yfn:~-3,1! 2>nul
[9]ECHO MD z\!yfn:~-3,1!\!yfn:~-2,1! 2>nul
[10]ECHO MD z\!yfn:~-3,1!\!yfn:~-2,1!\!yfn:~-1! 2>nul
[11]ECHO MOVE %%i z\!yfn:~-3,1!\!yfn:~-2,1!\!yfn:~-1!
[12])
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped
and should be rejoined. The [number] that starts the line should be removed

The ECHO keyword needs to be removed to activate the directory
creation/move
It is there as a safety measure to show what the process WOULD do until
you have verified that it will do what you require

%varname% will be evaluated as the value of VARNAME at the time that
the line is PARSED. The ENABLEDELAYEDEXPANSION option to SETLOCAL causes
!varname! to be evaluated as the CURRENT value of VARNAME - that is, as
modified by the operation of the FOR

The "2>nul" on [7..10] suppresses "directory already exists" messages

Prefix "z" on [7..11] with "d:\path\" as appropriate
 
I need help writing a batch file that will move a file based on the
last 3 characters of a file name.

For example, a file with the name of 216316CGA0010100.tif will be
moved into a subdirectory z:\1\0\0 and a file with the name of
216316CGA0010727.tif will be moved into a subdirectory z:\7\2\7.
The files will always be 11 or 16 characters long.

Any help would be greatly appreciated!!

Hi Jason

this is untested :

@echo off
call :sub "216316CGA0010727.tif"
call :sub "216316CGA0010100.tif"
goto :eof
:sub
set fn=%~n1
move %1 "Z:\%fn:~-3,1%\%fn:~-2,1%\%fn:~-1%\%~1"


HTH
 
Back
Top