Copy first file for multiple folders to some another location - batch-file

I have about 1000 folders on my hard drive. Each folder contains about 100 .jpg files. Now I need to copy from each folder "page (1).jpg" file in to some specific folder(each folder contain "page (1).jpg" file). And one more thing... On the end, after copying, each .jpg file from specific folder need to have name like folder from which it was copied.
How to do this on easiest way. Maybe with some batch file or something else...

not tested (you need to change the names of root_dir and target_dir):
#echo off
set "target_dir=E:\scriptests\redirection"
set "root_dir=E:\scriptests\labels"
pushd %root_dir%
setlocal enableDelayedExpansion
for /f "tokens=* delims=" %%a in ('dir /b /s "page ?1?.jpg"') do (
set "fdir=%%~dpa"
set stripped=!fdir:~0,-1!
for %%# in ("!stripped!") do (
echo %%~n#
set "dir_name=%%~n#"
)
copy /y "%%~fa" "%target_dir%\!dir_name!.jpg" >nul 2>nul
)
endlocal
popd
pause

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.

Copy a file from one directory and replace the file in multiple directory

I need to copy a jar file from directory(source) and replace the file in the destination. But the problem is my destination directories are different as explained below:
Source=D:\temp\R56A
Target=D:\path\AP\Different_folders\lib\i2
Target folder example:-
D:\path\AP\ABC1\lib\i2
D:\path\AP\XY_C\lib\i2
D:\path\AP\GHS3\lib\i2
I AM NOT ABLE TO FETCH THROUGH DIFFERENT FOLDER NAMES and the script not taking it.
This is for a windows box. Can we copy the folder name in a text file and call that text file as variable in a for loop? Is it possible?
#ECHO OFF
REM SETLOCAL ENABLEDELAYEDEXPANSION
set Source=D:\temp\R56A
set Target=D:\path\AP\<Different_Directory_names>\lib\i2
set file=i2-bam.jar
for /f "delims=" %%f in ('dir /a-d /b /s "%Source%\%file%"') do (
copy /V "%%f" "%Target%\" 2>nul
)
PART 2
#ECHO OFF
for /d "D:\temp\R56A\" %%f in (i2-bam.jar) do copy %%f "D:\path\AP\<Different_Directory_names>\lib\i2"
Is this what you're trying to do?
#Echo Off
Set "Source=D:\temp\R56A"
Set "File=i2-bam.jar"
Set "Target=D:\path\AP"
Set "Sub=lib\i2"
If Not Exist "%Source%\%File%" Exit /B
If Not Exist "%Target%\" Exit /B
For /D %%A In ("%Target%\*")Do If Exist "%%A\%Sub%\" Copy /Y "%Source%\%File%" "%%A\%Sub%">Nul

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.

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

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

List ONE level subfolder content on external hard disc (PC batch)

I'm an enthusiast photographer.
In my PC (windows 7) I would like to let a batch file list my external hard disc content to a file (dir names only, not file names).
But I like to list the root folders and ONLY ONE LEVEL subfolders, preferably in a format like one folder and its subfolders, then the next folder and its subfolders etc.
Thanks
#echo off
set "drive=d:"
for /d %%r in ("%drive%\*") do (
echo %%~fr
for /d %%f in ("%%r\*") do echo( %%~nxf
echo(
)
For each folder in the root of the drive, show the name of the folder and for each folder inside it, show its name
try this:
setlocal
set basedir=p:\
FOR /f %%g IN ('dir /B /AD %basedir%') do call :doOneFolder %%g
:doOneFolder foldername
echo %1
FOR /f %%g IN ('dir /B /AD %basedir%%1') do echo %%g
change the basedir to something you need

Resources