Speed up Batch file processing, moving file to folder of same name - file

I need some help moving files into folders of the same name. I have a batch file that works but it is very slow. I'm moving approximately 3300 .xlsx files into folders with like names. Here is what I have thus far:
#echo off
setlocal EnableDelayedExpansion
pushd "C:\New folder"
FOR %%G IN (*.xlsx) DO (
FOR /F "tokens=1 delims= " %%a IN ("%%G") do (
set "outFolder=%%a "
for /D %%i in (*.*) do (
for /F "tokens=1 delims= " %%b IN ("%%i") do (
if "%%a"=="%%b" set "outFolder=%%i"
)
)
if not exist "!outfolder!" md "!outfolder!"
move "%%G" "!outfolder!"
)
)
popd
pause
Again this works but is slow. This code moves the files to folder and if folder doesn't exist it creates it.
I have found this code and it works to a point. The following code does not seem to recognize the folders that already exist and instead creates a folder even though one already exists. Example: I have file 123456 Action List.xlsx that I would like to go to folder 123456 Health Center. The first code will accomodate that but is extremely slow and gets slower as it goes along. Here is the second code:
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.xlsx') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~0,6!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!" >nul
endlocal
)
Any help is appreciated.
#Squashman - I'll try to explain a little better...
123456 Action List.xlsx
123456 Reportcard.xlsx
123456 CHCUP.xlsx
123456 Combo3.xlsx
123457 Action List.xlsx
123457 Reportcard.xlsx
123457 CHCUP.xlsx
123457 Combo3.xlsx
Each month I end up with ~3300 files like this after running various macros. I have folders like "123456 Health Center" and "123457 MLK Center" already set up. What I'm trying to do is move all of those .xlsx files into the corresponding folder. That first code set works but like I said its slow. What its doing is looking to see if there are corresponding file and folder names based on first 6 characters. If there is it moves the file to that folder.
The second code is MUCH faster but it doesnt like the second part of the folder name i.e. "Health Center or MLK Center" from my examples and it then creates its own file with just the number portion i.e. 123456 or 123457 from my example.
Does that help?

Save this script as test.bat in a folder with other .bat files, and run from open Cmd Prompt. Replace dir value with the path to your folder with XLSX files. The script assumes, target folders in which the files are sorted are created in the dir folder. Let me know if any errors.
#echo off
setlocal enabledelayedexpansion
set "dir=C:\XLSX_Folder"
pushd "%dir%"
for /f "tokens=*" %%I in ('dir /b /a:-d "*.xlsx"') do (
if not exist "%%~nI" md "%%~nI" 2>nul
move "%%I" "%%~nI" >nul )
popd
exit /b

Related

Moving files with a batch file

I have 30 folders, one for each travel group with 10 subfolders each, each subfolder corresponds to an activity.Some pictures dont match with the group, so i have a list of the pictures that dont match.
Im using this batch to move the jpgs from the folders but it's putting them all together. is there any way that the batch creates a folder with the name of the folder it's been moved from?
for /r "originfolder" %%# in (*) do findstr "%%~nx#" "filelist.txt"&&move "%%#" "destinyfolder"
PAUSE
You'll need a few more lines. There are a few different ways, this being one of them.
#echo off
setlocal enabledelayedexpansion
pushd "originfolder"
for /F "usebackq delims=" %%a in ("filelist.txt") do for /F "delims=" %%i in ('dir /b /s "%%a" 2^>nul') do (
set "filepath=%%~dpi"
set "filepath=!filepath:~0,-1!"
for %%f in (!filepath!) do set "destpath=%%~nxf"
mkdir "C:\Full path to\destinypath\!destpath!"
move "%%~a" "C:\Full path to\destinypath\!destpath!"
)
popd
simply get the path of the file, then use the last name of the path (folder where file exists) and create that folder name in your destination path, before moving the file.
Note, this is untested code, so please use a test scenario before attempting this in production.

Automatically Create Folders Based on String in Filename

I need a batch file to create a process for a large list (2k) of TIF files in a local folder directory.
The filename structure is, for example: 12345_1.tif.
I need the batch to:
1 - Create a folder name based on the number(s) after the underscore, as this is the only constant in the naming. The folders only based on this sole number.
2 - Copy and move the file into the newly created folder.
In the example above, the batch would create a folder called 1 and then move the file 12345_1.tif into that folder. If it found another file such as 54321_1.tif, that file would also be moved to the "1" folder. In my files, the numbers after the _ range from 1 through 77, and there could multiple files that share the same number after the _.
I've observed some similar scripts online, but I need help to modify my requirement. Is it possible to modify this to meet my requirement?
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.PDF') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~11,6!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!"
endlocal
)
It seems you didn't understood my comment, so I post it here as an answer with complete code:
#echo off
for /F "tokens=1,2 delims=_." %%a in ('dir /B *.tif') do (
md "%%b" 2>NUL
move "%%a_%%b.tif" "%%b"
)

Batch file to create folder and move file in the folder

I am in the middle of batch extracting screenshots for contents we are planning to use on a tube site I am working on.
The jpeg files per content is labled as followed:
6c82c0239f6eb839-1
6c82c0239f6eb839-2
all the way to 120
The file name is different per content
a82384e2c46ba4af-1
a82384e2c46ba4af-2
etc.
They will all be extracted to a singe folder.
So I basically need a batch file that will create folders based on the content name without the dash and number and move all 120 jpegs in the folder with the content name.
For example:
Create folder named 6c82c0239f6eb839 and
move 6c82c0239f6eb839-1 to 6c82c0239f6eb839-120 in to the created folder.
I saw another thread with the following batch file. its pretty much what I want but the folder name is only 3 characters long and the files are copied to the newly created folders instead of moving them.
#echo off
SetLocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
set Folder=!Name:~0,3!
xcopy /y "%%a" !Folder!\
)
Could someone change this so that it will display full file name without the dash and number for the folders and move files in its respective folders instead of copy?
Thank you
#echo off
setlocal
#rem Get each jpeg file.
for /F "delims=" %%A in ('2^>nul dir /b *.jpeg') do (
rem Get filename as token before the dash.
for /f "delims=-" %%B in ("%%~A") do (
rem Make dir if needed.
if not exist "%%~B" md "%%~B"
rem Check if isdir.
2>nul pushd "%%~B" && popd
if errorlevel 1 (
>&2 echo Failed isdir "%%~B".
) else (
rem Do the move operation.
>nul move /y "%%~A" "%%~B"
if errorlevel 1 (
>&2 echo Failed move "%%~A" to "%%~B"
)
)
)
)
exit /b %errorlevel%
The code is well remarked so if you want to understand
the evaluated code by changing #echo off to #echo on.
The use of %errorlevel% after the exit /b is not
required though will let you know what the errorlevel is
when #echo on is used.
The pushd tests for a directory
(even if it is a symlink).
errorlevel is checked to decide if to echo a
error message or do the move.
As the for loop variables are used direct, use of
enabledelayedexpansion is not needed.
Many commands support the argument of /? to get help
about the command. i.e. move /?.
If you only try to copy the correct jpeg to the correct folder, you can do this:
#echo off
SetLocal EnableDelayedExpansion
CD <CORRECT ROOT PATH>
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
REM I presume all the files have 16 characters before the dash
set Folder=!Name:~0,16!
IF NOT EXIST !Folder! MKDIR !FOLDER!
xcopy /y "%%a" !Folder!\
)
I was not able to test.
First of all, I would like to apologize for my manners regarding my initial post.
The answer by micheal_heath has resolved my issue.
Furthermore, I happened to find this post by user Salmon Trout from a different site which also worked.
Batch file to make folders with part of file name and then copy files
#echo off
setlocal enabledelayedexpansion
for %%A in (*.psd *.jpg) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
pause
I just changed the the following line remove the hypen and numbers to create folders for the file name properly.
for /f "tokens=1* delims=-***" %%D in ("!fname!") do set folname=%%D
I still lack the knowledge on why and how both methods work, but this has been an interesting start for me. I hope other beginners trying to solve a similar issue can find something useful from this post.

create folders based on string in file name

I need a batch file to create a process for a list of files in a directory.
The filename structure is, for example: 00000_AAA_132144_2012021.txt
I need the batch to:
1 - Create a folder name based on the numbers after the second underscore, as this is the only constant in the naming.
2 - Move the file into the new folder.
In the example of the above the batch would create a folder called 132144 and then move the file 00000_AAA_132144_2012021.txt into the folder
For a similar requirement I used the script Endoro created for me (below). Is it possible to modify this to meet my requirement?
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.PDF') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~11,6!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!"
endlocal
)
If you know that the filenames will be the same length, you can do the following to get the numbers after the second underscore -
set filename=00000_AAA_132144_2012021.txt
set dirname=%filename:~10,6%
If the spacing may vary - you can do the following -
for /f "delims=_ tokens=3" %%a in ('echo %filename%') do set dirname=%%a
And yes, the script written for you seems to do essentially the same thing as what you're asking - I've edited it to do what you've asked -
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir /b /a-d *.txt') do (
set "filename=%%~i"
for /f "delims=_ tokens=3" %%a in ('echo !filename!') do set folder=%%a
mkdir "!folder!" 2>nul
move "!filename!" "!folder!"
)
This will move all *.txt documents to a folder created based on the third section of the text files name. Note that this will cause problems if you have .txt documents in the directory that do not follow the same naming standard.
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%\*_*_*_*.txt" ') DO (
FOR /f "tokens=3delims=_" %%m IN ("%%a") DO (
ECHO MD "%destdir%\%%m"
ECHO MOVE "%sourcedir%\%%a" "%destdir%\%%m\"
)
)
GOTO :EOF
Endoro's routine selects .pdf files, you've specified .txt
Find filenames matching the mask, find the third _-separated token in the name, make that directory and then move the file.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories and change ECHO MOVE to MOVE to actually move the files.
Append 2>nul to suppress error messages (eg. when the directory already exists)
Append >nul to suppress report messages (eg. 1 file moved)

Recursively Iterating through Directories in Batch

Here's the situation:
I have a folder with lots of sub-folders with pdf files. I want to make a batch script that goes through each sub-folder and zip the pdf files if there are over 100 of them (using 7Zip, not asking for help with that part).
This is my first time dealing with windows batch scripting and I am extremely discourage. I have spent hours on Google and I don't think I'm any wiser on the subject. I have found a lot of reference material and example code but not a whole lot of word by word breakdown on examples. I find the syntax to be extremely user unfriendly.
Anyways here's what I've got:
#echo off
for /r %%A in (.) do (
set pdfCount = "Code that gets the total number of pdf files in current directory, something like dir *.pdf?"
if pdfCount GEQ 100 (
set beginDate = "Code that gets the date of the oldest pdf, use in the zip file name"
set endDate = "Code that gets the date of the newest pdf, use in the zip file name"
"Use a 7Zip command to zip the files, I am not asking for help with this code"
DEL *.pdf
echo %pdfcount% files zipped in "Code for current directory"
)
)
pause
My understanding is that "for /r %%A in (.) do ()" is supposed to do execute the code in every sub-directory.
This script is locale dependent, meaning it depends on the way dates and times are formatted on your machine. My machine uses mm/dd/yyyy hh:mm am format. The script will create zip files with names in the form of PDF yyyy_mm_dd yyyy_mm_dd.7z.
#echo off
setlocal disableDelayedExpansion
for /r /d %%P in (*) do (
set "beg="
set /a cnt=0
pushd "%%P"
for /f "eol=: delims=" %%F in ('dir /b /a-d /od *.pdf 2^>nul') do (
set /a cnt+=1
set "end=%%~tF"
if not defined beg set "beg=%%~tF"
)
setlocal enableDelayedExpansion
if !cnt! gtr 100 (
for /f "tokens=1-6 delims=/ " %%A in ("!beg:~0,10! !end!") do (
7zip a "PDF %%C_%%A_%%B %%F_%%D_%%E.7z" *.pdf
del *.pdf
)
)
endlocal
popd
)
This may work. It's not destructive and atm just echoes the 7zip and parameters to the screen.
#echo off
for /f "delims=" %%a in ('dir /b /ad /s') do (
pushd "%%a"
for /f %%b in ('dir *.pdf /b ^|find /c /v "" ') do (
if %%b GTR 100 echo 7zip a "%%~nxa.7z" "*.pdf"
)
popd
)
It takes all the folders in the current directory tree, pushes the directory on the stack to make it current, uses dir and find to count the PDF files, and if the result is greater than 100 it will echo the line to the console. And popd pops the directory off the stack again. The 7z files will be created in the folder with the PDF files and they get the folders name.7z , unless you specify a location for them.

Resources