batch add leading zeroes filename - batch-file

I've got a script that I'm using for all kinds of things, including renaming and repacking comics. Variants of this script are used by others as well.
I've been seeing a limitation in repacking comics, however; I've found that some comics don't have the leading zeroes at their pages, which makes the pages appear out of order. So, I've added a part that should add leading zeroes.
The file does the following:
loop over all subfolders of the current folder
rename comic archives to their proper extension (so I can see where it is)
loop over all archives, and extract them to a temporary folder
loop over all files in the temporary folder, and add leading zeroes
repack it as a 7zip
rename the file to a comic book extension
Somehow, it's not renaming the files properly, and the name of the archive appears as a subfolder in the archive. For example:
let's repack 'testfolder', which contains images from 1 to 100. It renames, extracts, packs, and renames again, without problem. However, the new archive contains the folder named 'testfolder' in the archive next to the images, which don't have the leading zeroes. I'm not sure what's going on, and I've been fighting with it for a while now, so I thought to put it online (it's a good script to share, anyway). Does anyone have an idea on what's going wrong here?
#ECHO ON
rem mode con: cols=80 lines=60
for /f "delims=" %%F in ('dir /ad/s/b') do (
cd %%F
IF EXIST *.cbr (
RENAME *.cbr *.rar
)
IF EXIST *.cbz (
RENAME *.cbz *.zip
)
IF EXIST *.cb7 (
RENAME *.cb7 *.7z
)
FOR %%I IN (*.RAR, *.ZIP *.7Z) DO (
ECHO Extracting %%I...
"C:\Program Files\7-Zip\7z.exe" e "%%I" -oC:\TMPPACKDIR\* -y | FIND /V "ing "
echo %%~nI
cd C:\TMPPACKDIR\%%~nI\
FOR /f "delims=" %%P IN ('dir *.JPG, *.PNG, *.BMP') DO (
SET %%N = %%P
SET %%N = 00%%N
SET %%N = %%N:~-2%
echo %%P
echo %%N
pause
rename 'C:\TMPPACKDIR\%%~nI\%%P' %%N
)
pause
echo %%F
cd %%F
ECHO Repacking
"C:\Program Files\7-Zip\7z.exe" a -t7z "%%~nI.7z" "C:\TMPPACKDIR\%%~nI*" -mx=9 | FIND /V "ing "
IF %ERRORLEVEL% EQU 0 RD /S /Q C:\TMPPACKDIR
ECHO Renaming new file
RENAME *.7z *.CB7
ECHO Removing original file
DEL "%%I"
ECHO File %%I is done
)
)
REM del /f/q "%~0" | exit

Yes, the problem is in the code portion between cd C:\TMPPACKDIR\%%~nI\ and pause. You are trying to set a for variable reference %%N, which does not work. You need to use a normal environment variable instead, like NAME, for example; you can only do sub-string expansion (like ~-2 in your code) using normal environment variables. In addition, since you are setting and reading the same environment variable within a single block of code, you need to use delayed expansion; otherwise, you would always receive the value present when the entire block is read.
The code portion should look like this:
cd /D "C:\TMPPACKDIR\%%~nI"
for /F "delims=" %%P in ('dir /B *.JPG, *.PNG, *.BMP') do (
set "FILE=C:\TMPPACKDIR\%%~nI\%%P"
set "NAME=00%%~nP"
setlocal EnableDelayedExpansion
set "NAME=!NAME:~-2!"
rename "!FILE!" "!NAME!%%~xP"
endlocal
)
pause

I've removed some unnecessary stuff and made a few changes, (the main error being that which aschipfl has already identified).
FOR /F "DELIMS=" %%F IN ('DIR/AD/S/B') DO (
PUSHD "%%F"
IF EXIST *.cbr REN *.cbr *.rar
IF EXIST *.cbz REN *.cbz *.zip
IF EXIST *.cb7 REN *.cb7 *.7z
FOR %%I IN (*.RAR, *.ZIP *.7Z) DO (
ECHO Extracting %%I...
"%ProgramFiles%\7-Zip\7z.exe" e "%%I" -o"C:\TMPPACKDIR\*" -y
PUSHD "C:\TMPPACKDIR\%%~nI"
FOR %%P IN (*.JPG, *.PNG, *.BMP) DO (
SET "_N=100%%~nP"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_N=!_N:~-2!"
REN "%%P" "!_N!%%~xP"
ENDLOCAL
)
POPD
ECHO Repacking
"%ProgramFiles%\7-Zip\7z.exe" a -t7z "%%~nI.7z" "C:\TMPPACKDIR\%%~nI*" -mx=9
IF NOT ERRORLEVEL 1 RD/S/Q "C:\TMPPACKDIR\%%~nI"
ECHO Renaming new file
REN "%%~nI.7z" "%%~nI.CB7"
ECHO Removing original file
DEL "%%I"
ECHO File %%I is done
)
POPD
)
Things to look into:Can 7z.exe not just extract .cbr, .cbz & .cb7 directly without renaming them first. In the same way, when repacking once you've provided the file type, -t7z can the file not be given the name "%%~nI.CB7" directly instead of later renaming it.

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.

Compress separately files within subfolders

Hi all and thanks for the answers,
Firstly, I tried to find the answer to my problem but I did not find anything.
I have a tree of folders and sub-folders and I want to use 7zip to compress the files within those folders separately.
I have got this piece of code from this very website, it does what I want to get but it places the compressed files on the main folder:
set extension=.*
for /R %%a in (*%extension%) do "%sevenzip%" a -mx "%%~na.zip" "%%a"
I wonder if I can get a zip file of every file and have it in the sub-folder containing the source file. Or doing the process above and place every zip file inside the appropriate sub-folder.
I tried with a double 'For /d' but I was unable to get it:
cd /d %~dp0
rem 7z.exe path
set sevenzip=
if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%\7-zip\7z.exe" set
sevenzip=%ProgramFiles(x86)%\7-zip\7z.exe
if "%sevenzip%"=="" if exist "%ProgramFiles%\7-zip\7z.exe" set
sevenzip=%ProgramFiles%\7-zip\7z.exe
if "%sevenzip%"=="" echo 7-zip not found&pause&exit
for /D %%O in (*) do (
for /R %%I in ("%%O\*") do (
"%sevenzip%" a -mx "%%~na.zip" "%%a"
:: rd /s /q "%%I" **Because I do not want to delete anything by now.
)
)
Again, thank you.
Alex.
If you have somewhat complex folder structure then you probably better use plain list from dir:
dir /a:-d /s /b /o
Just use its output in for:
for /f %%f in ('dir /a:-d /s /b /o') do (
echo %%f <-- %%f is a full path to a file, do something with it
)
Btw, 7zip has useful option -sdel to remove the source file when archive has been created successfully.
This is the final code:
#echo off
cd /d %~dp0
rem 7z.exe path
set sevenzip=
if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%\7-zip\7z.exe" set sevenzip=%ProgramFiles(x86)%\7-zip\7z.exe
if "%sevenzip%"=="" if exist "%ProgramFiles%\7-zip\7z.exe" set sevenzip=%ProgramFiles%\7-zip\7z.exe
if "%sevenzip%"=="" echo 7-zip not found&pause&exit
#echo searching...
for /R %%I in (*) do (
"%sevenzip%" a -mx -mmt4 "%%I.7z" -r -x!*.bat "%%I"
)
del "Compressing_files_7zip.bat.7z"*
del *.7z.7z
del *.zip.7z
::::::::::::::::::::::::::For setting up shutdown 60' after the end of the process.Remove colons in the line below.
::shutdown.exe /s /t 3600
pause
Thanks all for the support, especially to Frost.
For a simpler version without using 7zip:
for /f %%f in ('dir /a:-d /s /b /o *.mdb') do (
zip -r -p "%%f.zip" "%%f"
)

Batch file that creates folder with wildcard in path

I want to write a batch file that creates a folder (if it does not exist) and copies a certain file into that folder. So far so good.
The problem is that one folder in the path varies slightly from time to time, so a wildcard becomes necessary.
The following code works just fine but obviously misses to create the folder (Reports). So if the folder is not there, it simply does nothing.
for /r "c:\Users\%USERNAME%\AppData\Local\Packages" &&G in ("LocalState\acn\Reports") do #if exist %%G xcopy /s /i /y c:\temp\Reporting "%%G"
The full path is:
c:\Users\FSchneider\AppData\Local\Packages\“WILDCARD"\LocalState\acn\Reports\
Any idea?
Add /d switch in for to indicate you're looking for a directory, not a file
Add * and omit quotes in the wildcard to indicate it's actually a wildcard
No need for if exist now
for /d /r "%LocalAppData%\Packages" %%G in (LocalState\acn.*) do xcopy /s /i /y c:\temp\Reporting "%%G\Reports"
Next script could help.
#ECHO OFF
SETLOCAL enableextensions
set "_fldrtop=%USERPROFILE%\AppData\Local\Packages"
set "_fldrsub=LocalState\acn"
if not "%~1"=="" set "_fldrsub=%~1" :: my testing data, remove this line
set "_fldrlow=Reports"
if not "%~2"=="" set "_fldrlow=%~2" :: my testing data, remove this line
for /F "delims=" %%G in ('dir /B /AD "%_fldrtop%"') do (
if exist "%_fldrtop%\%%G\%_fldrsub%\" (
if exist "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\" (
echo echo "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
) else (
echo md "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
rem echo xcopy /s /i /y c:\temp\Reporting "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
)
Output:
==>D:\bat\SO\31672436.bat
==>D:\bat\SO\31672436.bat "LocalState\Cache"
md "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\Reports\"
==>D:\bat\SO\31672436.bat "LocalState\Cache" 2
echo "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\2\"

batch file to find files with double file extensions and remove the last one

I have several folders that have files with double file extensions along with regular file extensions. I need to create a batch script to search all the folders and remove the last extension with any files that have double extensions. None of the file extensions are consistent.
Here's an example
C:\test\regular.exe
C:\test\picture.jpg.doc
C:\newtest\document.doc.pdf
End Result I need
C:\test\regular.exe
C:\test\picture.jpg
C:\newtest\document.doc
#ECHO OFF
SETLOCAL
SET sourcedir=c:\sourcedir
FOR /r "%sourcedir%" %%i IN (*.*) DO (
FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF NOT EXIST "%%~dpni" ECHO REN "%%~fi" "%%~ni"
FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%%~dpni" ECHO CAN NOT REN "%%~fi" "%%~ni"
)
GOTO :EOF
This batch should accomplish the task.
For each file in the tree rooted at sourcedir, if the NAME of the file itself contains an 'extension' and the filename without the original extension does not exist, then rename the file. That way, if ...picture.jpg.doc is found, the rename should occur only if ...picture.jpg does not exist.
The command to rename is simply ECHOed. You'd need to remove the ECHO keyword to activate the rename - after verifying that's what you want to do.
I've added a second line to report that a rename could not be done because of an existing file.. This could be done very slightly better, but it will work.
Revised to modify name in case simple rename can not be done.
Caution - this version will rename immediately - there are no ECHOes to provide a list first because it's nonsense to provide such a list when renaming a file may produce different results on the main rename run.
#ECHO OFF
SETLOCAL
SET sourcedir=c:\sourcedir
FOR /r "%sourcedir%" %%i IN (*.*) DO (
FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%%~dpni" (
SET renreq=Y
FOR %%a IN (new alt extra another 1 2 3 4 5 6 7 8 9) DO IF DEFINED renreq (
IF NOT EXIST "%%~dpi%%~nn_%%a%%~xn" (
REN "%%~fi" "%%~nn_%%a%%~xn"
SET "renreq="
)
)
IF DEFINED renreq ECHO CAN NOT REN "%%~fi"
) ELSE (
REN "%%~fi" "%%~ni"
)
)
GOTO :EOF
Reasonably obviously, the list of "extras" can be extended if required.
Try this and remove the echo, if the output is OK:
#echo off &setlocal
for /r \ %%i in (*) do (
for %%j in ("%%~ni") do if "%%~xj" neq "" echo ren "%%~fi" "%%~nj"
)
Edit: added support for the entire HD.

How to copy files from a random folder into a destination folder in batch?

I need a batch script to copy files from a random subfolder of a specific directory into a destination directory.
For example, there will be a number of files in their own subdirectory, for example
.../source/a/file.txt
.../source/b/file.txt
So there a number of these files, and I would like to randomly select one of them and have it copied into the new directory
.../destination/file.txt
So the file.txt in the destination is just being overwritten with other files that have the same name but different content.
I'm new to batch scripting and I can't quite figure out how to select each file from a random subfolder. I'd also like it to repeat every 30 seconds until I terminate the script, but I think it should be easy enough to just make a second script that calls this .bat file every 30 seconds once I get it going.
Thanks!
This can do what you request. Just set your source directory, destination directory, and your file name filter.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "...\source\"
:: Enumerate Files. Method 1
set "xCount=0"
for /r %%A in (file.txt) do if exist "%%~A" set /a "xCount+=1"
echo %xCount%
:: Select a Random File.
set /a "xIndex=%Random% %% %xCount%"
echo %xIndex%
:: Find an Copy that File. Method 1
set "xTally=0"
for /r %%A in (file.txt) do if exist "%%~A" (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
:End
popd
endlocal
pause
Type xcopy /? to see all of its options.
Here are some alternate loop methodologies for the file enumeration.
:: Enumerate Files. Method 2
set "xCount=0"
for /f %%A in ('dir *.txt /a:-d /s ^| find "File(s)"') do set "xCount=%%~A"
echo %xCount%
:: Find an Copy that File. Method 2
set "xTally=0"
for /f "delims=" %%A in ('dir *.txt /a:-d /b /s') do (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
Enjoy :)
#echo off
setlocal EnableDelayedExpansion
rem Enter into the directory that contain the folders
pushd \Fullpath\source
rem Create an array with all folders
set i=0
for /D %%a in (*) do (
set /A i+=1
set folder[!i!]=%%a
)
rem Randomly select one folder
set /A index=(%random%*i)/32768 + 1
rem Copy the desired file
copy "!folder[%index%]!\file.txt" "\Fullpath\destination" /Y
rem And return to original directory
popd
The features of batch script is
It copies all files from source to destination folder with similar structure(even retains empty folders).
Can retain N number of days recent files in Archive folder(source) remaining files will be moved to backup folder(destination).
Can be scheduled to N number of days to N number of years.
Can be used in any Source to destination folder backup.
Source folder can add N number of folder any time and delete folder or files, but Destination folder always adds the folder and never deletes any folder or file.
#echo off
Set "sourcefolder=E:\Interfaces"
Set "destinationfolder=E:\BackupInterface"
If Exist %sourcefolder% (
For /F %%* In ('Dir /b /aD "%sourcefolder%" 2^>nul') do (If Not Exist "%destinationfolder%\%%*" ( RD /S /Q "%destinationfolder%\%%*")
xcopy /e /v /i /y /q "%sourcefolder%\%%*" "%destinationfolder%\%%*"
forfiles /p "%sourcefolder%\%%*" /s /d -30 /c "cmd /c del /Q /S #file" )
) Else (echo.Source folder could not be found)
:end of batch
echo.&echo.finished!

Resources