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

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.

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

Update file in each subdirectory with a specific name

I have a batch-file which pulls a file from a url using powershell and then outputs/updates the file in a specific directory. But I have many of these directories, the only thing that changes about the path is numbers between \command\ and \setup\. How would I get it to put the file in every folder automatically?
Essentially I would like to output the downloaded text file in each of the install subdirectories of that path.
Also how could I make it happen silently?
#echo off
echo !!! PRESS ANY KEY TO CONTINUE AND UPDATE!!!
pause
powershell -Command "Invoke-WebRequest http://example.com/log/read.txt" -OutFile C:\Users\Administrator\AppData\Roaming\base\command\234235234\setup\install\read.txt 2>NUL >NUL
echo !!! DONE NOW !!!
echo !!! YOU CAN RE-OPEN NOW !!!
for /d /r "dirname" %%a in (*) do if /i "%%~nxa"=="install" echo %%a
may be useful to you.
Your requirement is unclear. Do you want to copy the file to the install subdirectories of ...\234235234\.. only, or of ...\*\... ?
Replace dirname with the name of the starting directory, be it ...\234235234\.. or C:\Users\Administrator\AppData\Roaming\base\command and the command I have shown will report all of the install directories contained under dirname. All you need then do is to change the echo to an appropriate copy command - see copy /? from the prompt. You can suppress copy's responses by appending >nul 2>nul (suppress messages and suppress error messages)
for /d /r with * as the list element will process a list of all subdirectories starting at the nominated directory. The if command selects only the leaf directories that match install in either case (/i)
Since
for /d /r ...
does nor detect hidden directories, another approach is
for /f "delims=" %%a in ('dir /s /b /ad "dirname" ') do if /i "%%~nxa"=="sub1" echo %%a
Which in this case should be
for /f "delims=" %%a in ('dir /s /b /ad "C:\Users\Administrator\AppData\Roaming\base\command" ') do if /i "%%~nxa"=="sub1" echo %%a
The dir command produces a list in /b basic (name-only) form, /s including subdirectories, /ad of directories only (names with the directory attribute set). This list is processed line-by-line by for /f without delimiters so the entire line (including spaces, if any) is assigned to %%a and displayed.

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

How to set variable with the result of findstr

I am trying to write a batch file which searches for pdf files and finds how many pages they have and loop all pages.
I wrote the following. I can find the files, I can even find the pagecounts with a tool named pdftk. The results is as below.
C:\Users\test\Documents\fishes\Fish_1.pdf
NumberOfPages: 5
How can I set a variable which has the value of 5?
#ECHO off
for /R %%i IN (*.pdf) DO (
ECHO %%i
"C:\Program Files (x86)\PDF Labs\PDFtk Server\bin\pdftk.exe" %%i dump_data | findstr NumberOfPages
set pagecount = findstr NumberOfPages ???
FOR /L %%j IN (1,1,%pagecount%) DO (
ECHO "page " + %%j
)
)
You were already 90% there. Use the FOR /F command to process the results of a command. Type HELP FOR from the command prompt for more info.
for /f "tokens=2" %%A in (
'"C:\Program Files (x86)\PDF Labs\PDFtk Server\bin\pdftk.exe" %%i dump_data ^| findstr NumberOfPages'
) do set numberOfPages=%%A

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.