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"
)
Related
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.
I need to sort all the sub-folders in a directory and print the first folder that contains a file, i tried using
FOR /F "delims=" %%i IN ('dir "directorypath" /b /ad-h /t:c /o-d') DO SET a=%%i
IF EXIST a\*.exe
echo folder %a%
in line 1 i tried sorting the folders in the directory based on date modified
and then searched for .exe in each folder and print the latest folder which contains an exe.
Thanks for help in advance.
Based purely on your question without response to my comment, I'm going to assume you wish to do something like this:
#Echo Off
Set "dP=directorypath"
Set "dN="
For /F "Delims=" %%A In ('Dir/B/AD-S-L-H/OD/TC "%dP%" 2^>Nul'
) Do If Exist "%dP%\%%A\*.exe" Set "dN=%%A"
If defined dN Echo %dN%
Pause
Just modify directorypath on line two to suit your full or relative source directory.
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
I'm a bit of a novice when it comes to .BAT files. Please, I'm in need of a batch file that will create folders based on the names of files in the local folder, and then move those files into their corresponding folders. The folder names need to be determined by the characters before the delimiter in the file names, in this case an underscore.
For example, it would take these files
june_jahdfjlkaluie2.xlsx
june_jahdfjlkaluie.xlsx
august_nnnvbcnkziery2.xlsx
august_nnnvbcnkziery.xlsx
december_bagjd_kasdgf.xlsx
december_byuueyiuyoi.xlsx
Create these folders
june
august
december
And move the files into those folders based on the characters before the underscore.
This is the code I have so far
#echo off &setlocal
for /f "delims=_" %%i in ('dir /b /a-d *.xls') do (
set "file=%%~i"
setlocal enabledelayedexpansion
set "folder=!file!"
mkdir "!folder!" 2>nul
move "!file!" "!folder!" >nul
)
endlocal
echo Did it work?
Pause
The batch file is able to make the folders based on the file names. However, when it attempts to move the files, it produces an error stating 'The process cannot access the file because it is being used by another process.' I've attempted several fixes, including creating a separate for command for moving the files, and nothing seems to work.
Any advice is greatly appreciated. Thank you.
#echo OFF
SETLOCAL
for /f "delims=_" %%i in ('dir /b /a-d *_*.xlsx') do (
mkdir "%%i" 2>nul
move "%%i_*.xlsx" "%%i" >NUL 2>nul
)
echo Did it work?
GOTO :EOF
would likely work and be easier.
Note that I've changed the target filemask to ….xlsX because the short filename for a .xslx file does not necessarily retain the *_*.xls format.
I have an old archive system that has died and want to create a batch file to put pdf files into a folder based on part of the file name. There are 1000's of files in one directory, example file name abc12345620110101.pdf.
I need the batch file to create a folder based on the 4th to 9th character "123456" (accountnumber) from the example above and then place the file in that folder.
X:\\123456\\abc12345620110101.pdf
I will have multiple files that will go into the same folders so the batch needs to work even when the folder already exists.
Thanks in advance.
P.S. I have tried the following:
setlocal enabledelayedexpansion
for /f %%i in ('dir /b *.PDF') do (
set filename1=%%i
set folder1=!filename1:~4,20!
mkdir !folder1!
but I had no luck and I am open to any batch that will work.
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.PDF') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~3,9!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!"
endlocal
)