Home > Software design >  Move files of certain type while keeping file structure
Move files of certain type while keeping file structure

Time:07-26

I'm trying to set up a batch script that can move files of a certain file type into a sub-folder, but with the same file structure. So for example, I have the following folder structure all full of different file types, including .txt files:

  • folder0/folder1
  • folder0/folder2
  • folder0/folder2/folder3

and so on. I want to move only the .txt files from these folders into a sub-folder with the same file structure:

  • folder0/folderNew/folder1
  • folder0/folderNew/folder2
  • folder0/folderNew/folder2/folder3

and so on. The batch file would be located in folder0. It's also important that this is done with relative file paths. What's an easy way to go about this? I've seen solutions so far to move everything with the same file extension to one folder, but nothing where the file structure is kept the same. Any help is appreciated!

CodePudding user response:

That file movement task can be done with a single command line in the batch file using ROBOCOPY.

@%SystemRoot%\System32\robocopy.exe "%~dp0\" "%~dp0folderNew" *.txt /S /MOVE /XD "%~dp0folderNew" /NDL /NFL /NJH /NJS /R:0 >nul

%~dp0 references the full path (drive path) of the batch file directory always ending with a backslash. Run in a command prompt window call /? to get output the usage help of command CALL which explains how batch file arguments can be referenced in a batch file including always existing argument 0.

The source directory is the directory of the batch file. There must be appended a backslash as %~dp0 expands always to a path ending with a backslash and ROBOCOPY interprets a backslash left to one more \ or " as escape character for the backslash or double quote. The second " should be interpreted as end of source directory path and not as literal character being part of the source directory path which is the reason for the additional backslash in first ROBOCOPY argument string "%~dp0\".

The destination directory is the subdirectory folderNew in batch file directory folder0. ROBOCOPY automatically creates the entire directory tree to the destination directory on not existing.

The third argument is *.txt to move only files with that file extension.

The first option is /S to search not only in source directory folder0 for text files, but also in all non-empty subdirectories.

The second option is /MOVE to move the found text files instead of copying them. The directory structure is automatically replicated by ROBOCOPY. The directory creations and file movements are done with just updating the file allocation table of the file system of the drive resulting in a very short execution time.

The destination directory is a subdirectory of the source directory. For that reason the option /XD with the destination directory must be used additionally to exclude the destination directory on searching for text files in specified source directory and its subdirectories.

The other options are just for preventing any output than error output and doing the file movement without any retry in case of a text file to move is opened currently in an application with denied shared access preventing moving the file while being opened by the application. ROBOCOPY just copies a file being opened by an application if at least reading the file contents is allowed for the opened file.

File attributes like the hidden attribute or the read-only attribute do not matter as ROBOCOPY moves by default all files matching the wildcard pattern *.txt independent on the attributes.

Run in a command prompt window robocopy /? for output of its usage help explaining the syntax of this Windows command and its options.

Which directory is the current directory on execution of the batch file does not matter in this case.

There can be used also the following command line to always do the file movement in current directory instead of batch file directory:

@%SystemRoot%\System32\robocopy.exe "." "folderNew" *.txt /S /MOVE /XD "folderNew" /NDL /NFL /NJH /NJS /R:0 >nul

CodePudding user response:

Something like this might help you:

md NewFolder
for /f "tokens=*" %%d in ('dir /b /s /ad') do for %%f in ("%%d\*.txt") do (
if NOT exist "NewFolder\%%d" md "NewFolder\%%d"
move "%%f" "NewFolder\%%d"
)

Note that the command extensions should be available.

  • Related