Search code examples
batch-filebatch-processingbatch-rename

Move file from between subfolders under folder level


I have around 200 folders mapped to a drive, we will call D:. Within the 200 folders on D: they have four main folders, two of which are the current and expired folders. My question is, is there a way I can write a script to go through the 200 folders, and copy the one document from current to the expired folder on that level. After this step, I would like to rename the document in the current folder to remove the date (11 characters).

ex.

Copy D:\Client\Current\Current Quote 09 30 2014.docx --> D:\Client\Expired\Current Quote 09 30 2014.docx
Rename D:\Client\Current\Current Quote 09 30 2014.docx --> D:\Client\Current\Current Quote.docx

Thanks you in advance for your suggestions,

Jordan


Solution

  • Not sure if this will cover it, but it should at least set you in the right direction. It's a bit unclear if you have multiple files that you need to handle, or just one with potentially varying dates.

    @echo off
    setlocal ENABLEDELAYEDEXPANSION
    for /d %%a in (*) do (
        pushd %%a\current
        for %%f in (filename*) do (
            copy "%%f" ..\expired
            set fn=%%~nf
            set fn=!fn:~0,-11!
            ren "%%f" "!fn!%%xf"
        )
        popd
    )