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
)
)
Related
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 need a batch file to zip files inside a folder with a specific file type. The file type is (.asc).
Could anyone help me correct the code I have below?
Thanks.
for /r "C:\Test_Results\Report_%Date:~-10,2%_%Date:~-7,2%_%Date:~-4,4%" %%G in (.asc*)do (
if /i "%%~nxa"==".asc" (
pushd "%%a"
REM zip all files in the backup directory
FOR %%A IN (*.asc* *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.rar" "%%A"
FOR %%A IN (*.asc *.cpi) DO DEL "%%A"
popd
)
)
The code below zips files inside any folder called Logfile. This works well but I now need to do something a little different (as mentioned in the first post).
#J.Baoby, I have changed the .asc* to *.asc, but I'm still having issues with zipping the files. I'm actually not sure what the IF statement after the FOR loop is doing. Is it needed here?
#echo 1. This step zips all files inside any folder with the name Logfile under C:\TestReports\Report_today's date.
for /d /r "C:\TestReports\Report_%Date:~-10,2%_%Date:~-7,2%_%Date:~-4,4%" %%a in (Logfile*) do (
if /i "%%~nxa"=="Logfile" (
pushd "%%a"
REM zip all files in the backup directory
FOR %%A IN (*.blf* *.cpi*) DO "C:\Program Files\WinRAR\WinRAR.exe" a -r "%%~nA.zip" "%%A"
FOR %%A IN (*.blf *.cpi) DO DEL "%%A"
popd
)
)
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"
)
I am a php developer and I know nothing about windows batch files.
Maybe somebody will help me...
I have a directory structure like here:
directory1/1/
directory1/2/
directory1/5/
directory2/1/
directory2/2/
directory2/5/
etc...
How can I write a bat file that will do the following:
-move all files from all sub-directories '1' into a different directory
I had tried this
pushd %CD%\in\
for /r %%a in (*.*) do (
echo COPY "%%a" "%CD%\out\%%~nxa"
)
popd
but this code takes all the files from "in" folder and copies them to "out" folder. How can I determine subdirectories here?
You can do it with this batch command:
#echo off
::Set Directory
set Dir=%CD%\in
::Set Destination
set Des=%CD%\out
::Set Sub Directory you want to move
set SubDir=1
if exist "%Dir%\*" (
if not exist "%Des%\*" mkdir "%Des%"
for /D %%a in ("%Dir%\*.*") do (
for /D %%b in ("%%a\*.*") do (
if "%%~nxb" EQU "%SubDir%" ROBOCOPY "%%b" "%Des%\%%~nxa\%%~nxb" /E /IS /MOVE>Nul
)
)
)
I've created a batch file which unzips all files
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM
REM Remove the double quotes from the front and end of the root path
REM
SET ROOT=%1
SET ROOT=%ROOT:~1%
SET ROOT=%ROOT:~0,-1%
ECHO %ROOT%
REM Searching directory structure from root for subfolders and zipfiles,
REM then extracting the zipfiles into a subfolder of the same name as the zipfile.
FOR /F "delims==" %%d IN ('dir /ogne /ad /b /s "%ROOT%"') DO (
ECHO Extracting : "%%d"
FOR /F "delims==" %%f IN ('dir /b "%%d\*.zip"') DO (
REM Getting filename without extension.
SET subfolder=%%~nf
ECHO mkdir "%%d\!subfolder!"
mkdir "%%d\!subfolder!"
REM Extracting zipfile content to the newly created folder.
ECHO 7z x "%%d\%%f" -o "%%d\!subfolder!"
"C:\Program Files\7-Zip\7z.exe" x "%%d\%%f" -o"%%d\!subfolder!"
)
)
ENDLOCAL
Now I want to delete the files which has been created.
Can anyone help in deleting the unzipped files alone and not the zipped files and also care should be taken that it should not delete the root and the zip file.
Any other solution is also appreciated.
I want the zip files only within the folders. All other files can be deleted without user saying Y/N.
Try this and remove the echos if the output is OK:
for /r "%root%" %%i in (*.zip) do (
echo del /f /q "%%~dpni\*.*"
echo rd /q "%%~dpni"
)