0

I see this question How to get folder path from file path with CMD

and try resolve my problem without success.

I need move files from origem to destination similar path using CMD

my source with subfolder "origem"

c:\temp\origem\subfolderA\file1.pdf
c:\temp\origem\subfolderA\file2.pdf
c:\temp\origem\subfolderA\file3.pdf
c:\temp\origem\subfolderB\file1.pdf

I need move files to diferent (similar) path with changing subfolder "origem" to "destination"

c:\temp\destination\subfolderA\file1.pdf
c:\temp\destination\subfolderA\file2.pdf
c:\temp\destination\subfolderA\file3.pdf
c:\temp\destination\subfolderB\file1.pdf

If use above code

FOR /R c:\temp\origem %F IN (*.pdf) DO move "%~dpfF" "%~dpF"

On "%~dpF" the parameter p I don't know change to destination

Samir Morimoto
  • 189
  • 2
  • 4
  • In `FOR /R c:\temp\origem %F IN (*.pdf) DO ...` command, the `"%~fF"` parameter (no need for `dp` modifiers) specify _the full file path_, like in `c:\temp\origem\subfolderA\file1.pdf`. However, `"%~dpF"` specify just the drive and path, like in `c:\temp\origem\subfolderA`. If you insert this in a _nested_ `FOR %G in ("%~dpF") DO ...`, then `"%~nG"` is _the (last) name of the path_, like in `subfolderA`. Then, just combine previous values this way: `FOR /R c:\temp\origem %F IN (*.pdf) DO FOR %G in ("%~dpF") DO move "%~fF" "c:\temp\destination\%~nG"` – Aacini Dec 28 '22 at 07:41
  • 1
    Please open a [command prompt](https://www.howtogeek.com/235101/), run `robocopy /?` and read the output help. There can be used in a command prompt window or in a batch file `%SystemRoot%\System32\robocopy.exe "C:\temp\origem" "C:\temp\destination" *.pdf /S /MOVE /NDL /NFL /NJH /NJS /R:1 /W:1 >nul` That command line moves all PDF files in `C:\temp\origem` to `C:\temp\destination` with replicating the directory structure. All folders in `C:\temp\origem` being empty after moving the PDF files are deleted from `C:\temp\origem`. – Mofi Dec 28 '22 at 08:41
  • 1
    There can be used `/MOV` instead of `/MOVE` to just move the PDF files without deletion of empty folders in source directory tree. Please note that `/MOVE` deletes all empty folders in source directory tree, even the empty directories not containing a PDF file at all and not created in destination directory tree. __ROBOCOPY__ creates the destination directory `C:\temp\destination` and the subdirectories automatically. – Mofi Dec 28 '22 at 08:43
  • Thank you @Mofi. I tested your answer and when using ROBOCOPY and understood help parameters. When use /MOVE all folder are deletes **include "origem"**. Is possible delete all folder into "origem", but **not delete "origem" folder**? My solution was to **create a file.txt into the "origem" folder**. But, I wouldn't want to use use it this way. – Samir Morimoto Dec 28 '22 at 20:26
  • If the folder `origem` is also deleted by __ROBOCOPY__, then the reason is that all subfolders contain only PDF files and so all subfolders are moved to the destination folder resulting in finally also `origem` folder is being empty. In this case it would be faster to use `ren "C:\temp\origem" "destination"`. The command line `pushd "C:\temp\origem" && (%SystemRoot%\System32\robocopy.exe "C:\temp\origem" "C:\temp\destination" *.pdf /S /MOVE /NDL /NFL /NJH /NJS /R:1 /W:1 >nul & popd)` can be used to make temporarily the source directory the current directory for `cmd.exe`. – Mofi Dec 29 '22 at 10:22
  • That prevents the deletion of the source directory because of Windows denies the deletion of a directory which is the current directory of any running process. – Mofi Dec 29 '22 at 10:24

0 Answers0