user3010328
user3010328

Reputation: 23

batch moving file from subfolder to parent folder

So here is my scenario.

This is my folder structure

C:\DOCS\Project1\docname1\image.jpg
                \docname2\image.jpg
                \docname3\image.jpg
C:\DOCS\Project2\docname1\image.jpg
                \docname2\image.jpg
                \docname3\image.jpg

I'm trying to get a .bat going that will run from the "DOCS" folder, and move all the image.jpgs up one folder from the "docname" folders to the ""Project" folder.

The docname and project names are all different and follow to specific naming scheme so I cant just use a source and dest directory.

It would have to be something that would just find the image.jpg and move it up a parent folder.

this is what ive got but it isnt working.

for /d %f in (docs\*) do (
pushd %f
copy .\*.jpg ..
popd
)

Also I only need one of the .jpgs per project folder. so replacing/renaming isnt an issue.

Upvotes: 2

Views: 3630

Answers (3)

foxidrive
foxidrive

Reputation: 41224

This echos move commands to the screen - if they match what you need to do then remove the echo and run it. It focuses on all JPG files from c:\docs and below.

The .. here means to move up one level to the parent folder.

@echo off
pushd "C:\DOCS"
    for /f "delims=" %%a in ('dir *.jpg /a-d /b /s') do echo move /y "%%a" ..
popd
pause

Upvotes: 0

PA.
PA.

Reputation: 29339

in a bat file the loop variables are specified as %%f instead of just %f

so you shouldchange into

for /d %%d in (docs\*) do (
  pushd %%d
  ...

but you want to recurse into the directory tree and only inspect the docname* folders

so you should change to

for /d /r %%d in (docname*) do (
  pushd %%d
  ....

then you will have to move your .jpg files one dir up, and avoid the possible clash of filenames, using the move command with the /y option

so your bat finally becomes

for /d /r %%d in (docname*) do (
  pushd %%d
  move /y *.jpg ..
  popd
)

Upvotes: 0

mihai_mandis
mihai_mandis

Reputation: 1658

@echo off
setlocal enabledelayedexpansion

for /f "tokens=*" %%a in ('dir /A:D /S /B "C:\docs\*"') do (
    for %%y in ("%%a\*.jpg") do (
        call :GETPARENTPARENT "%%y" ret

        echo ret=!ret!
        move /Y "%%y" "!ret!"
    )
)

goto:EOF

:GETPARENTPARENT
set fileP=%1
echo received=%fileP%
for %%a in (%fileP%) do (
    set parent=%%~dpa
    cd !parent!\..
    set PPPath=!cd!
    for %%x in ("!PPPath!") do (
        set "%~2=%%~dpnx"
    )
)
GOTO:EOF

Upvotes: 1

Related Questions