Batch create folders based on part of the file name and move files to this folder

I have 1.6 million (!) PDF files in one folder. All file names are similar to the following:

LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf LAST_FIRST_7-24-1936 Glasses RX 6-1-11_3.pdf 

I need to create a folder based on the first part of a file, and then transfer this file and all other files with the same first part of the file name to this folder. In this case, the folder will be called "LAST_FIRST_7-24-1936". The folder will always be called the same as the first part of the file, to the place.

I would like to create a batch file that will do this. Thanks to my elementary programming knowledge, I came up with this logical process for this:

 1 Take the first file and name it var1 2 Remove everything after the space in var1 and name it var2 3 Create a folder named var2 4 Move the file var1 into the folder var2 5 If there are more files Go to line 1, otherwise end 

I do not know what the syntax for this will be.

I found this link. I need a script to create folders based on file names and automatically move files. I made this batch based on this link

 pushd D:\Data\Medinfo PDFs for %%F in (*.pdf) do ( 2>nul md "%%~nF" >nul move /y "%%~nF*.*" "%%~nF" ) popd 

But this does not allow me to create a folder name only from part of the file name. If I can understand this part, I think it will work. I know that I need to create a variable for the folder name, but I do not know how to edit the file name variable to delete everything after the space. Any help would be greatly appreciated. I don't mind doing it in PowerShell or something else if it works natively in Windows Server 2008 R2.

+4
windows folder batch-file
source share
2 answers
 @ECHO OFF SETLOCAL SET "sourcedir=c:\sourcedir" PUSHD %sourcedir% FOR /f "tokens=1*" %%a IN ( 'dir /b /ad "*_*_*-*-* *.*"' ) DO ( ECHO MD %%a ECHO MOVE "%%a %%b" .\%%a\ ) POPD GOTO :EOF 

This should complete the required task - or at least show the necessary instructions.

If you are satisfied with the issued commands, install sourcedir in the required root directory and delete the two echo keywords to activate.

The message "already exists" generated by MD when trying to recreate an existing directory can be canceled by adding 2>nul to the MD line.

Similarly, a single file move report can be canceled by adding >nul to the MOVE line.

2>nul suppresses error messages (attempting to create an existing directory is an error), while the message "files moved" is a normal output message, hence the difference.


Addition - how it works.

First, PUSHD sets the current target directory.
The output of the DIR command is indicated by the FOR/F symbol. The tokens=1* sentence indicates that the first token (1) is assigned to the nominated meta-variable ( %%a ) and implicitly the second token (*) - %%b is just the next alphabet. The current * means everything after those token numbers explicitly mentioned . There is no delims clause, so default separators are used (set SEPARATORS, SPACE ,; TAB .

DIR targets the mask *_*_*-* *.* , So only files matching this mask will be found - where * means any number of any characters -. Since the mask is "quoted" , spaces are included in the mask. Without quotes, two separate masks will be specified. The /b option creates a list in basic form, that is, only names, without headings or summaries. The /ad switch suppresses any directory names that can be set on the mask.

Therefore, for LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf DIR lists LAST_FIRST_7-24-1936 Diagnostic - Topography 11-18-10_1.pdf and FOR/F tokenizes as LAST_FIRST_7-24-1936 to %%a and Diagnostic - Topography 11-18-10_1.pdf to %%b , using SPACE as the delimiter.

You can then restore the file name by reinserting a space between %%a and %%b . Any file name containing a delimiter must be specified to group characters and signals that they are not separated elements. The move target completes with \ to indicate "This is the directory name."

POPD restores the original registered directory.

+5
source share

It really helped me, and I made a small modification to process my digital photos (I'm a photographer) and named the batch file FolderAndMove. Here is my code:

 @ECHO OFF SETLOCAL SET "sourcedir=***PASTE_DIRECTORY_HERE***" PUSHD %sourcedir% FOR /f "tokens=1,2,3,4 delims=_" %%a IN ( 'dir /b /ad "*_*_*_*.*"' ) DO ( MD %%a_%%b 2>nul MOVE "%%a_%%b_%%c_%%d" .\%%a_%%b\ >nul ) POPD GOTO :EOF 

Here is a simple description I made for other photographers:

FolderAndMove will take a folder with *_*_*_*.* Format files (for example, LastName_FirstInitial_Event#_Sequence#.JPG ), create one folder in the selected root folder for each unique file prefix *_* (in the example case, LastName_FirstInitial) and move all files after *_*_*_*.* agreements in the folder *_* corresponding to their prefix.

Note that the file format is accurate: variables (*) must be separated (~ segmented) with underscores without spaces for FolderAndMove to work.

0
source share

All Articles