Moving files conditionally

June 25, 2010 at 10:31:23
Specs: Windows2003

I am trying to write a batch file which needs to browse a given directory and look for all *.pdf and move files matching a name criterion to a subfolder.

It needs to look for 6 matching chars at offset 15 as defined in varComputed string variable. I am having trouble telling the script how to do it. Could someone help?

See More: Moving files conditionally

Report •

June 25, 2010 at 12:51:10
@echo off

set filename=aaaaaaaaaaaaaaaBBBBBBccccccccccccc.pdf
set var=BBBBBB

if "%filename:~15,6%" equ "%var%" echo Match!

Report •

June 25, 2010 at 13:17:59
Thanks Mark. i should be more clear on what I need though...
It should
1. read all files in the directory (no recursing)
2. move files matching 6 characters at offset 15 to a predefined variable to a subfolder

Consider the following:

set compvar=201006
For /F "tokens=*" %%A in ('dir /b /a-d *.pdf') DO (
set checkvar=%%a
set checkvar=!checkvar:~14,6!
if checkvar==%compvar% DO (
Move /Y %%A <subfolder name>

This is what I can come up with. What do you think?

Report •

June 25, 2010 at 14:00:32
@echo off & setlocal EnableDelayedExpansion
set compvar=201006

cd /D Your Directory

for %%j in (*.pdf) do (
  set checkvar=%%j
  if "!checkvar:~14,6!"=="%compvar%" move "%%j" "<subfolder name>"

Report •

Related Solutions

Ask Question