BAT File to delete old log entries from a TXT File - batch-file

I have a PC which runs specific BAT files at different times in the day. In order to ensure I keep track that all tasks are occurring I make each task register an entry in a log file called tack.txt. This is typically how it looks:
Start Cams # 10/09/2016 15:11:43
Update List # 11/09/2016 14:13:47
EPG Update # 11/09/2016 16:59:48
Start Cams # 11/09/2016 17:01:35
Restart # 13/09/2016 17:16:52
Each entry is on a separate line and has a date and time as presented.
Is it possible to have create a BAT file which deletes entries older than say 3 days to keep the process tidy?

Yes. It's hard to get yesterday's date with Batch (handling special cases across months/years/leapdays), but it's easy with a bit of help from Powershell.
You can get the dates like this:
for /f %%a in ('"powershell [DateTime]::Now.AddDays(-2).ToString('dd/MM/yyyy')"') do ( set today-2=%%a )
Then you can search for them like this, and output the matching lines to a file:
findstr "%today-2% %today-1% %today%" test.txt >matching.txt
Then you can replace your log file with matching.txt via copy /y and del.

Related

Changing Delimiter in Directory List

I'm a little bit stuck and need some help.
Currently have a process where completed work is stored in a directory as .txt files (the filename describes what the job was eg. Job1_Machine1_Randomly generated number.txt).
What I would like to do is run a dirlist via a batch file to easily extract the days work. I currently do this as:
dir *.* /s | find "%date%" >dirlisttoday.txt
The next part of my process is to upload the list into an Access database for matching and tracking, I use the files date and time stamp as a 'completed date'.
Currently the second process requires a manual manipulation as the Access import from .txt file only allows a single delimiter, (I have spaces between date/times and underscores in the title).
I can't use fixed width imports as Machine1 can vary in length. The directory is also used by other processes which can't be changed, so changing the filename isn't possible either.
I want to automate this process so it can be carried out by Windows Task Scheduler. Is there a line of script which can be added to my batch file to alter the directory list I create dirlisttoday.txt from:
31/08/2017 12:30 Job1_Machine1_Randomly generated number.txt
to:
31/08/2017 12:30 Job1 Machine1 Randomly generated number.txt

Cannot load files from a concatenated path

I am working on a script that will run daily. The script will compare individual configuration files from multiple host on one day with individual configuration files from a previous day. I am working on a CentOS host I have limited access to - meaning I can't make major changes.
Details
My hosts are running a cron job that uploads their configuration file to an sftp server in a generic stable directory (/var/log/Backups) by hostname (hostname.txt).
A cron job on the server creates a date stamped directory and moves the files from /var/log/Backups to /var/log/Backups/ddmmyyyy.
Later, well after all sftp and file move operations I want to load an array with the file names in a current directory and I load an array with matching file names from the previous days directory.
I want a script to diff the matching file names and output the information to a single text file.
I can't get the array to load the current days files and echo them to the terminal. I get a file operation error.
Script:
#!/bin/bash
# Set current date
now=`date +%d-%m-%Y`
echo $now
base=/var/log/GmonBackups/
loc=$base$now
echo $loc
# Load files from /var/log/GmonBackups/$now into an array
t_files=`ls loc`
echo $t_files
Something along these lines might help you get further:
today=$(date +%d%m%Y)
yesterday=$(date --date=yesterday +%d%m%Y)
base=/var/log/GmonBackups
today_dir=$base/$today
yesterday_dir=$base/$yesterday
today_files=( $today_dir/* )
yesterday_files=( $yesterday_dir/* )
A few points:
prefer $() to ``
don't use ls to get your list of files because it's not robust
I didn't put quotes around the variables because there are no spaces in your directory names.

SVN Update specific files from repository only

There's this repository which is many many gigabytes, 99% of which I don't need. What I want to do is get/update only the *.js *.css *.html .doc and *.pdf files. The rest, which are the enormous ones, I want to leave up there and not waste time and disk space getting because I don't need to look at them and I'll never be changing them.
I realize that the svn:ignore feature isn't what I need, that's related only to what gets checked in and what gets ignored. I also know that there's no parameters or settings in SVN that I can take advantage of to do what I want.
What I have found though is that if I right-click on my SVN folder and select "Check for Modifications" and then in the next dialog choose "Check repository" then I get a full list of the files I don't have. It's then an easy task to add "Extension" to the column headers and sort by extension. I can then scroll down and find all the .js files grouped together.
Here's where my #fail happens. If I right-click on ONE of the JS files and select UPDATE, then it will bring the file down and create the sub-directory hierarchy necessary to support that file. This is exactly what I'd want to happen. At this point I jump in the air thinking I've found what I need. This isn't such a troublesome process, I can live with this. Then I selected all of the JS files and right-clicked. First thing I noticed is that the context menu that appears has less options, that's troubling. But the UPDATE option is there, so I'm not too worried. I choose UPDATE then click OK, just like I did for the one single JS file I'd earlier tried. What happens next is the weird thing though. Instead of repeating the process that happened with the one single file, but this time to all selected files, it shows "Skipped" against each file and reports it's done. This happens every time. I can do each file manually (which would take hours) but I can't do them all at once.
Help. I'm doing this in a virtual machine which I'd rather not quadruple the size of just to get files I don't need.
Sorry for the delayed answer, there's been a lot going on and I also had to spend some time to get this working. As you already noted, there is no straightforward way to do an "extension-only-checkout". But there is a way using Subversion command-line tools and a batch script I wrote. It works by using the sparse directories feature of Subversion, which lets you specify which "depth" a checkout should have. By specifying a depth of empty, an empty working copy is created and no files or folders are actually checked out. Then, you can update immediate files and folders of your choice from the repository into that working copy. This allows to create that "extension-only-checkout" which you're after.
The script I wrote allows you to specify multiple extensions in the EXTENSIONS variable separated by spaces. The repository specified in the SVN_ROOT variable is then scanned for files with the given extensions. Then it proceeds to build up a working copy which consists only of the directory structure needed to support the files having the extensions you specified (using the method described above). I tested it quite a bit and hope it will suit your needs.
Note: Depending on the size of the repository and the number of files matching the specified extensions, the process of creating the working copy will take some time.
#ECHO OFF
SetLocal EnableDelayedExpansion
SET SVN_ROOT=svn://your-repository.com/svn/your-project
SET EXTENSIONS=.js .css .html .doc .pdf
SET ROOT_DIR=%CD%
ECHO Listing repository...
svn -R ls %SVN_ROOT% > _files-all.txt
REM filter list for specified extensions
FOR %%H IN (%EXTENSIONS%) DO (
TYPE _files-all.txt | FINDSTR /I /R "%%H$" >> _files-selected.txt
)
REM initial checkout in empty mode
svn co %SVN_ROOT% --depth empty .
FOR /F "tokens=*" %%I IN (_files-selected.txt) DO (
REM "escape" path elements by wrapping them into double quotes
SET TMP_PATH=%%I
SET TMP_PATH="!TMP_PATH:/=" "!"
ECHO Fetching %%I
REM iterate over path elements
FOR %%J IN (!TMP_PATH!) DO (
REM "unescape" each path element again
SET PATH_ELEM=%%J
SET PATH_ELEM=!PATH_ELEM:~1,-1!
REM if we don't have this element, fetch it from repository
IF NOT EXIST "!PATH_ELEM!" (
svn up %%J --depth empty 2>&1 > nul
)
REM if the element is a directory, enter it
IF EXIST %%~sJ\NUL CD %%J
)
CD !ROOT_DIR!
)
REM clean up temporary files
DEL _files-all.txt _files-selected.txt
I ended up having to abandon my dreams of having an svn update that only gets me certain file extensions and leaves all others on the server. I had to accept I need to get the whole thing, unless I want each update to involve navigating a large tree structure and selecting only the sub-folders I want.

batch file copy multiple .txt files with date restrictions from multiple directories to one directory

I am trying to copy all .txt files that I have scattered throughout several subdirectories of one main directory into another directory using a batch file. I have research this site and found lots of answers at this link: batch file Copy files with certain extensions from multiple directories into one directory. Like the code below from Jay:
set dSource=C:\Main directory\sub directory
set dTarget=D:\Documents
set fType=*.doc
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
copy /V "%%f" "%dTarget%\" 2>nul
)
My question is how to modify this code or other codes on this link to batch copy the files with time stamps, like I only want to copy .txt files created from Jan 1, 2012 to Nov 1, 2012.
My suggestion for finding and moving *.txt files in a directory tree of a drive, or the entire drive, or even multiple drives with last modification date in a definite time period is:
Start Windows Explorer.
Click on button Search.
Open advanced search options for finding files and folders.
Select/enter to search for files by last modification date.
Enter the two dates to specify time period or select the time period.
Run the search.
Select all found files in search result, for example with Ctrl+A.
Press Ctrl+X to mark the found files for being cut (moved).
Open the folder into which the files should be moved.
Press Ctrl+V to paste the files (move them).
That's it.
Nobody needs to code a batch job for this task if this find + move files job should not be done periodically using a scheduling task.
The exact steps for doing such an advanced find for files in a definite time period with Windows Explorer depends on version of Windows. See for example the computer tips
Find files by date modified in Windows for Windows 8/7/Vista, or
for Windows XP: How do I search for a file on my computer?
And of course there are many freeware and shareware tools which support also finding files according to various search criterias like last modification date within a specified time period and move them.
Well, that does not really answer the question as it does not contain the batch code for doing the job. So I answer this question with another question:
Why thinking about coding a batch file for such a task hard to adapt for varying dates if dozens of GUI applications including Windows Explorer exist doing the same by simple user input with no need on coding skills and therefore very easy to use, and the find + move must be done only once or from time to time with changed criterias?

DOS command to move all files in subdirectories one level up without overwriting same file name, unique size

MY QUESTION:
I have the same situation as Rishi. I have a number of versions of the same song by the same artist that appear on different CD's. If I use the batch command as written, will DOS overwrite songs with the same name, even if the file size is different for each unique file?
PREVIOUS QUESTION: DOS command to move all files in subdirectories one level up
REFERENCE Rishi asked the question on Jan 15th:
"I have a folder with a lot of sub folders with one or more files in each. I am trying to write a batch file that moves all those files to C:\songs (for example).
C:>FOR /R C:\Test %i IN (*) DO MOVE %i C:\Songs
The folders Test and songs exist, but I get an error saying
%i was unexpected at this time.
What am I doing wrong?"
ANSWER WAS
"FOR /R %i IN (C:\Test*) DO MOVE "%i" C:\Songs
In a batch file, it has to be %%i. Weird quirk of batch."
Within a given folder there can only be one version of a file with a given name. When executed within a batch, the MOVE command will automatically overwrite any pre-existing file of the same name. So the answer to your question is - YES, a file with the same name will be over-written, even if it has a different file size. (Note - if you are using Windows XP then you are not using DOS)
You can prevent a batch move from overwriting an existing file by piping N to MOVE with the -y option:
echo n | move /-y "%%~i" "C:\songs\"
If you want to copy and preserve both versions into the same folder, then at least one version will have to be renamed. You will have to decide what kind of naming scheme you want to use before you can begin coming up with a solution.

Resources