Extracting / unzipping files to folders with date / time stamp with 7zip 7z - date

I am using 7zip command line in a batch file to extract zip files to folders of the same name as the zip file and output the folders to a destination directory: I've tried a few variations without success including
7za x "%directory_destination%*.zip" -o%directory_destination%\%date%*"
7za x "%directory_destination%\*.zip" -o%directory_destination%\*"
I am having trouble including the current %date% and %time% in the folder name of the extracted zip file.
For example, the contents of a zip file called abc.zip should be extracted to a folder called abc_18.10.14_7.34 etc.

Test this on some sample files:
#echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
cd /d "c:\folder with zip files\"
set "directory_destination=c:\target\folder"
for /f "delims=" %%a in ('dir *.zip /b /a-d ') do (
md "%directory_destination%\%%~na_%dd%.%mm%.%yyyy%_%hh%.%min%%%~xa" 2>nul
7za x "%%a" -o"%directory_destination%\%%~na_%dd%.%mm%.%yyyy%_%hh%.%min%%%~xa"
)
pause

Related

Batch file to rename based on size on disk

I was going to make a batch script to share for ex-Dropbox account holders that will go through and rename a file OR folder if the size on disk is > 1 byte.
Using other forums I've managed to come up with the below.
#ECHO OFF
setlocal
set maxbytesize=1
FOR /F %%i IN ('PowerShell -ExecutionPolicy Bypass -File "FileSizeOnDisk.ps1" "E:\Desktop\Folder"') DO set SizeonDisk=%%i AND if %SizeonDisk% LSS %maxbytesize% (ren *.* *.*-DELETED)
EXIT
I just need help to loop the powershell command for every file, folder and files in a folder. I don't know how to properly place 2 commands after a DO and can't figure out the code to get it to work on multiple files.
#ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%o in ('dir /b /a-d "nameofdirectorytoscan\filemask"') do
FOR /F %%i IN ('PowerShell -ExecutionPolicy Bypass -File "FileSizeOnDisk.ps1" "nameofdirectorytoscan\%%o"') DO set "Jumba#%%o=%%i"
set size#
pause
The result is a set of variables named Jumba#nameoffile with values of filesize as returned from Powersmell.
You can then scan the list using
for /f "tokens=2,3delims=#=" %%b in ('set Jumba#') do echo name=%%b, size=%%c

BAT/POWERSHELL copy (only)yesterday files

I'm trying to write some script to copy all files which have been created yesterday (only!)
/D parameter for xcopy means copy files changed on or after the specified date so its not what i'm searching for. Any ideas? ;/
I suggest you to use powershell. You can use Get-ChildItem and Where-Object to get the list of files created the day before
$yesterdayFiles = Get-ChildItem | Where-Object {$_.CreationTime.Date -eq ((Get-Date).AddDays(-1).Date)}
Then you can copy the files stored in $yesterdayFiles variable using Copy-Item cmdlets
I've tried with robocopy /minage:1 /maxage:1 but seems does not work.But work when I set the current date.Here's the script (you'll need to set your source and destination):
#echo off
set "source=C:\folder1"
set "dest=C:\folder2"
pushd "%temp%"
::get cirrent date
makecab /D RptFileName=~.rpt /D InfFileName=nul /f nul >nul
for /f "tokens=3-7" %%a in ('find /i "makecab"^<~.rpt') do (
set "year=%%e"
set "mon=%%b"
set "day=%%c"
)
del ~.*
popd
:: convert month to numeric string
for %%a in (
"Jan-01" "Feb-02" "Mar-03" "Apr-04" "May-05" "Jun-06" "Jul-07" "Aug-08" "Sep-09" "Oct-10" "Nov-11" "Dec-12"
) do (
for /f "tokens=1,2 delims=-" %%x in ("%%~a") do (
if "%mon%" equ "%%x" (
set "mon=%%y"
goto :skip
)
)
)
:skip
set "c_date=%year%%mon%%day%"
::echo %c_date%
:: is switch is for force overwriting
robocopy "%source%" "%dest%" * /maxage:1 /minage:%c_date% /is
robocopy is built-in every windows since Vista.If your are running under XP or Vista you'll need to download it from microsoft site.
This Batch file selects all files that were created on the same date before today. If you are sure that there are files created yesterday, then it solve your problem.
#echo off
setlocal EnableDelayedExpansion
set "yesterday="
for /F "skip=5 tokens=1,4*" %%a in ('dir /TC /O-D /A-D') do (
if "%%a" neq "%date%" (
if not defined yesterday set "yesterday=%%a"
if "%%a" equ "!yesterday!" (
echo Created yesterday: %%a "%%c"
) else (
goto break
)
)
)
:break

batch file to copy only newly created or modified folders

I want to create a batch file which will copy only newly created folders.I am using the following code but with this it is picking only files(text file or xml files) not the folder.
xcopy "D:\Splunk\var\lib\splunk\defaultdb\db" "D:\test\Incremental_data_backup\" /m
Please suggest what I am missing
Thanks
Vikas
You can use xcopy for that, just need to specify the correct options. Here is which ones are relevant to you:
/H Copy hidden and system files and folders (default=N)
/D:mm-dd-yyyy
Copy files changed on or after the specified date.
If no date is given, copy only files whose
source date/time is newer than the destination time.
/S Copy folders and subfolders
/E Copy folders and subfolders, including Empty folders.
May be used to modify /T.
So something along the lines of:
xcopy <src> <dest> /HE /D:mm-dd-yyyy
Scripting the date is a bit more complicated:
%date:~4,2% - month
%date:-4% - year
%date:~7,2% - day
So the current date will be:
%date:~4,2%-%date:~7,2%-%date:-4%
You can try with this code. I use a parameter to mark as a modified folder
REM set up the TODAY variable
REM -----
for /f "tokens=1-3 delims=/ " %%a in ('date /T') do set year=%%c
for /f "tokens=1-3 delims=/ " %%a in ('date /T') do set month=%%b
for /f "tokens=1-3 delims=/ " %%a in ('date /T') do set day=%%a
set TODAY=%year%%month%%day%
REM -----
:: yesterdays date
#echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^&
right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YY=%result:~2,2%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "data=%mm%-%dd%-%yy%"
xcopy D:\YourFolder D:\BAK\BAK_%TODAY% /S /D:%data% /C /R /I /K /Y

Batch File to display file name and then certain lines in the files

I have a folder of .log files where the content of each file has multiple lines of the following format:
yyyy/mm/dd, hh:mm:ss, ComputerName, IPAddress, stuff, stuff
I would like to create a batch file to parse through the .log files and create the following output for any line in a file where ComputerName starts with "XPLT":
filename,yyyy/mm/dd,ComputerName,IPAddress
And preferably, I'd like to only look at files with a modified date within the last 30 days.
So far, I've only gotten the following code which doesn't even work and doesn't even include the file modified date and parsing by ComputerName. Looking for help because I've just not done this very much, and I can't find a good example online.
Echo EID,Date,PCName,IPAdd>CitrixLogs.csv
setlocal enabledelayedexpansion
for /f "tokens=1,3,4" %%i in ('dir /b "C:\LogFiles\*.log"') do (
echo %%i,%%j,%%k,%%l>>CitrixLogs.csv
)
I'd use a bit of FINDSTR magic:
set LOG_DIR=c:\logfiles
for /f "tokens=1-7 delims=:," %%L in ('%SystemRoot%\System32\findstr.exe /r /c:"^[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9],[0-9][0-9]:[0-9][0-9]:[0-9][0-9],xplt.*," %LOG_DIR%\*.log') do #echo %%L,%%M,%%N:%%O:%%P,%%Q,%%R,
`
REM
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=2-8delims=:, " %%a IN ('findstr /l /i /c:", XPLT" "%sourcedir%\*.log"') DO (
ECHO %%~nxa,%%b,%%c:%%d:%%e,%%f,%%g
)
GOTO :eof
redirect to your .csv as you will...
#echo off
setlocal enableextensions disabledelayedexpansion
:: configuration
set "logFolder=%cd%"
set "logFiles=*.log"
set "maxFileAge=30"
set "computerName=XPLT"
set "outputFile=CitrixLogs.csv"
:: adjust commands to execute according to configuration
set "ageFilter=robocopy "%logFolder%" "%logFolder%" "%logFiles%" /l /is /njh /njs /nc /ns /ndl /maxage:%maxFileAge%"
set "contentFilter=findstr /f:/ /i /r /c:"^^[^^,]*, [^^,]*, %computerName%" "
:: Generate output file
( echo(EID,Date,PCName,IPAdd
for /f "usebackq tokens=2,* delims=:" %%a in (
`cmd /q /c "for /f tokens^=* %%a in ('%ageFilter%') do echo(%%a" ^| %contentFilter% `
) do for /f "tokens=1,3,4 delims=," %%c in ("%%b") do echo(%%~nxa,%%c,%%d,%%e
) > "%outputFile%"
endlocal
This will use robocopy (or you can change it with forfiles) to search for files with a max age of 30 days in the indicated folder. Files will not be copied (/l) but the list will be echoed (the rest of the switch configure the output). This list of files is piped into findstr (/f:/) indicating where to search for the lines that match the indicated condition. This will generate an output with each line in the input file matching the condition, prefixed with the name (full name) of the file. This line is then splitted to output only the required fields.

Appending a txt file from multiple CSVs in subdirectories

I am trying to write a batch file which will append all *.csv files in the immediate subdirectories to a single text file in the current directory.
From various sources I have managed to piece together this code which works fine for files in the current dir but not sub-dirs
for %%a in (*.csv) do (type %%a >> csvreport.txt)
If anybody could help me with this I would be extremely grateful as I have tried various approaches with wildcards but without success.
Yet another option...
for /f usebackq %%a in (`dir /s /b *.csv`) do (type %%a >> csvreport.txt)
EDIT: Reading your details a bit more ... you want just the immediate directories, you can do this:
for /f usebackq %%a in (`dir /b /ad`) do for %%b in ("%%a"\*.csv) do (type "%%b" >> csvreport.txt)
for /R .\ %%a in (*.csv) do (type %%a >> csvreport.txt)
The /R indicates recursive and the parameter afterward is the folder in which to start (.\ is the current directory).
You can find up more if you run for /?
dir /ad /b > dirs.txt
for /f "tokens=1*" %%i in (dirs.txt) do cd %%i & for %%b in (*.csv) do (type %%b >> c:\csvreport.txt) & cd ..
Using the /R flag will traverse all subdirectory trees. You can nest the 'for' statements to only work with the immediate subdirectories but not their subdirectories.