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"
)
Related
I'm doing some BATCH scripting looping through files to copy. But I came to a problem where I need the path relative to the current .bat execution folder (%cd%)
So if I have files like this:
c:\games\batchTest\test.bat
c:\games\batchTest\subFolder1\test1.txt
How can I get just "subFolder1\test1.txt" so I can copy the file with the sub folder?
My current code:
for /r %%a in (*) do (
echo "%%a"
)
You can try this:
#Echo Off
Setlocal enabledelayedexpansion
For /r %%a In (*) Do (
Set p="%%a"
Echo !p:%__CD__%=!
)
The for /R loop always returns absolute paths, even if the (optional) given root directory behind /R is relative.
A possible way to get relative paths is to (mis-)use the xcopy command together with its /L option that prevents anything to be copied:
xcopy /L /S /I ".\*.*" "%TEMP%"
To remove the summary line # File(s) apply a filter using find using a pipe:
xcopy /L /S /I ".\*.*" "%TEMP%" | find ".\"
To process the returned items use a for /F loop:
for /F "eol=| delims=" %%F in ('
xcopy /L /S /I ".\*.*" "%TEMP%" ^| find ".\"
') do (
echo Processing file "%%F"...
)
If you just want to copy files including the sub-directory structure you do not even need the above stuff with for loops, you can simply use xcopy:
xcopy /S /I "D:\Source\*.*" "D:\Destination"
or robocopy:
robocopy /S "D:\Source" "D:\Destination" "*.*"
I created a batch file that allow to move a file from a folder to another.
My issue is that my bat file should also browse source subfolders in order to find files that have a specific pattern.
Actually :
#ECHO ON
SET SourceDir=C:\Users\me\Documents\source
SET CopyDir=C:\Users\me\Documents\repository
SET FilePatterName=*pattern*.pdf
FOR %%A IN ("%SourceDir%\%FilePatterName%") DO (
ECHO F | XCOPY /Y /F "%%~A" "%CopyDir%\"
DEL /Q /F "%%~A"
)
GOTO EOF
For example : in my source folder, if i have sub1, sub2, sub3 folders and a sub1-1 folder in sub1, i would like to check each folders, check the files and move them without creating any folder in the repository
As per my comment, use for /r which will recurse through the directories:
for /R "%SourceDir%" %%A in ("%FilePatterName%") do...
if it feels too ugly then first pushd to the directory, then recursively search from there:
#Echo off
Set "SourceDir=C:\Users\me\Documents\source"
Set "CopyDir=C:\Users\me\Documents\repository"
Set FilePatterName=*pattern*.pdf
Pushd "%SourceDir%"
For /R %%a in ("%FilePatterName%") do (
Echo F | Xcopy /Y /F "%%~a" "%CopyDir%\"
Del /Q /F "%%~a"
)
Popd
Here's an alternative, using the same structure but robocopy instead of xcopy and del:
#Set "SourceDir=%UserProfile%\Documents\source"
#Set "CopyDir=%UserProfile%\Documents\repository"
#Set "FilePatterName=*pattern*.pdf"
#If Exist "%SourceDir%\" For /R "%SourceDir%" %%# In ("%FilePatterName%")Do #"%__AppDir__%Robocopy.exe" "%%~dp#." "%CopyDir%" "%%~nx#" /Mov>NUL 2>&1
If you really needed to see the filenames, I suppose you could include additional RoboCopy options like /FP, /NDL, /NS, /NC, /NJH and /NJS.
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.
I am looking to find all folders with the name "Logfile" inside slightly different folder structures. For example how would I find the Logfile folder inside C:\ECU\ECU1\Logfile, C:\ECU\ECU2\Logfile, C:\ECU\ECU3\Logfile and C:\ECU\ECU4\Logfile? I then want to zip the .txt contents of this folder in each case. I currently have a batch file running which allows me to zip the contents of a folder which has the same folder structure each time but need to combine the above all together. Any help would be great...
Thanks.
#echo off
pushd "C:\ECU\ECU2" || goto :eof
REM zip all files in the backup directory
FOR %%A IN (*.TXT*, *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT,*.cpi) DO DEL "C:\ECU\ECU2.cpi*" "%%A"
popd
Try this as a test first to see if it prints the correct folders:
#echo off
for /d /r "c:\ecu" %%a in (target*) do (
if /i "%%~nxa"=="target" (
echo "%%a"
)
)
pause
and if it's ok then this should work - test it with dummy files, but your DEL command is odd in the first term. I replaced it with what might work.
#echo off
for /d /r "c:\ecu" %%a in (target*) do (
if /i "%%~nxa"=="target" (
pushd "%%a"
REM zip all files in the backup directory
FOR %%A IN (*.TXT* *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.TXT *.cpi) DO DEL "%%A"
popd
)
)
I have the folder and file structure as given below. I am in need of a MS DOS batch file to rename the files in multiple folders. Can anyone please help out?
- Main Folder
-->Sub Folder1
--- File1_EN.txt
--- File2_EN.txt
--> Sub Folder2
--- File3_EN.txt
--- File4_EN.txt
I want to rename the suffix "EN" in file names to "ENU".
#echo off
for /D %%d in (*) do (
ren "%%d\File*_EN.txt" "File*_ENU.txt"
)
You can do it by this way:
#Echo OFF
Set "Folder=C:\Users\Administrador\Desktop\Nueva carpeta"
Set "Suffix=_EN"
Set "Replace=_ENU"
Set "RegEx=\".*%Suffix%\"$"
FOR /R "%Folder%" %%# in ("*") DO (
(Echo "%%~n#"| FINDSTR /I "%RegEx%" 1>NUL) && (
Set "NewFileName=%%~nx#"
Call Set "NewFileName=%%NewFileName:%Suffix%=%Replace%%%"
Call Echo [+] Renaming: "%%~nx#" "%%NewFileName%%"
Ren "%%#" "%%NewFileName%%"
)
)
Pause&Exit
The Findstr is to ensure the matched string is a suffix, is better than doing a substring or splitting the filename from "_" character to the right.
Try this:
ren folder1\file*.txt file*_enu.txt
ren folder2\file*.txt file*_enu.txt
If you want all child folders to be changed use:
for /f "delims=*" %a in ('dir File*_EN.txt /b /s') do ren "%a" File*_ENU.txt