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
Related
I've previously asked the following question and was helped with a great solution.
How to compress all files with exception of newest file in each subfolder of a folder into one ZIP file per subfolder?
Now at the end of the year I would like to modify this solution so that it zips any remaining files from last year into the existing zip file.
The directory structure is like this:
Directory-Parent
---Sub-Directory-1
--------File1
--------File2
--------File3
---Sub-Directory-2
--------File1
--------File2
As mentioned and referred to in my previous question, I have a batch at the Directory-Parent level that will create a zip in each sub-directory of all the files except the latest file (or amount I specify). It also adds the year to the end of the zip file name.
So the result is:
Directory-Parent
-Sub-Directory-1
--------File1
--------Sub-Directory-12019.zip
---Sub-Directory-2
--------File1
--------Sub-Directory-22019.zip
The following is the batch file solution I have from my previous question. It zips all the files in each sub-directory (ignoring folders that I specify) into a zip file with the name of the directory appended by the current year.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FilesToIgnore=2"
set "theYear=%DATE:~-4%"
set "ListFile=%Temp%\%~n0.lst"
set "Move2=-sdel" & rem // (set this to `-sdel` if you want the files to be moved into the archive)
set "FoldersToExclude=FoldersToExclude.txt"
del "%ListFile%" 2>nul
REM for /D %%I in (*) do call :CompressFiles "%%I"
for /F "eol=| delims=" %%I in ('dir /AD-H /B /ON 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /R /V /X /G:"%FoldersToExclude%"') do call :CompressFiles "%%I"
goto EndBatch
:CompressFiles
pushd %1
set "ZipFile=%~nx1%theYear%.zip"
for /F "skip=%FilesToIgnore% eol=| delims=" %%J in ('dir /A-D /B /O-D /TW 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /L /V /X /C:"%ZipFile%"') do >>"%ListFile%" echo %%J
if exist "%ListFile%" (
echo Compressing files in directory %1 ...
echo 7z a %Move2% -bso0 -i"#%ListFile%" -mx5 -scsDOS "%ZipFile%"
7z a %Move2% -bso0 -i"#%ListFile%" -mx5 -scsDOS "%ZipFile%"
del "%ListFile%"
)
popd
goto :EOF
:EndBatch
pause
endlocal
I cannot figure out how to modify this to instead zip all files with a specified (or previous) year.
I believe the answer is in the second for loop in the dir command. Is there a way in the dir command to filter by date.
Is there a way to use forfiles command instead with #fdate?
Thank you for your help.
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 am looking for a simple batch or vbs script I can edit to complete the following tasks.
Search a folder (including sub dirs) for *.rar files.
Extract the *.rar found to a specific drive i.e E:/ or F:/ (I can change the file for this)
The twist, is the script must rename the extracted file to the directory name.
i.e
C:\Documents\Shop1_A\file.rar
Inside file.rar there is a file.pdf
I require the script to extracted the file.rar to a drive and rename the extracted file to E:\Shop1_A.pdf
There will only ever be 1 file in the archive (no duplicate or overwrite errors)
set "sourceDir=c:\someware"
set "targetDir=f:\"
set "unrar=c:\program files\WinRar\unrar.exe"
for /r "%sourceDir%" %%f in (*.rar) do for /d %%d in ("%~dpf\.") do (
"%unrar%" p -inul "%%~f" > "%targetDir%\%%~nd.pdf"
)
do you only have pdf files? If not, try this:
#ECHO OFF &SETLOCAL
set "SourceFolder=%userprofile%"
set "DestinationFolder=%temp%"
for /d /r "%SourceFolder%" %%a in (*) do for %%b in ("%%~fa\*.rar") do for /f "delims=" %%c in ('rar lb "%%~Fb"') do (
rar e -idq "%%~fb" "%%~c" "%DestinationFolder%"
ren "%DestinationFolder%\%%~c" "%%~na%%~Xc"
)
If you don't own rar, you can also work with the free unrar.
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"
)
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!