3

I am hoping somebody can help me whip up a batch file.

I have a folder with over 800 directories, each directory has a photo.png and a sub-directoryA which contains 3 sub-directories, I need to move the photos from each directory 2 sub-directories down, into sub-directory1

I know a little bit about programming/scripting just not enough lol, I figure it would be some kind of for loop to recursively look through each directory and find every instance of the photo.png and somehow move each one into the sub-directories

For example

> ─Parent
>     ├─────directory1
>     │         ├──sub-directoryA
>     │                  ├──sub-directory1   <──────────│        
>     │                  ├──sub-directory2              │
>     │                  ├──sub-directory3              │
>     │         photo.png <────────move this into here──│ 
>     ├─────directory2
>     │         ├──sub-directoryA
>     │                  ├──sub-directory1   <──────────│        
>     │                  ├──sub-directory2              │
>     │                  ├──sub-directory3              │
>     │         photo.png <────────move this into here──│ 
>     ├─────directory800
>     │         ├──sub-directoryA
>     │                  ├──sub-directory1          
>     │                  ├──sub-directory2     
>     │                  ├──sub-directory3      
>     │         photo.png 
2

PowerShell:

$path       = 'c:\parent'
$filter     = 'photo.png'
$DestSubDir = 'subDirectoryA\SubDirectory1'

( gci $path $filter -recurse -depth 1 ) |
    Move-Item -Destination { Join-Path $_.DirectoryName $DestSubDir }

gci Move-Item Join-Path

  • Thanks for replying, I have never messed with powershell before, but I'll have to give it a try – Ohallahan Apr 2 '20 at 1:23
1

Alright, so after googling around I finally found a solution that I will post here for future reference in case anybody else is wanting to do the same thing. I will post the full script and then a breakdown of what each part does. Just place this batch file in the directory you are working in and it will scan through the directories below it and move the photos, just change up the names of the photo or directories, you can even change the file extension and move around videos, text files, etc :)

Remember use a single % for a CMD window, and 2 %% for a batch file

for /f "tokens=*" %%a in ('dir /b /A:D "*"') 
do (
if not exist "%%a\sub-directoryA" 
md "%%a\sub-directoryA\sub-directory1"
Echo >-------------------------------------------------------------------<

if exist "%%a\photo.jpg" echo  %%a photo.jpg found.
if exist "%%a\photo.jpg" move "%%a\photo.jpg" "%%a\sub-directoryA\sub-directory1\" >NUL
if exist "%%a\*.jpg" echo  %%a *.jpg found.
if exist "%%a\*.jpg" move "%%a\*.jpg" "%%a\sub-directoryA\sub-directory1\" >NUL
Echo:
)

Echo >-------------------------------------------------------------------<

timeout /t 5 >NUL

.

for /f "tokens=*" %%a in ('dir /b /A:D "*"') 

:for /f Loop command against a set of files

"tokens=*" cause all items to be processed

%%a is a command line parameter, just creates a parameter for each item found

('dir /b /A:D "*"')

/B - Bare format (no heading, file sizes or summary)

/A:D - file attributes in this case it's a Folder

"*" - just denotes all folders

do (
if not exist "%%a\sub-directoryA" md "%%a\sub-directoryA\sub-directory1"

:checks if a folder called sub-directoryA exists against all, if it doesn't it creates it, as well as sub-directory1

if exist "%%a\photo.jpg" echo  %%a photo.jpg found.
if exist "%%a\photo.jpg" move "%%a\photo.jpg" "%%a\aa\artwork\" >NUL

just checks against all files and if the file named is found ECHO displays the message on the screen and then moves it to the designated folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.