batch file to delete files and folders except zip files - batch-file

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"
)

Related

If zip file and folder name are identical, copy zip file into folder

I have many .zip files and folders some of them have identical names.
I will check zip file name and folder name if it is identical.
I will copy zip file into the folder and delete the zip file.
Is it possible to Automate the process?
I have found following code but couldn't edit it:
#echo off
setlocal EnableDelayedExpansion
pushd "C:\New folder"
FOR %%G IN (*.zip DO (
FOR /F "tokens=1 delims= " %%a IN ("%%G") do (
set "outFolder=%%a Random Center"
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
It creates Random Center folder and copy all the zip files in it which I don't want.
#echo off
setlocal
pushd "C:\New folder" || exit /b 1
for %%A in (*.zip) do if exist "%%~nA" (
pushd "%%~nA" && (
move /y "..\%%~nxA"
popd
)
)
popd
pause
Using move as see no need to
copy and del the zip file.
Argument /y will automatically
overwrite an existing file without
prompt.
Initial pushd changes the current directory
else exits with errorlevel 1.
The for loop iterates through each zip filename.
It checks if the name of the filename, without extension,
does exist, which is expected to be a folder.
If does exist, pushd into the directory, and then
move the zip file from the parent directory into
the current directory. popd will restore to the
previous directory.
Does final popd to restore to initial current directory.

zip file with created date

Could you please suggest me on how to zip file under folder in it's sub folder and rename the file with it's created date using batch script then delete the original one. It should be able to make the number of day to archive configurable. For instance if i want to archive any files older than 7 days, it should zip up the file older than 7 days. And also make delete file configurable as well.
For instance, file is abc.log created on 9/14/2016 it will be zip and rename as abc.20160914.zip
I search through forum but not found. Really appreciate for your help.
I find the answer for this
#echo off
setlocal enabledelayedexpansion
set Archive=D:\scripts\test\Archive
set Temp=D:\scripts\test\Temp
set DaytoDelete=7
REM Moving file older than %DaytoDelete% days to TEMP folder
for /f "tokens=*" %%G in (D:\scripts\test\Location.txt) do (
forfiles -p "%%G" -s -m *.log -d -%DaytoDelete% -c "cmd /c move #path %TEMP%"
)
if not exist "%Archive%" (md "%Archive%")
if not exist "%Temp%" (md "%Temp%")
REM Zip file in %Temp% and rename zip file with creation date
for %%a in ("%Temp%\*.log") do (
echo Processing %%~nxa ...
set File=%%~fa
set Archive=D:\scripts\test\Archive
for /f "tokens=1* delims=," %%a in ('wmic datafile where "name='!File:\=\\!'" get 'CreationDate' /format:csv ^| find /i "%ComputerName%"') do (set CreationDate=%%b)
echo %%~nxa: !CreationDate!
set cYear=!CreationDate:~0,4!
set cMonth=!CreationDate:~4,2!
set cDay=!CreationDate:~6,2!
set FileTime=!cYear!!cMonth!!cDay!
REM zip "%Archive%\temp.zip" %%~fa Issue with zip command, it zips whole full path of file
"C:\Program Files\7-Zip\7z.exe" a -tzip "%Archive%\temp.zip" %%~fa
ren %Archive%\temp.zip %%~na.!FileTime!.zip
)
REM Delete file after zipping
DEL /F /S /Q %Temp%\*.*
pause

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"
)

How to zip all sub folders inside folder with a specific name

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
)
)

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