Fix last-but-one space-separated words to move files - batch-file

I try to move files making with a script their respective folders.Example of desidered and wrong output
To set a substring I use
for %%I in (*.cbr) do (
set "file=%%I"
for %%J in ("!file: =\!") do set "folder=!file: %%~nxJ=!"
For example if I have this filenames
the punisher one.cbr
the punisher two.cbr
the folder name will be the punisher
Problem is when I try to move with another example. If I have 3 files
the punisher beta.cbr
the punisher beta new.cbr
the punisher past.cbr
and I use this command to make directories
mkdir "!folder!" 2>NUL
move /Y "!file!" "!folder!"
But with this example is where I fail and I get unwanted outcome because it creates 3 folders instead of only one (it wrongly also move all files when should be move only 2 files in one folder)
Where is the mistake? I don't understand if is here
for %%J in ("!file: =\!") do set "folder=!file: %%~nxJ=!"
or here
mkdir "!folder!" 2>NUL
move /Y "!file!" "!folder!"
Entire code example is
#echo off
setlocal EnableDelayedExpansion
for %%I in (*.pdf) do (
set "file=%%I"
for %%J in ("!file: =\!") do set "folder=!file: %%~nxJ=!"
mkdir "!folder!" 2>NUL
move /Y "!file!" "!folder!"
echo "!file!" moved to "!folder!"
)

Related

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.

Batch File: move/organize files after image conversion

I'm working on a batch script for drag and drop function for a folder which contains image files prepared for conversion. And simply I'm looking for a help with merging those two scripts below:
cls
#pushd %~dp0
:::::::::::::::::::
#echo off
set exepath="\\output\_REPO\TOOLS\IMAGE_TOOLS\ImageConvert\IMAGECONV_x64.exe"
set rename=-r
for /r %%i in (*.tga) do %exepath% "%%i"
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b /a-d *_converted.tga') do (
set "imagefilename=%%~i"
set "folder=output"
mkdir "!folder!" 2>nul
move /y "!imagefilename!" "!folder!" >nul
)
endlocal
:::::::::::::::::::
#popd
pause
After image conversion I'd like to move those converted files (*_converted.tga) to a new folder named "output" which is located in the same directory as the non converted files.
files_for_conversion (here are the files before conversion)
|
|
|_output (here I want to move files after conversion)
Script above only evaluates to a point where image files are converted and then it returns "file not found". When I split it into two parts it works like it should. Second script is evaluated from "output" folder and does the job.
cls
#pushd %~dp0
:::::::::::::::::::
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b /a-d *_converted.tga') do (
set "imagefilename=%%~i"
set "folder=output"
mkdir "!folder!" 2>nul
move /y "!imagefilename!" "!folder!" >nul
)
endlocal
:::::::::::::::::::
#popd
pause
So It'll be nice to have it done in an one step solution rather than two steps.
I'm trying to wrap my head around it but apparently batch commands are not my thing. Any help and advice appreciate it.
Provided the *_converted.tga files are stored in the current folder this might do:
:: Q:\Test\2018\04\13\SO_49813044.cmd
#echo off
set exepath="\\output\_REPO\TOOLS\IMAGE_TOOLS\ImageConvert\IMAGECONV_x64.exe"
set rename=-r
for /r "%~dp0" %%i in (*.tga) do (
mkdir "%%~dpi\output" 2>nul
pushd "%%~dpi\output"
%exepath% "%%~i"
popd
)
endlocal
pause

Move files to parent directory if their extension matches the sub-directory extension

I am looking for a batch script which will move files from a sub-directory to its parent if their extension matches the sub-directory extension.
Examples:
Move any .txt file from directory parent\files.txt\
"parent\files.txt\test.txt" will become "parent\test.txt"
Move any .zip file from directory parent\files.zip\
"parent\files.zip\test.zip" will become "parent\test.zip"
I want only to move the file if its extension is the same as that of its sub-directory name. The sub-directory and any other content has to be left alone.
This is what I have now, but it only removes my files:
#echo off
md temp
set view=
if "%view%"=="1" #echo on
color 72
mode con cols=30 lines=8
setlocal enableDelayedExpansion
set /p location_with_dirs=location:
echo:
type nul> ".\temp\folderlist.txt"
FOR /D %%G IN ("%location_with_dirs%\*") DO (
echo process file:
echo %%G
choice
if "%errorlevel%"=="1" echo %%G >> ".\temp\folderlist.txt"
cls
)
FOR /f "delims=" %%G in (".\temp\folderlist.txt") DO (
for %%F in (%%G\*.*) do move /Y %%F "%location_with_dirs%\"
rd %%G
)
del /f /q ".\temp\folderlist\*.txt"
I'm sure there will be many ways of achieving this, here's one:
#CD /D "Parent" 2>Nul || Exit /B
#For /D %%A In (*) Do #If /I Not "%%~xA"=="" Move /-Y "%%A\*%%~xA">Nul
Just change Parent on line 1 to the full or relative path of your parent directory.
Just as a courtesy, heres a version which hopefully improves on the more important part of your provided code, (it ignores the progress bar and color stuff):
#Echo Off
:AskDir
Set "dir_with_files="
Set /P "dir_with_files=location: "
If "%dir_with_files%"=="" GoTo AskDir
If "%dir_with_files:~-1%"=="\" Set "dir_with_files=%dir_with_files:~,-1%"
If Not Exist "%dir_with_files%\" GoTo :AskDir
Set "_rand=%dir_with_files%\%random%.organizer.lock.txt"
Type Nul>"%_rand%" 2>Nul && (Del "%_rand%") || GoTo :AskDir
If "%dir_with_files:~-1%"==":" Set "dir_with_files=%dir_with_files%\"
CD /D "%dir_with_files%" 2>Nul || Exit /B
For /D %%A In (*) Do If /I Not "%%~xA"=="" Move /-Y "%%A\*%%~xA">Nul 2>&1
Exit /B.

recursive .bat file to apply jpegtran and pngout to all images in subfolders

I have this script to optimize images inside a directory, I need to make this recursive in order to optimize all images in subdirectories, leaving filenames and folder structure unchanged.. Anyone can help to modify this code in order to modify images in subdirectories too?
#echo none
cd %1
md "%~1\OptimizedJPEGS"
for %%i in (*.jpg) do "C:\imageoptimization\jpegtran.exe" -optimize -progressive -copy none "%%i" "%~1\OptimizedJPEGS\%%i"
move /Y "%~1\OptimizedJPEGS\*.*" "%~1"
rd "%~1\OptimizedJPEGS"
for %%i in (*.png) do "C:\imageoptimization\pngout.exe" "%%i"
This should work when launched in the main folder of the image tree.
Test it on some sample folders first.
#echo none
for /d /r %%a in (*) do (
pushd "%%a"
echo processing "%%a"
md "OptimizedJPEGS"
for %%i in (*.jpg) do "C:\imageoptimization\jpegtran.exe" -optimize -progressive -copy none "%%i" "OptimizedJPEGS\%%i"
move /Y "OptimizedJPEGS\*.*" .
rd "OptimizedJPEGS"
for %%i in (*.png) do "C:\imageoptimization\pngout.exe" "%%i"
popd
)

Batch to copy a file to a folder and only ONE subfolder deep

I have this batch file now that copies example.jpg from C:\Temp to the Test-folder and all its subfolders.
I would like it to copy the file only 1 subfolder deep in the Test-folder.
For example copy the picture to Test\subfolder but NOT to Test\subfolder\subfolder2
#echo off
for /r "C:\Temp\Test" %%f in (.) do (
copy "C:\Temp\example.jpg" "%%~ff" > nul
)
PAUSE
Don't use recursion if you want to go only 1 level deep. Try this instead:
#echo off
set src=C:\Temp\example.jpg
set dst=C:\Temp\Test
copy "%src%" "%dst%" >nul
for /d %%d in ("%dst%\*") do (
copy "%src%" "%%~fd" >nul
)
#ECHO OFF
SETLOCAL
SET destroot=c:\temp
FOR /f "delims=" %%i IN ( ' dir /ad/b "%destroot%"' ) DO ECHO COPY "c:\temp\example.jpg" "%destroot%\%%i\"
Simply shows what the batch PROPOSES to do. Remove the ECHO keyword to activate the copy and add >nul to suppress the "copied" message

Resources