Question: Firstly I'm newbie so sorry if this is a simple task.
I have a folder which contains only PDF files and I need to copy only files which contain certain text [this text varies but the start is always the same]
for example

* test\_variablename\_datestamp.PDF.
* testing\_variablename\_datestamp.PDF.
* sample\_variablename\_datestamp.PDF.

I would like to copy any file which starts with test to a test folder, and so testing to testing folder and sample to sample folder.
I plan to have this as a script to run every 30 minutes so it only needs to copy if the file has changed [this should reduce the amount of copying it has to do] 

I'm new to batch scrip writing but I am trying it out today.

Thanks in advance
J

Answer: 
```dos
@ECHO OFF
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
FOR /f "tokens=1*delims=_" %%a IN (
 'dir /b /a-d "%sourcedir%\*_*.pdf" '
 ) DO IF NOT EXIST "%destdir%\%%a\%%a_%%b" (
  ECHO(MD "%destdir%\%%a"
  ECHO(COPY "%sourcedir%\%%a_%%b" "%destdir%\%%a\%%a_%%b"
)
GOTO :EOF

```

You would need to change the settings of `sourcedir` and `destdir` to suit your circumstances.

The required MD commands are merely `ECHO`ed for testing purposes. **After you've verified that the commands are correct**, change `ECHO(MD` to `MD` to actually create the directories. Append `2>nul` to suppress error messages (eg. when the directory already exists)

The required COPY commands are merely `ECHO`ed for testing purposes. **After you've verified that the commands are correct**, change `ECHO(COPY` to `COPY` to actually copy the files. Append `>nul` to suppress report messages (eg. `1 file copied`)

The process reads each filename from a directory listing in basic form (/b) suppressing directory entries (/a-d) of all files matching `*_*.pdf`, assigning the token before the first `_` to `%%a` and the remainder to `%%b`.

If the destination filename does not exist, then make the subdirectory `%%a` under the destination directory, then copy the file.

If you want to copy regardless, remove the `if not exist...` clause, but retain the closing parenthesis on that physical line.
This should reduce unnecessary copies:

```
@echo off

rem -- set folder paths WITHOUT ending slash
set pdf_folder=c:\pdfs
set test_folder=c:\tests
set testing_folder=c:\tests
set sample_folder=c:\sample

pushd %pdf_folder%

 rem -- COPY the non-existing in target folder files
 echo n|copy /-y test_*.pdf %test_folder%\
 echo n|copy /-y test_*.pdf %testing_folder%\<n
 echo n|copy /-y sample_*.pdf %sample_folder%\<n

 rem -- UPDATE only modified files
 replace test_*.pdf %test_folder% /u
 replace testing_*.pdf %testing_folder% /u
 replace sample_*.pdf %sample_folder% /u

popd

```